Index: branches/RL2/extensions/Gadgets/Gadgets.i18n.php |
— | — | @@ -28,6 +28,7 @@ |
29 | 29 | Administrators manage the [[Special:Gadgets|gadget definitions, titles and descriptions]] of available gadgets.', |
30 | 30 | 'gadgets-preference-description' => '$1: $2', |
31 | 31 | 'gadgets-sharedprefstext' => 'Below is a list of gadgets from other wikis. TODO: This needs more text', |
| 32 | + 'gadgets-sharedprefs-ajaxerror' => 'An error occurred while attempting to fetch category and description information for shared gadgets. The gadgets below are shown uncategorized and without descriptions or proper titles.', |
32 | 33 | |
33 | 34 | # For Special:Gadgets |
34 | 35 | // General |
Index: branches/RL2/extensions/Gadgets/Gadgets.php |
— | — | @@ -234,6 +234,10 @@ |
235 | 235 | ), |
236 | 236 | 'ext.gadgets.preferences' => $gadResourceTemplate + array( |
237 | 237 | 'scripts' => 'ext.gadgets.preferences.js', |
| 238 | + 'dependencies' => 'ext.gadgets.api', |
| 239 | + 'messages' => array( |
| 240 | + 'gadgets-sharedprefs-ajaxerror' |
| 241 | + ), |
238 | 242 | ), |
239 | 243 | 'ext.gadgets.preferences.style' => $gadResourceTemplate + array( |
240 | 244 | 'styles' => 'ext.gadgets.preferences.css', |
Index: branches/RL2/extensions/Gadgets/modules/ext.gadgets.preferences.js |
— | — | @@ -11,7 +11,7 @@ |
12 | 12 | * This hides the container td for shared gadgets preferences, reorders them by moving them |
13 | 13 | * into a new td in the right order, updates their label texts, then replaces the old td with |
14 | 14 | * the new one. |
15 | | - * @param gadgetsByCategory {Object} Map of { repo: { categoryID: { gadgetID: gadgetDescription } } } |
| 15 | + * @param gadgetsByCategory {Object} Map of { repo: { categoryID: { gadgetID: gadgetObj } } } |
16 | 16 | * @param categoryNames {Object} Map of { repo: { categoryID: categoryDescription } } |
17 | 17 | */ |
18 | 18 | function fixPreferenceForm( gadgetsByCategory, categoryNames ) { |
— | — | @@ -22,6 +22,11 @@ |
23 | 23 | $spinner = $oldContainer.closest( '.mw-gadgetsshared-item-unloaded' ), |
24 | 24 | repo, category, gadget, $oldItem; |
25 | 25 | for ( repo in gadgetsByCategory ) { |
| 26 | + if ( repo == 'local' ) { |
| 27 | + // Skip local repository |
| 28 | + // FIXME: Just don't request the info in the first place then, waste of API reqs |
| 29 | + continue; |
| 30 | + } |
26 | 31 | $newContainer.append( $( '<h1>' ).text( repo ) ); |
27 | 32 | for ( category in gadgetsByCategory[repo] ) { |
28 | 33 | if ( category !== '' ) { |
— | — | @@ -33,7 +38,7 @@ |
34 | 39 | .find( '#mw-input-wpgadgetsshared-' + gadget ) |
35 | 40 | .closest( '.mw-htmlform-multiselect-item' ); |
36 | 41 | // Update the label text |
37 | | - $oldItem.find( 'label' ).text( gadgetsByCategory[repo][category][gadget] ); |
| 42 | + $oldItem.find( 'label' ).text( gadgetsByCategory[repo][category][gadget].title ); |
38 | 43 | // Move the item from $oldContainer to $newContainer |
39 | 44 | $newContainer.append( $oldItem ); |
40 | 45 | } |
— | — | @@ -43,49 +48,100 @@ |
44 | 49 | // Unhide the container by removing the unloaded class, and remove the spinner too |
45 | 50 | $spinner.removeClass( 'mw-gadgetsshared-item-unloaded mw-ajax-loader' ); |
46 | 51 | } |
47 | | - |
48 | | - // Temporary testing data |
49 | | - var categoryNames = { |
50 | | - 'wiki2': { |
51 | | - 'foo': 'The Foreign Category of Foo' |
52 | | - }, |
53 | | - 'wiki3': { |
54 | | - 'foo': 'The Remote Category of Foo', |
55 | | - 'bar': 'The Remote Category of Bar' |
| 52 | + |
| 53 | + /** |
| 54 | + * Displays an error on the shared gadgets preferences tab, between the intro text |
| 55 | + * and the checkboxes. This also unhides the checkboxes container and removes the spinner, |
| 56 | + * if applicable. |
| 57 | + * |
| 58 | + * @param msgKey {String} Message key of the error message |
| 59 | + */ |
| 60 | + function showPreferenceFormError( msgKey ) { |
| 61 | + var $oldContainer = $( '#mw-prefsection-gadgetsshared' ).find( '.mw-input' ), |
| 62 | + $oldContainerTR = $oldContainer.closest( '.mw-gadgetsshared-item-unloaded' ), |
| 63 | + $errorMsg = $( '<p>' ).addClass( 'error' ).text( mw.msg( msgKey ) ); |
| 64 | + |
| 65 | + $oldContainerTR |
| 66 | + .before( $( '<tr>' ).append( $( '<td>' ).attr( 'colspan', 2 ).append( $errorMsg ) ) ) |
| 67 | + // Unhide the container and remove the spinner |
| 68 | + .removeClass( 'mw-gadgetsshared-item-unloaded mw-ajax-loader' ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Reformat the output of mw.gadgets.api.getForeignGadgetsData() to |
| 73 | + * suitable input for fixPreferenceForm() |
| 74 | + * |
| 75 | + * @param data {Object} { repo: { gadgetID: gadgetObj } } |
| 76 | + * @return {Object} { repo: { categoryID: { gadgetID: gadgetObj } } } |
| 77 | + */ |
| 78 | + function reformatGadgetData( data ) { |
| 79 | + var retval = {}, repo, gadget, category; |
| 80 | + for ( repo in data ) { |
| 81 | + retval[repo] = { '': {} }; // Make sure '' is first in the list, fixPreferenceForm() needs it to be |
| 82 | + for ( gadget in data[repo] ) { |
| 83 | + category = data[repo][gadget].metadata.settings.category; |
| 84 | + if ( retval[repo][category] === undefined ) { |
| 85 | + retval[repo][category] = {}; |
| 86 | + } |
| 87 | + retval[repo][category][gadget] = data[repo][gadget]; |
| 88 | + } |
56 | 89 | } |
57 | | - }; |
58 | | - // TODO: Actually fetch this info using AJAX |
59 | | - var gadgetsByCategory = { |
60 | | - 'wiki2': { |
61 | | - '': { |
62 | | - 'b': 'Gadget B', |
63 | | - 'UTCLiveClock': 'A clock that displays UTC time blah blah blah' |
64 | | - }, |
65 | | - 'foo': { |
66 | | - 'a': 'Gadget A' |
| 90 | + return retval; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Reformat the output of mw.gadgets.api.getForeignGadgetCategories() |
| 95 | + * to suitable input for fixPreferenceForm() |
| 96 | + * |
| 97 | + * @param data {Object} { repo: [ { name: categoryID, title: categoryTitle } ] } |
| 98 | + * @return { repo: { categoryID: categoryTitle } } |
| 99 | + */ |
| 100 | + function reformatCategoryMap( data ) { |
| 101 | + var retval = {}, repo, i; |
| 102 | + for ( repo in data ) { |
| 103 | + retval[repo] = {}; |
| 104 | + for ( i = 0; i < data[repo].length; i++ ) { |
| 105 | + retval[repo][data[repo][i].name] = data[repo][i].title; |
67 | 106 | } |
68 | | - }, |
69 | | - 'wiki3': { |
70 | | - '': { |
71 | | - 'c': 'Gadget C', |
72 | | - 'd': 'Gadget D' |
73 | | - }, |
74 | | - 'foo': { |
75 | | - 'e': 'Gadget E' |
76 | | - }, |
77 | | - 'bar': { |
78 | | - 'f': 'Gadget F', |
79 | | - 'g': 'Gadget G' |
80 | | - } |
81 | 107 | } |
82 | | - }; |
| 108 | + return retval; |
| 109 | + } |
83 | 110 | |
84 | 111 | $( function() { |
85 | | - // TODO make all of this nicer once we have AJAX |
| 112 | + var gadgetsByCategory = null, categoryNames = null, failed = false; |
| 113 | + |
86 | 114 | // Add spinner |
87 | 115 | $( '#mw-prefsection-gadgetsshared' ).find( '.mw-gadgetsshared-item-unloaded' ).addClass( 'mw-ajax-loader' ); |
88 | | - // Simulate AJAX delay |
89 | | - setTimeout( function() { fixPreferenceForm( gadgetsByCategory, categoryNames ) }, 2000 ); |
| 116 | + |
| 117 | + // Do AJAX requests and call fixPreferenceForm() when done |
| 118 | + mw.gadgets.api.getForeignGadgetsData( |
| 119 | + function( data ) { |
| 120 | + gadgetsByCategory = reformatGadgetData( data ); |
| 121 | + if ( categoryNames !== null ) { |
| 122 | + fixPreferenceForm( gadgetsByCategory, categoryNames ); |
| 123 | + } |
| 124 | + }, |
| 125 | + function( error ) { |
| 126 | + if ( !failed ) { |
| 127 | + failed = true; |
| 128 | + showPreferenceFormError( 'gadgets-sharedprefs-ajaxerror' ); |
| 129 | + } |
| 130 | + } |
| 131 | + ); |
| 132 | + mw.gadgets.api.getForeignGadgetCategories( |
| 133 | + function( data ) { |
| 134 | + categoryNames = reformatCategoryMap( data ); |
| 135 | + if ( gadgetsByCategory !== null ) { |
| 136 | + fixPreferenceForm( gadgetsByCategory, categoryNames ); |
| 137 | + } |
| 138 | + }, |
| 139 | + function( error ) { |
| 140 | + if ( !failed ) { |
| 141 | + failed = true; |
| 142 | + showPreferenceFormError( 'gadgets-sharedprefs-ajaxerror' ); |
| 143 | + } |
| 144 | + } |
| 145 | + ); |
90 | 146 | } ); |
91 | 147 | |
92 | 148 | } )( jQuery ); |