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

Revision: 88262
Author:   salvatoreingala
Date:     2011-05-16 19:35:49 +0000 (Mon, 16 May 2011)
Log Message:
-----------
Toy implementation of UI with HTMLForm-based dialogs

Modified Paths:
--------------
    branches/salvatoreingala/Gadgets/Gadgets.i18n.php
    branches/salvatoreingala/Gadgets/Gadgets.php
    branches/salvatoreingala/Gadgets/Gadgets_body.php

Added Paths:
-----------
    branches/salvatoreingala/Gadgets/GadgetsAjax.php
    branches/salvatoreingala/Gadgets/modules/
    branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js

Modified: branches/salvatoreingala/Gadgets/Gadgets.i18n.php
===================================================================
--- branches/salvatoreingala/Gadgets/Gadgets.i18n.php   2011-05-16 19:27:54 UTC 
(rev 88261)
+++ branches/salvatoreingala/Gadgets/Gadgets.i18n.php   2011-05-16 19:35:49 UTC 
(rev 88262)
@@ -43,6 +43,8 @@
 <pre>$2</pre>
 You must have appropriate permissions on destination wiki (including the right 
to edit system messages) and import from file uploads must be enabled.',
        'gadgets-export-download' => 'Download',
+       'gadgets-ajax-wrongparams' => 'An AJAX request with wrong parameters 
has been made; this is most likely a bug.',
+       'gadgets-ajax-unlogged' => 'This action is only allowed to registered, 
logged in users.',
 );
 
 /** Message documentation (Message documentation)

Modified: branches/salvatoreingala/Gadgets/Gadgets.php
===================================================================
--- branches/salvatoreingala/Gadgets/Gadgets.php        2011-05-16 19:27:54 UTC 
(rev 88261)
+++ branches/salvatoreingala/Gadgets/Gadgets.php        2011-05-16 19:35:49 UTC 
(rev 88262)
@@ -45,9 +45,18 @@
 $wgAutoloadClasses['GadgetHooks'] = $dir . 'Gadgets_body.php';
 $wgAutoloadClasses['GadgetResourceLoaderModule'] = $dir . 'Gadgets_body.php';
 $wgAutoloadClasses['SpecialGadgets'] = $dir . 'SpecialGadgets.php';
+$wgAutoloadClasses['GadgetsAjax'] = $dir . 'GadgetsAjax.php';
+$wgAutoloadClasses['GadgetsSpecialPreferencesTweaksModule'] = $dir . 
'GadgetsAjax.php';
 
 $wgSpecialPages['Gadgets'] = 'SpecialGadgets';
 $wgSpecialPageGroups['Gadgets'] = 'wiki';
 
 $wgAPIListModules['gadgetcategories'] = 'ApiQueryGadgetCategories';
 $wgAPIListModules['gadgets'] = 'ApiQueryGadgets';
+
+$wgAjaxExportList[] = 'GadgetsAjax::getUI';
+$wgAjaxExportList[] = 'GadgetsAjax::setPreferences';
+
+$wgResourceModules['ext.gadgets.preferences'] = array(
+       'class'                 => 'GadgetsSpecialPreferencesTweaksModule'
+);

Added: branches/salvatoreingala/Gadgets/GadgetsAjax.php
===================================================================
--- branches/salvatoreingala/Gadgets/GadgetsAjax.php                            
(rev 0)
+++ branches/salvatoreingala/Gadgets/GadgetsAjax.php    2011-05-16 19:35:49 UTC 
(rev 88262)
@@ -0,0 +1,59 @@
+<?php
+
+class GadgetsAjax {    
+       public static function getUI( /*$args...*/ ) {
+               global $wgUser;
+               
+               if ( $wgUser->isAnon() ) {
+                       return '<err#>' . wfMsgExt( 'gadgets-ajax-notallowed', 
'parseinline' );
+               }
+               
+               //params are in the format "param|val"
+               $args = func_get_args();
+
+               foreach ( $args as $arg ) {
+                       $set = explode( '|', $arg, 2 );
+                       if ( count( $set ) != 2 ) {
+                               return '<err#>' . wfMsgExt( 
'gadgets-ajax-wrongparams', 'parseinline' );
+                       }
+                       
+                       list( $par, $val ) = $set;
+                       
+                       switch( $par ) {
+                               case "gadget":
+                                       $gadget = $val;
+                                       break;
+                               default:
+                                       return '<err#>' . wfMsgExt( 
'gadgets-ajax-wrongparams', 'parseinline' );
+                       }
+               }
+
+               if ( !isset( $gadget ) ) {
+                       return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 
'parseinline' );                                
+               }
+               
+               $prefs_json = Gadget::getGadgetPrefsDescription( $gadget );
+               
+               //If $gadget doesn't exists or it doesn't have preferences, 
something is wrong
+               if ( $prefs_json === NULL || $prefs_json === '' ) {
+                       return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 
'parseinline' );                                
+               }
+               
+               $prefs = json_decode( $prefs_json, TRUE );
+               
+               //TODO: $prefs === NULL?
+               
+               //TODO: check correct usage of RequestContext
+               $form = new HTMLForm( $prefs, new RequestContext(), 
'gadget-prefs' );
+               
+               $form->mFieldData = Gadget::getUserPrefs( $wgUser, $gadget );
+
+               //TODO: HTMLForm::getBody is not meant to be public, a 
refactoring is needed            
+               return $form->getBody();
+       }
+       
+       public static function setPreferences( /* args */ ) {
+               //TODO
+               throw new MWException( 'Not implemented' );
+       }
+}


