http://www.mediawiki.org/wiki/Special:Code/MediaWiki/91456
Revision: 91456 Author: salvatoreingala Date: 2011-07-05 14:07:10 +0000 (Tue, 05 Jul 2011) Log Message: ----------- Added preferences of type 'date' with jquery.ui.datepicker. Modified Paths: -------------- branches/salvatoreingala/Gadgets/Gadgets.php branches/salvatoreingala/Gadgets/Gadgets_tests.php branches/salvatoreingala/Gadgets/backend/GadgetPrefs.php branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js Modified: branches/salvatoreingala/Gadgets/Gadgets.php =================================================================== --- branches/salvatoreingala/Gadgets/Gadgets.php 2011-07-05 13:52:18 UTC (rev 91455) +++ branches/salvatoreingala/Gadgets/Gadgets.php 2011-07-05 14:07:10 UTC (rev 91456) @@ -74,7 +74,7 @@ $wgResourceModules['jquery.formBuilder'] = array( 'scripts' => array( 'jquery.formBuilder.js' ), - 'dependencies' => array( 'jquery', 'jquery.ui.slider', 'jquery.validate' ), + 'dependencies' => array( 'jquery', 'jquery.ui.slider', 'jquery.ui.datepicker', 'jquery.validate' ), 'messages' => array( 'gadgets-formbuilder-required', 'gadgets-formbuilder-minlength', 'gadgets-formbuilder-maxlength', 'gadgets-formbuilder-min', 'gadgets-formbuilder-max', 'gadgets-formbuilder-integer' Modified: branches/salvatoreingala/Gadgets/Gadgets_tests.php =================================================================== --- branches/salvatoreingala/Gadgets/Gadgets_tests.php 2011-07-05 13:52:18 UTC (rev 91455) +++ branches/salvatoreingala/Gadgets/Gadgets_tests.php 2011-07-05 14:07:10 UTC (rev 91456) @@ -379,6 +379,55 @@ } } + //Tests for 'date' type preferences + function testPrefsDescriptionsDate() { + $correct = array( + 'fields' => array( + 'testDate' => array( + 'type' => 'date', + 'label' => 'some label', + 'default' => null + ) + ) + ); + + //Tests with correct default values + $correct2 = $correct; + foreach ( array( + null, + '2011-07-05T15:00:00Z', + '2011-01-01T00:00:00Z', + '2011-12-31T23:59:59Z', + ) as $def ) + { + $correct2['fields']['testDate']['default'] = $def; + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); + } + + //Tests with wrong default values + $wrong = $correct; + foreach ( array( + '', true, false, array(), 0, + '2011-07-05T15:00:00', + '2011-07-05T15:00:61Z', + '2011-07-05T15:61:00Z', + '2011-07-05T25:00:00Z', + '2011-07-32T15:00:00Z', + '2011-07-5T15:00:00Z', + '2011-7-05T15:00:00Z', + '2011-13-05T15:00:00Z', + '2011-07-05T15:00-00Z', + '2011-07-05T15-00:00Z', + '2011-07-05S15:00:00Z', + '2011-07:05T15:00:00Z', + '2011:07-05T15:00:00Z' + ) as $def ) + { + $wrong['fields']['testDate']['default'] = $def; + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); + } + } + //Data provider to be able to reuse this preference description for several tests. function prefsDescProvider() { return array( array( Modified: branches/salvatoreingala/Gadgets/backend/GadgetPrefs.php =================================================================== --- branches/salvatoreingala/Gadgets/backend/GadgetPrefs.php 2011-07-05 13:52:18 UTC (rev 91455) +++ branches/salvatoreingala/Gadgets/backend/GadgetPrefs.php 2011-07-05 14:07:10 UTC (rev 91456) @@ -104,6 +104,15 @@ 'isMandatory' => false, 'checker' => 'GadgetPrefs::isFloatOrInt' ) + ), + 'date' => array( + 'default' => array( + 'isMandatory' => true + ), + 'label' => array( + 'isMandatory' => true, + 'checker' => 'is_string' + ) ) ); @@ -112,7 +121,7 @@ 'string' => 'GadgetPrefs::checkStringOptionDefinition', 'number' => 'GadgetPrefs::checkNumberOptionDefinition', 'select' => 'GadgetPrefs::checkSelectOptionDefinition', - 'range' => 'GadgetPrefs::checkRangeOptionDefinition', + 'range' => 'GadgetPrefs::checkRangeOptionDefinition' ); //Further checks for 'string' options @@ -233,7 +242,7 @@ } $type = $optionDefinition['type']; - + if ( !isset( self::$prefsDescriptionSpecifications[$type] ) ) { return false; } @@ -403,6 +412,20 @@ } return true; + case 'date': + if ( $pref === null ) { + return true; + } + + //Basic syntactic checks + if ( !is_string( $pref ) || + !preg_match( '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/', $pref ) ) + { + return false; + } + + //Full parsing + return date_create( $pref ) !== false; default: return false; //unexisting type } Modified: branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js =================================================================== --- branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js 2011-07-05 13:52:18 UTC (rev 91455) +++ branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js 2011-07-05 14:07:10 UTC (rev 91456) @@ -138,7 +138,7 @@ return this.$c.is( ':checked' ); }; - //A field with a textbox + //A field with a textbox accepting string values StringField.prototype = object( LabelField.prototype ); StringField.prototype.constructor = StringField; @@ -191,7 +191,7 @@ return settings; }; - + //A field with a textbox accepting numeric values NumberField.prototype = object( LabelField.prototype ); NumberField.prototype.constructor = NumberField; function NumberField( $form, name, desc ){ @@ -250,7 +250,7 @@ return settings; }; - + //A field with a drop-down list SelectField.prototype = object( LabelField.prototype ); SelectField.prototype.constructor = SelectField; function SelectField( $form, name, desc ){ @@ -289,6 +289,7 @@ }; + //A field with a slider, representing ranges of numbers RangeField.prototype = object( LabelField.prototype ); RangeField.prototype.constructor = RangeField; function RangeField( $form, name, desc ){ @@ -336,13 +337,68 @@ return this.$slider.slider( 'value' ); }; + + //A field with a textbox with a datepicker + DateField.prototype = object( LabelField.prototype ); + DateField.prototype.constructor = DateField; + function DateField( $form, name, desc ){ + LabelField.call( this, $form, name, desc ); + if ( typeof desc.value == 'undefined' ) { + $.error( "desc.value is invalid" ); + } + + var date; + if ( desc.value !== null ) { + date = new Date( desc.value ); + + if ( !isFinite( date ) ) { + $.error( "desc.value is invalid" ); + } + } + + this.$text = $( '<input/>' ) + .attr( 'type', 'text' ) + .attr( 'id', idPrefix + this.name ) + .attr( 'name', idPrefix + this.name ) + .datepicker(); + + if ( desc.value !== null ) { + this.$text.datepicker( 'setDate', date ); + } + + this.$p.append( this.$text ); + } + + DateField.prototype.getValue = function() { + function pad( n ) { + return n < 10 ? '0' + n : n; + } + + var d = this.$text.datepicker( 'getDate' ); + + if ( d === null ) { + return null; + } + + //UTC date in ISO 8601 format [YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]Z + return '' + d.getUTCFullYear() + '-' + + pad( d.getUTCMonth() + 1 ) + '-' + + pad( d.getUTCDate() ) + 'T' + + pad( d.getUTCHours() ) + ':' + + pad( d.getUTCMinutes() ) + ':' + + pad( d.getUTCSeconds() ) + 'Z'; + }; + + + var validFieldTypes = { "boolean": BooleanField, "string" : StringField, "number" : NumberField, "select" : SelectField, - "range" : RangeField + "range" : RangeField, + "date" : DateField }; /* Public methods */ _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs