http://www.mediawiki.org/wiki/Special:Code/MediaWiki/89658

Revision: 89658
Author:   salvatoreingala
Date:     2011-06-07 17:08:43 +0000 (Tue, 07 Jun 2011)
Log Message:
-----------
- Added options of type 'select'
- Minor glitches

Modified Paths:
--------------
    branches/salvatoreingala/Gadgets/Gadgets_body.php
    branches/salvatoreingala/Gadgets/modules/jquery.formBuilder.js

Modified: branches/salvatoreingala/Gadgets/Gadgets_body.php
===================================================================
--- branches/salvatoreingala/Gadgets/Gadgets_body.php   2011-06-07 17:04:29 UTC 
(rev 89657)
+++ branches/salvatoreingala/Gadgets/Gadgets_body.php   2011-06-07 17:08:43 UTC 
(rev 89658)
@@ -288,17 +288,31 @@
                                'isMandatory' => false,
                                'checker' => 'Gadget::isFloatOrInt'
                        )
+               ),
+               'select' => array(
+                       'default' => array(
+                               'isMandatory' => true
+                       ),
+                       'label' => array(
+                               'isMandatory' => true,
+                               'checker' => 'is_string'
+                       ),
+                       'options' => array(
+                               'isMandatory' => true,
+                               'checker' => 'is_array'
+                       )
                )
        );
 
        //Type-specific checkers for finer validation
        private static $typeCheckers = array(
-               'string' => 'Gadget::checkStringOption',
-               'number' => 'Gadget::checkNumberOption'
+               'string' => 'Gadget::checkStringOptionDefinition',
+               'number' => 'Gadget::checkNumberOptionDefinition',
+               'select' => 'Gadget::checkSelectOptionDefinition'
        );
        
        //Further checks for 'string' options
-       private static function checkStringOption( $option ) {
+       private static function checkStringOptionDefinition( $option ) {
                if ( isset( $option['minlength'] ) && $option['minlength'] < 0 
) {
                        return false;
                }
@@ -337,7 +351,7 @@
        }
        
        //Further checks for 'number' options
-       private static function checkNumberOption( $option ) {
+       private static function checkNumberOptionDefinition( $option ) {
                if ( isset( $option['integer'] ) && $option['integer'] === true 
) {
                        //Check if 'min', 'max' and 'default' are integers (if 
given)
                        if ( intval( $option['default'] ) != $option['default'] 
) {
@@ -364,6 +378,33 @@
                return true;
        }
 
+       private static function checkSelectOptionDefinition( $option ) {
+               $options = $option['options'];
+               
+               foreach ( $options as $opt => $optVal ) {
+                       //Correct value for $optVal are NULL, boolean, integer, 
float or string
+                       if ( $optVal !== NULL &&
+                               !is_bool( $optVal ) &&
+                               !is_int( $optVal ) &&
+                               !is_float( $optVal ) &&
+                               !is_string( $optVal ) )
+                       {
+                               return false;
+                       }
+               }
+               
+               $values = array_values( $options );
+               
+               $default = $option['default'];
+               
+               //Checks that $default is one of the option values
+               if ( !in_array( $default, $values, true ) ){
+                       return false;
+               }
+               
+               return true;
+       }
+
        /**
         * Creates an instance of this class from definition in 
MediaWiki:Gadgets-definition
         * @param $definition String: Gadget definition
@@ -744,10 +785,11 @@
                                        ++$count;
                                }
                                
-                               $checker = $typeSpec[$fieldName]['checker'];
-                               
-                               if ( !call_user_func( $checker, $fieldValue ) ) 
{
-                                       return false;
+                               if ( isset( $typeSpec[$fieldName]['checker'] ) 
) {
+                                       $checker = 
$typeSpec[$fieldName]['checker'];
+                                       if ( !call_user_func( $checker, 
$fieldValue ) ) {
+                                               return false;
+                                       }
                                }
                        }
                        
@@ -850,27 +892,37 @@
                                        return true;
                                }
                                
+                               if ( $pref === null ) {
+                                       return false; //$required === true, so 
null is not acceptable
+                               }
+
                                $integer = isset( $prefDescription['integer'] ) 
? $prefDescription['integer'] : false;
                                
                                if ( $integer === true && intval( $pref ) != 
$pref ) {
                                        return false; //not integer
                                }
                                
-                               if ( isset( $prefsDescription['min'] ) ) {
-                                       $min = $prefsDescription['min'];
+                               if ( isset( $prefDescription['min'] ) ) {
+                                       $min = $prefDescription['min'];
                                        if ( $pref < $min ) {
                                                return false; //value below 
minimum
                                        }
                                }
 
-                               if ( isset( $prefsDescription['max'] ) ) {
-                                       $max = $prefsDescription['max'];
+                               if ( isset( $prefDescription['max'] ) ) {
+                                       $max = $prefDescription['max'];
                                        if ( $pref > $max ) {
                                                return false; //value above 
maximum
                                        }
                                }
 
                                return true;
+                       case 'select':
+                               $values = array_values( 
$prefDescription['options'] );
+                               if ( !in_array( $pref, $values, true ) ) {
+                                       return false;
+                               }
+                               return true;
                        default:
                                return false; //unexisting type
                }
@@ -933,13 +985,12 @@
                
                
                if ( !$res ) {
-                       return null;
+                       $userPrefs = array(); //No prefs in DB, will just get 
defaults
+               } else {
+                       $userPrefsJson = $res->up_value;
+                       $userPrefs = FormatJson::decode( $userPrefsJson, true );
                }
                
-               $userPrefsJson = $res->up_value;
-               
-               $userPrefs = FormatJson::decode( $userPrefsJson, true );
-               
                self::matchPrefsWithDescription( $prefsDescription, $userPrefs 
);
                
                return $userPrefs;
@@ -1076,6 +1127,7 @@
        }
        
        
+       //TODO: should depend on gadget's last modification time, also
        public function getModifiedTime( ResourceLoaderContext $context ) {
                $touched = RequestContext::getMain()->getUser()->getTouched();
                

Modified: branches/salvatoreingala/Gadgets/modules/jquery.formBuilder.js
===================================================================
--- branches/salvatoreingala/Gadgets/modules/jquery.formBuilder.js      
2011-06-07 17:04:29 UTC (rev 89657)
+++ branches/salvatoreingala/Gadgets/modules/jquery.formBuilder.js      
2011-06-07 17:08:43 UTC (rev 89658)
@@ -241,12 +241,50 @@
                                
                return settings;
        }
+
+
+       SelectField.prototype = object( LabelField.prototype );
+       SelectField.prototype.constructor = SelectField;
+       function SelectField( name, desc ){ 
+               LabelField.call( this, name, desc );
+
+               var $select = this.$select = $( '<select/>' )
+                       .attr( 'id', idPrefix + this.name )
+                       .attr( 'name', idPrefix + this.name );
+               
+               var values = [];
+               $.each( desc.options, function( optName, optVal ) {
+                       var i = values.length;
+                       $( '<option/>' )
+                               .text( $s( optName ) )
+                               .val( i )
+                               .appendTo( $select );
+                       values.push( optVal );
+               } );
+
+               this.values = values;
+
+               if ( $.inArray( desc.value, values ) == -1 ) {
+                       $.error( "desc.value is not in the list of possible 
values" );
+               }
+
+               var i = $.inArray( desc.value, values )
+               $select.val( i ).attr( 'selected', 'selected' );
+
+               this.$p.append( $select );
+       }
        
+       SelectField.prototype.getValue = function() {
+               var i = parseInt( this.$select.val() );
+               return this.values[i];
+       };
+       
 
-       var validFields = {
+       var validFieldTypes = {
                "boolean": BooleanField,
                "string" : StringField,
-               "number" : NumberField
+               "number" : NumberField,
+               "select" : SelectField
        };
 
        function buildFormBody() {
@@ -283,7 +321,7 @@
                                //TODO: validate fieldName
                                var field = description.fields[fieldName];
 
-                               var FieldConstructor = validFields[field.type];
+                               var FieldConstructor = 
validFieldTypes[field.type];
 
                                if ( typeof FieldConstructor != 'function' ) {
                                        mw.log( "field with invalid type: " + 
field.type );


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to