r110203 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110202‎ | r110203 | r110204 >
Date:04:44, 28 January 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
added combobox inputs for course level, field of study and master course
Modified paths:
  • /trunk/extensions/EducationProgram/EducationProgram.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPHTMLCombobox.php (added) (history)
  • /trunk/extensions/EducationProgram/includes/EPHTMLDateField.php (modified) (history)
  • /trunk/extensions/EducationProgram/resources/ep.combobox.css (added) (history)
  • /trunk/extensions/EducationProgram/resources/ep.combobox.js (added) (history)
  • /trunk/extensions/EducationProgram/specials/SpecialEditCourse.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/specials/SpecialEditCourse.php
@@ -21,7 +21,7 @@
2222 public function __construct() {
2323 parent::__construct( 'EditCourse', 'ep-course', 'EPCourse', 'Courses', 'Course' );
2424
25 - $this->getOutput()->addModules( 'ep.datepicker' );
 25+ $this->getOutput()->addModules( array( 'ep.datepicker', 'ep.combobox' ) );
2626 }
2727
2828 /**
@@ -54,7 +54,6 @@
5555 'type' => 'text',
5656 'label-message' => 'ep-course-edit-token',
5757 'maxlength' => 255,
58 - 'required' => true,
5958 'size' => 20,
6059 'validation-callback' => function ( $value, array $alldata = null ) {
6160 $strLen = strlen( $value );
@@ -80,16 +79,22 @@
8180 'required' => true,
8281 );
8382
 83+ $fieldFields = EPCourse::selectFields( 'field', array(), array( 'DISTINCT' ) );
 84+ $fieldFields = array_merge( array( '' => '' ), $fieldFields );
8485 $fields['field'] = array (
85 - 'type' => 'text',
 86+ 'class' => 'EPHTMLCombobox',
8687 'label-message' => 'ep-course-edit-field',
8788 'required' => true,
 89+ 'options' => array_combine( $fieldFields, $fieldFields ),
8890 );
8991
 92+ $levels = EPCourse::selectFields( 'level', array(), array( 'DISTINCT' ) );
 93+ $levels = array_merge( array( '' => '' ), $levels );
9094 $fields['level'] = array (
91 - 'type' => 'text',
 95+ 'class' => 'EPHTMLCombobox',
9296 'label-message' => 'ep-course-edit-level',
9397 'required' => true,
 98+ 'options' => array_combine( $levels, $levels ),
9499 );
95100
96101 $langOptions = EPUtils::getLanguageOptions( $this->getLanguage()->getCode() );
@@ -104,10 +109,13 @@
105110 }
106111 );
107112
 113+ $mcs = EPCourse::selectFields( 'mc', array(), array( 'DISTINCT' ) );
 114+ $mcs = array_merge( array( '' => '' ), $mcs );
108115 $fields['mc'] = array (
109 - 'type' => 'text',
 116+ 'class' => 'EPHTMLCombobox',
110117 'label-message' => 'ep-course-edit-mc',
111118 'required' => true,
 119+ 'options' => array_combine( $mcs, $mcs ),
112120 );
113121
114122 $fields['description'] = array (
Index: trunk/extensions/EducationProgram/includes/EPHTMLDateField.php
@@ -2,6 +2,7 @@
33
44 /**
55 * HTMLForm date field input.
 6+ * Requires jquery.datepicker
67 *
78 * @since 0.1
89 *
Index: trunk/extensions/EducationProgram/includes/EPHTMLCombobox.php
@@ -0,0 +1,32 @@
 2+<?php
 3+
 4+/**
 5+ * HTMLForm combobox field input.
 6+ * Requires easyui.combobox
 7+ *
 8+ * @since 0.1
 9+ *
 10+ * @file EPHTMLCombobox.php
 11+ * @ingroup EducationProgram
 12+ *
 13+ * @licence GNU GPL v3 or later
 14+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 15+ */
 16+class EPHTMLCombobox extends HTMLSelectField {
 17+
 18+ public function __construct( $params ) {
 19+ parent::__construct( $params );
 20+ $this->mClass .= " ep-combobox-tr";
 21+ }
 22+
 23+ function getInputHTML( $value ) {
 24+ return parent::getInputHTML( $value );
 25+ }
 26+
 27+ function validate( $value, $alldata ) {
 28+ // TODO: further validation
 29+
 30+ return true;
 31+ }
 32+
 33+}
