Index: branches/salvatoreingala/Gadgets/Gadgets_body.php |
— | — | @@ -114,6 +114,7 @@ |
115 | 115 | |
116 | 116 | wfProfileIn( __METHOD__ ); |
117 | 117 | |
| 118 | + //tweaks in Special:Preferences |
118 | 119 | if ( $out->getTitle()->isSpecial( 'Preferences' ) ) { |
119 | 120 | $out->addModules( 'ext.gadgets.preferences' ); |
120 | 121 | } |
— | — | @@ -207,6 +208,21 @@ |
208 | 209 | $onByDefault = false, |
209 | 210 | $category; |
210 | 211 | |
| 212 | + |
| 213 | + //Mandatory arguments for any kind of preferences |
| 214 | + private static $prefs_mandatory_args = array( |
| 215 | + 'type', |
| 216 | + 'default' |
| 217 | + ); |
| 218 | + |
| 219 | + //Other mandatory arguments for specific types |
| 220 | + private static $prefs_mandatory_args_by_type = array( |
| 221 | + 'select' => array( 'options' ), |
| 222 | + 'selectorother' => array( 'options' ), |
| 223 | + 'selectandother' => array( 'options' ), |
| 224 | + 'multiselect' => array( 'options' ) |
| 225 | + ); |
| 226 | + |
211 | 227 | /** |
212 | 228 | * Creates an instance of this class from definition in MediaWiki:Gadgets-definition |
213 | 229 | * @param $definition String: Gadget definition |
— | — | @@ -531,51 +547,65 @@ |
532 | 548 | return $gadgets; |
533 | 549 | } |
534 | 550 | |
| 551 | + |
| 552 | + public static function isGadgetPrefsDescriptionValid( $prefs_json ) { |
| 553 | + $prefs = json_decode( $prefs_json, true ); |
| 554 | + |
| 555 | + if ( $prefs === null ) { |
| 556 | + return false; |
| 557 | + } |
| 558 | + |
| 559 | + //TODO: improve validation |
| 560 | + foreach ( $prefs as $option => $option_definition ) { |
| 561 | + foreach ( self::$prefs_mandatory_args as $arg ) { |
| 562 | + if ( !isset( $option_definition[$arg] ) ) { |
| 563 | + return false; |
| 564 | + } |
| 565 | + } |
| 566 | + |
| 567 | + $type = $option['type']; |
| 568 | + |
| 569 | + if ( isset( self::$prefs_mandatory_args_by_type[$type] ) ) { |
| 570 | + foreach ( self::$prefs_mandatory_args_by_type[$type] as $arg ) { |
| 571 | + if ( !isset( $option_definition[$arg] ) ) { |
| 572 | + return false; |
| 573 | + } |
| 574 | + } |
| 575 | + } |
| 576 | + } |
| 577 | + |
| 578 | + return true; |
| 579 | + } |
| 580 | + |
535 | 581 | //Gets preferences for gadget $gadget; |
536 | | - // returns * NULL if the gadget doesn't exists |
| 582 | + // returns * null if the gadget doesn't exists |
537 | 583 | // * '' if the gadget exists but doesn't have any preferences |
538 | 584 | // * the preference description in JSON format, otherwise |
539 | 585 | public static function getGadgetPrefsDescription( $gadget ) { |
540 | | - //TODO: load gadget's preference description |
541 | | - |
542 | | - //For now we assume there is only one gadget, named Test |
543 | | - if ( $gadget != 'Test' ) { |
544 | | - return NULL; |
| 586 | + $gadgets_list = Gadget::loadStructuredList(); |
| 587 | + foreach ( $gadgets_list as $section_name => $gadgets ) { |
| 588 | + foreach ( $gadgets as $gadget_name => $gadget_data ) { |
| 589 | + if ( $gadget_name == $gadget ) { |
| 590 | + //Gadget found; are there any prefs? |
| 591 | + |
| 592 | + $prefs_msg = "Gadget-" . $gadget . ".preferences"; |
| 593 | + |
| 594 | + //TODO: should we cache? |
| 595 | + |
| 596 | + $prefs_json = wfMsgForContentNoTrans( $prefs_msg ); |
| 597 | + if ( wfEmptyMsg( $prefs_msg, $prefs_json ) ) { |
| 598 | + return null; |
| 599 | + } |
| 600 | + |
| 601 | + if ( !self::isGadgetPrefsDescriptionValid( $prefs_json ) ){ |
| 602 | + return ''; |
| 603 | + } |
| 604 | + |
| 605 | + return $prefs_json; |
| 606 | + } |
| 607 | + } |
545 | 608 | } |
546 | | - //And here is his preferences description. |
547 | | - return <<<EOD |
548 | | -{ |
549 | | - "popupDelay": { |
550 | | - "type": "float", |
551 | | - "default": 0.5, |
552 | | - "label-message": "popup-delay-description", |
553 | | - "options": { |
554 | | - "min": 0, |
555 | | - "max": 3 |
556 | | - } |
557 | | - }, |
558 | | - "popupHideDelay": { |
559 | | - "type": "float", |
560 | | - "default": 0.5, |
561 | | - "label-message": "popup-hidedelay-description", |
562 | | - "options": { |
563 | | - "min": 0 |
564 | | - } |
565 | | - }, |
566 | | - "popupModifier": { |
567 | | - "type": "select", |
568 | | - "default": "", |
569 | | - "label-message": "popup-modifier-description", |
570 | | - "options": { |
571 | | - "popup-modifier-nothing": "", |
572 | | - "popup-modifier-ctrl": "ctrl", |
573 | | - "popup-modifier-shift": "shift", |
574 | | - "popup-modifier-alt": "alt", |
575 | | - "popup-modifier-meta": "meta" |
576 | | - } |
577 | | - } |
578 | | -} |
579 | | -EOD; |
| 609 | + return null; //gadget not found |
580 | 610 | } |
581 | 611 | |
582 | 612 | //Get user's preferences for a specific gadget |
— | — | @@ -584,11 +614,11 @@ |
585 | 615 | //for now, we just return defaults |
586 | 616 | $prefs_json = Gadget::getGadgetPrefsDescription( $gadget ); |
587 | 617 | |
588 | | - if ( $prefs_json === NULL || $prefs_json === '' ) { |
589 | | - return NULL; |
| 618 | + if ( $prefs_json === null || $prefs_json === '' ) { |
| 619 | + return null; |
590 | 620 | } |
591 | 621 | |
592 | | - $prefs = json_decode( $prefs_json, TRUE ); |
| 622 | + $prefs = json_decode( $prefs_json, true ); |
593 | 623 | |
594 | 624 | $userPrefs = array(); |
595 | 625 | |
— | — | @@ -660,12 +690,13 @@ |
661 | 691 | foreach ( $gadgets_list as $section_name => $gadgets ) { |
662 | 692 | foreach ( $gadgets as $gadget => $gadget_data ) { |
663 | 693 | $prefs = Gadget::getGadgetPrefsDescription( $gadget ); |
664 | | - if ( $prefs !== NULL && $prefs !== '' ) { |
| 694 | + if ( $prefs !== null && $prefs !== '' ) { |
665 | 695 | $configurable_gadgets[] = $gadget; |
666 | 696 | } |
667 | 697 | } |
668 | 698 | } |
669 | 699 | |
| 700 | + //TODO: broken in debug mode |
670 | 701 | //create the mw.gadgets object |
671 | 702 | $script = "mw.gadgets = {}\n"; |
672 | 703 | //needed by ext.gadgets.preferences.js |
Index: branches/salvatoreingala/Gadgets/Gadgets.i18n.php |
— | — | @@ -44,6 +44,7 @@ |
45 | 45 | You must have appropriate permissions on destination wiki (including the right to edit system messages) and import from file uploads must be enabled.', |
46 | 46 | 'gadgets-export-download' => 'Download', |
47 | 47 | 'gadgets-ajax-wrongparams' => 'An AJAX request with wrong parameters has been made; this is most likely a bug.', |
| 48 | + 'gadgets-ajax-wrongsyntax' => 'There was an unexpected error while reading the saved gadget\'s configuration description.', |
48 | 49 | 'gadgets-ajax-unlogged' => 'This action is only allowed to registered, logged in users.', |
49 | 50 | ); |
50 | 51 | |
Index: branches/salvatoreingala/Gadgets/GadgetsAjax.php |
— | — | @@ -29,22 +29,25 @@ |
30 | 30 | } |
31 | 31 | |
32 | 32 | if ( !isset( $gadget ) ) { |
33 | | - return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' ); |
| 33 | + return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' ); |
34 | 34 | } |
35 | 35 | |
36 | 36 | $prefs_json = Gadget::getGadgetPrefsDescription( $gadget ); |
37 | 37 | |
38 | 38 | //If $gadget doesn't exists or it doesn't have preferences, something is wrong |
39 | | - if ( $prefs_json === NULL || $prefs_json === '' ) { |
40 | | - return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' ); |
| 39 | + if ( $prefs_json === null || $prefs_json === '' ) { |
| 40 | + return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' ); |
41 | 41 | } |
42 | 42 | |
43 | | - $prefs = json_decode( $prefs_json, TRUE ); |
| 43 | + $prefs = json_decode( $prefs_json, true ); |
44 | 44 | |
45 | | - //TODO: $prefs === NULL? |
| 45 | + //If it's not valid JSON, signal an error |
| 46 | + if ( $prefs === null ) { |
| 47 | + return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongsyntax', 'parseinline' ); |
| 48 | + } |
46 | 49 | |
47 | | - //TODO: check correct usage of RequestContext |
48 | | - $form = new HTMLForm( $prefs, new RequestContext(), 'gadget-prefs' ); |
| 50 | + //TODO: options of "select" and similar fields cannot be passed as messages |
| 51 | + $form = new HTMLForm( $prefs, RequestContext::getMain() ); |
49 | 52 | |
50 | 53 | $form->mFieldData = Gadget::getUserPrefs( $wgUser, $gadget ); |
51 | 54 | |
Index: branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js |
— | — | @@ -7,13 +7,13 @@ |
8 | 8 | var gadget = id.substr( "mw-input-wpgadgets-".length ); |
9 | 9 | |
10 | 10 | if ( $.inArray( gadget, mw.gadgets.configurableGadgets ) != -1 ) { |
11 | | - $span = $( '<span></span>' ); |
| 11 | + var $span = $( '<span></span>' ); |
12 | 12 | |
13 | 13 | if ( !$( input ).is( ':checked' ) ) { |
14 | 14 | $span.hide(); |
15 | 15 | } |
16 | 16 | |
17 | | - $link = $( '<a></a>' ) |
| 17 | + var $link = $( '<a></a>' ) |
18 | 18 | .text( "Configure" ) //TODO: use a message instead |
19 | 19 | .click( function() { |
20 | 20 | var post_data = 'action=ajax&rs=GadgetsAjax::getUI' + |
— | — | @@ -60,7 +60,7 @@ |
61 | 61 | |
62 | 62 | //Toggle visibility on click to the input |
63 | 63 | $( input ).click( function() { |
64 | | - $span.toggle( 'fast' ); |
| 64 | + $span.fadeToggle( 'fast' ); |
65 | 65 | } ); |
66 | 66 | } |
67 | 67 | } ); |