Property changes on: branches/salvatoreingala/Gadgets/GadgetsAjax.php
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: branches/salvatoreingala/Gadgets/Gadgets_body.php
===================================================================
--- branches/salvatoreingala/Gadgets/Gadgets_body.php   2011-05-16 19:27:54 UTC 
(rev 88261)
+++ branches/salvatoreingala/Gadgets/Gadgets_body.php   2011-05-16 19:35:49 UTC 
(rev 88262)
@@ -114,6 +114,10 @@
                
                wfProfileIn( __METHOD__ );
 
+               if ( $out->getTitle()->isSpecial( 'Preferences' ) ) {
+                       $out->addModules( 'ext.gadgets.preferences' );
+               }
+
                $gadgets = Gadget::loadList();
                if ( !$gadgets ) {
                        wfProfileOut( __METHOD__ );
@@ -526,8 +530,76 @@
 
                return $gadgets;
        }
+       
+       //Gets preferences for gadget $gadget;
+       // returns * NULL if the gadget doesn't exists
+       //         * '' if the gadget exists but doesn't have any preferences
+       //         * the preference description in JSON format, otherwise
+       public static function getGadgetPrefsDescription( $gadget ) {
+               //TODO: load gadget's preference description
+
+               //For now we assume there is only one gadget, named Test
+               if ( $gadget != 'Test' ) {
+                       return NULL;
+               }
+               //And here is his preferences description.
+               return <<<EOD
+{
+  "popupDelay": {
+    "type": "float",
+    "default": 0.5,
+    "label-message": "popup-delay-description",
+    "options": {
+      "min": 0,
+      "max": 3
+    }
+  },
+  "popupHideDelay": {
+    "type": "float",
+    "default": 0.5,
+    "label-message": "popup-hidedelay-description",
+    "options": {
+      "min": 0
+    }
+  },
+  "popupModifier": {
+    "type": "select",
+    "default": "",
+    "label-message": "popup-modifier-description",
+    "options": {
+      "popup-modifier-nothing": "",
+      "popup-modifier-ctrl": "ctrl",
+      "popup-modifier-shift": "shift",
+      "popup-modifier-alt": "alt",
+      "popup-modifier-meta": "meta"
+    }
+  }
 }
+EOD;
+       }
 
+       //Get user's preferences for a specific gadget
+       public static function getUserPrefs( $user, $gadget ) {
+               //TODO
+               //for now, we just return defaults
+               $prefs_json = Gadget::getGadgetPrefsDescription( $gadget );
+               
+               if ( $prefs_json === NULL || $prefs_json === '' ) {
+                       return NULL;
+               }
+               
+               $prefs = json_decode( $prefs_json, TRUE );
+               
+               $userPrefs = array();
+               
+               foreach ( $prefs as $pref => $value ) {
+                       $userPrefs[$pref] = $value["default"];
+               }
+
+               return $userPrefs;
+       }
+}
+
 /**
  * Class representing a list of resources for one gadget
  */
@@ -565,3 +637,41 @@
                return $this->dependencies;
        }
 }
+
+
+//Module to tweak Special:Preferences
+class GadgetsSpecialPreferencesTweaksModule extends ResourceLoaderFileModule {
+       public function __construct() {
+               parent::__construct( array(
+                               'scripts'               => array( 
'ext.gadgets.preferences.js' ),
+                               'dependencies'  => array( 'jquery', 
'jquery.ui.dialog', 'mediawiki.htmlform' ),
+                               'localBasePath' => dirname( __FILE__ ) . 
'/modules/',
+                               'remoteExtPath' => 'Gadgets/modules'
+                       )
+               );
+       }
+       
+       public function getScript( ResourceLoaderContext $context ) {
+               global $wgUser;
+               
+               $configurable_gadgets = array();
+               $gadgets_list = Gadget::loadStructuredList();
+               
+               foreach ( $gadgets_list as $section_name => $gadgets ) {
+                       foreach ( $gadgets as $gadget => $gadget_data ) {
+                               $prefs = Gadget::getGadgetPrefsDescription( 
$gadget );
+                               if ( $prefs !== NULL && $prefs !== '' ) {
+                                       $configurable_gadgets[] = $gadget;
+                               }
+                       }
+               }
+               
+               //create the mw.gadgets object
+               $script = "mw.gadgets = {}\n";
+               //needed by ext.gadgets.preferences.js
+               $script .= "mw.gadgets.configurableGadgets = " . 
Xml::encodeJsVar( $configurable_gadgets ) . ";\n";
+               $script .= parent::getScript( $context );
+               
+               return $script;
+       }
+}

Added: branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js
===================================================================
--- branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js         
                (rev 0)
+++ branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js 
2011-05-16 19:35:49 UTC (rev 88262)
@@ -0,0 +1,67 @@
+/*
+ * JavaScript tweaks for Special:Preferences
+ */
+( function( $, mw ) {
+  $( '#mw-htmlform-gadgets input[name="wpgadgets[]"]' ).each( function( idx, 
input ) {
+    var id = input.id;
+    var gadget = id.substr( "mw-input-wpgadgets-".length );
+
+    if ( $.inArray( gadget, mw.gadgets.configurableGadgets ) != -1 ) {
+      $span = $( '<span></span>' );
+
+      if ( !$( input ).is( ':checked' ) ) {
+        $span.hide();
+      }
+
+      $link = $( '<a></a>' )
+              .text( "Configure" ) //TODO: use a message instead
+              .click( function() {
+                var post_data = 'action=ajax&rs=GadgetsAjax::getUI' +
+                                '&rsargs[]=gadget|' + encodeURIComponent( 
gadget );
+                // Send POST request via AJAX!
+                $.ajax( {
+                  url     : mw.config.get( 'wgScriptPath' ) + '/index.php',
+                  type    : "POST",
+                  data    : post_data,
+                  dataType: "html", // response type
+                  success : function( response ) {
+                    //Show dialog
+                    $( response ).dialog( {
+                      modal: true,
+                      width: 'auto',
+                      resizable: false,
+                      title: 'Configuration of ' + gadget, //TODO: use messages
+                      close: function() {
+                        $(this).dialog('destroy').empty(); //completely 
destroy on close
+                      },
+                      buttons: {
+                        //TODO: add "Restore defaults" button
+                        "Save": function() {
+                          //TODO
+                          alert( "I should save changes now, but I don't know 
how :'(..." );
+                        },
+                        "Cancel": function() {
+                          $( this ).dialog( "close" );
+                        }
+                      }
+                    } );
+                  },
+                  error   : function( response ) {
+                    //TODO
+                    alert( 'Something wrong happened' );
+                  },
+                } );
+
+                return false; //prevent event propagation
+              } );
+
+      $span.append( "&nbsp;ยท&nbsp;", $link );
+      $( input ).next().append( $span );
+
+      //Toggle visibility on click to the input
+      $( input ).click( function() {
+        $span.toggle( 'fast' );
+      } );
+    }
+  } );
+} )( jQuery, mediaWiki );


Property changes on: 
branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js
___________________________________________________________________
Added: svn:eol-style
   + native


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to