Index: trunk/extensions/EducationProgram/EducationProgram.php
@@ -78,6 +78,7 @@
7979 $wgAutoloadClasses['EPCA'] = dirname( __FILE__ ) . '/includes/EPCA.php';
8080 $wgAutoloadClasses['EPCAPager'] = dirname( __FILE__ ) . '/includes/EPCAPager.php';
8181 $wgAutoloadClasses['EPHTMLDateField'] = dirname( __FILE__ ) . '/includes/EPHTMLDateField.php';
 82+$wgAutoloadClasses['EPHTMLCombobox'] = dirname( __FILE__ ) . '/includes/EPHTMLCombobox.php';
8283 $wgAutoloadClasses['EPRevision'] = dirname( __FILE__ ) . '/includes/EPRevision.php';
8384 $wgAutoloadClasses['EPRevisionPager'] = dirname( __FILE__ ) . '/includes/EPRevisionPager.php';
8485 $wgAutoloadClasses['EPPageObject'] = dirname( __FILE__ ) . '/includes/EPPageObject.php';
@@ -332,6 +333,20 @@
333334 ),
334335 );
335336
 337+$wgResourceModules['ep.combobox'] = $moduleTemplate + array(
 338+ 'scripts' => array(
 339+ 'ep.combobox.js',
 340+ ),
 341+ 'styles' => array(
 342+ 'ep.combobox.css',
 343+ ),
 344+ 'dependencies' => array(
 345+ 'jquery.ui.core',
 346+ 'jquery.ui.widget',
 347+ 'jquery.ui.autocomplete',
 348+ ),
 349+);
 350+
336351 $wgResourceModules['ep.formpage'] = $moduleTemplate + array(
337352 'scripts' => array(
338353 'ep.formpage.js',
Index: trunk/extensions/EducationProgram/resources/ep.combobox.css
@@ -0,0 +1,51 @@
 2+/**
 3+ * CSS for the Education Program MediaWiki extension.
 4+ * @see https://www.mediawiki.org/wiki/Extension:Education_Program
 5+ *
 6+ * Code borrowed from the Semantic Forms extension.
 7+ * https://www.mediawiki.org/wiki/Extension:Semantic_Forms
 8+ */
 9+
 10+/* Override some jQuery UI settings */
 11+.ui-button { margin-left: -1px; }
 12+.ui-button-icon-only .ui-button-text { padding: 0.35em; }
 13+button.ui-button-icon-only {
 14+ width: 1.7em;
 15+ height: 1.5em;
 16+ vertical-align: bottom;
 17+ margin-top: 0.2em;
 18+} /* button elements seem to need a little more width */
 19+/* Override CSS from Header Tabs extension, if tabs are used in the form */
 20+input.ep-combobox {
 21+ border: 1px #a6a6a6 solid;
 22+}
 23+
 24+.ui-widget input, .ui-widget {
 25+ font-family: sans-serif;
 26+ font-size: 10pt;
 27+}
 28+
 29+.ui-widget input.ui-widget-content {
 30+ padding-left: 0.2em;
 31+ margin: 0 0 0 .1em;
 32+}
 33+
 34+.ui-menu-item .ui-state-hover {
 35+ background: #222288;
 36+ color: white;
 37+ border: none;
 38+}
 39+ul.ui-menu {
 40+ padding: 0 0 5px 0;
 41+ max-height: 200px;
 42+ overflow: auto;
 43+}
 44+.ui-menu .ui-menu-item a.ui-state-hover {
 45+ margin: auto;
 46+}
 47+
 48+.ui-corner-left, .ui-corner-right, .ui-corner-all {
 49+ border-radius: 0;
 50+ -moz-border-radius: 0;
 51+ -webkit-border-radius: 0;
 52+}
\ No newline at end of file
Index: trunk/extensions/EducationProgram/resources/ep.combobox.js
@@ -0,0 +1,131 @@
 2+/**
 3+ * JavasSript for the Education Program MediaWiki extension.
 4+ * @see https://www.mediawiki.org/wiki/Extension:Education_Program
 5+ *
 6+ * @licence GNU GPL v3 or later
 7+ * @author Jeroen De Dauw <jeroendedauw at gmail dot com>
 8+ */
 9+
 10+(function( $, mw ) {
 11+
 12+ $.widget( "ep.combobox", {
 13+ _create: function() {
 14+ var self = this,
 15+ select = this.element.hide(),
 16+ selected = select.children( ":selected" ),
 17+ value = selected.val() ? selected.text() : "";
 18+ var input = this.input = $( "<input>" )
 19+ .insertAfter( select )
 20+ .val( value )
 21+ .autocomplete({
 22+ delay: 0,
 23+ minLength: 0,
 24+ source: function( request, response ) {
 25+ var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
 26+ response( select.children( "option" ).map(function() {
 27+ var text = $( this ).text();
 28+ if ( this.value && ( !request.term || matcher.test(text) ) )
 29+ return {
 30+ label: text.replace(
 31+ new RegExp(
 32+ "(?![^&;]+;)(?!<[^<>]*)(" +
 33+ $.ui.autocomplete.escapeRegex(request.term) +
 34+ ")(?![^<>]*>)(?![^&;]+;)", "gi"
 35+ ), "<strong>$1</strong>" ),
 36+ value: text,
 37+ option: this
 38+ };
 39+ }) );
 40+ },
 41+ select: function( event, ui ) {
 42+ ui.item.option.selected = true;
 43+ self._trigger( "selected", event, {
 44+ item: ui.item.option
 45+ });
 46+ },
 47+ change: function( event, ui ) {
 48+ if ( !ui.item ) {
 49+ var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
 50+ valid = false;
 51+ select.children( "option" ).each(function() {
 52+ if ( $( this ).text().match( matcher ) ) {
 53+ this.selected = valid = true;
 54+ return false;
 55+ }
 56+ });
 57+ return false;
 58+ }
 59+ }
 60+ })
 61+ .addClass( "ui-widget ui-widget-content ui-corner-left ep-combobox" );
 62+
 63+ input.data( "autocomplete" )._renderItem = function( ul, item ) {
 64+ return $( "<li></li>" )
 65+ .data( "item.autocomplete", item )
 66+ .append( "<a>" + item.label + "</a>" )
 67+ .appendTo( ul );
 68+ };
 69+
 70+ this.button = $( "<button type='button'>&nbsp;&nbsp;&nbsp;&nbsp;</button>" )
 71+ .attr( "tabIndex", -1 )
 72+ .attr( "title", "Show All Items" )
 73+ .insertAfter( input )
 74+ .button({
 75+ icons: {
 76+ primary: "ui-icon-triangle-1-s"
 77+ },
 78+ text: false
 79+ })
 80+ .removeClass( "ui-corner-all" )
 81+ .addClass( "ui-corner-right ui-button-icon" )
 82+ .css('cssText', 'padding: 0 !important; margin: 0 !important; -moz-border-radius: 0; -webkit-border-radius: 0; width: 1.7em;')
 83+ .click(function() {
 84+ // close if already visible
 85+ if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
 86+ input.autocomplete( "close" );
 87+ return;
 88+ }
 89+
 90+ // work around a bug (likely same cause as #5265)
 91+ $( this ).blur();
 92+
 93+ // pass empty string as value to search for, displaying all results
 94+ input.autocomplete( "search", "" );
 95+ input.focus();
 96+ });
 97+ },
 98+
 99+ destroy: function() {
 100+ this.input.remove();
 101+ this.button.remove();
 102+ this.element.show();
 103+ $.Widget.prototype.destroy.call( this );
 104+ }
 105+ });
 106+
 107+ $( document ).ready( function() {
 108+
 109+ $( '.mw-htmlform-submit' ).click( function( e ) {
 110+ $( '.ui-autocomplete-input' ).each( function() {
 111+ var has = false,
 112+ $input = $( this ),
 113+ $select = $input.closest( 'td' ).find( 'select.ep-combobox-tr' );
 114+
 115+ $select.find( 'option' ).each( function() {
 116+ if ( $( this ).text() === $input.val() ) {
 117+ has = true;
 118+ }
 119+ } );
 120+
 121+ if ( !has ) {
 122+ $select.append( $( '<option>' ).val( $input.val() ).text( $input.val() ) );
 123+ $select.val( $input.val() );
 124+ }
 125+ } );
 126+ } );
 127+
 128+ $( '.ep-combobox-tr' ).find( 'select' ).combobox();
 129+
 130+ } );
 131+
 132+})( window.jQuery );
\ No newline at end of file

Status & tagging log