Index: branches/RL2/extensions/Gadgets/modules/ext.gadgets.api.js |
— | — | @@ -17,7 +17,7 @@ |
18 | 18 | * @var {Object} Keyed by repo, array of category objects |
19 | 19 | * @example { repoName: [ {name: .., title: .., members: .. }, { .. }, { .. } ] } |
20 | 20 | */ |
21 | | - gadgetCategoryCache = null; |
| 21 | + gadgetCategoryCache = {}; |
22 | 22 | |
23 | 23 | /* Local functions */ |
24 | 24 | |
— | — | @@ -73,14 +73,14 @@ |
74 | 74 | * @param data {Object} Data to put in the cache |
75 | 75 | */ |
76 | 76 | function cacheGadgetData( repoName, id, data ) { |
77 | | - if ( id === null ) { |
78 | | - gadgetCache[repoName] = data; |
79 | | - } else { |
80 | | - if ( !( repoName in cache ) ) { |
81 | | - gadgetCache[repoName] = {}; |
| 77 | + if ( id === null ) { |
| 78 | + gadgetCache[repoName] = data; |
| 79 | + } else { |
| 80 | + if ( !( repoName in gadgetCache ) ) { |
| 81 | + gadgetCache[repoName] = {}; |
| 82 | + } |
| 83 | + gadgetCache[repoName][id] = data; |
82 | 84 | } |
83 | | - gadgetCache[repoName][id] = data; |
84 | | - } |
85 | 85 | } |
86 | 86 | |
87 | 87 | /* Public functions */ |
— | — | @@ -103,20 +103,21 @@ |
104 | 104 | // Find out how many repos there are |
105 | 105 | // Needs to be in a separate loop because we have to have the final number ready |
106 | 106 | // before we fire the first potentially (since it could be cached) async request |
107 | | - for ( repo in this.conf.repos ) { |
| 107 | + for ( repo in mw.gadgets.conf.repos ) { |
108 | 108 | numRepos++; |
109 | 109 | } |
110 | 110 | |
111 | | - for ( repo in this.conf.repos ) { |
112 | | - mw.gadgets.api.getGadgetData( null, function( data ) { |
113 | | - combined[repo] = data; |
114 | | - if ( ++successes === numRepos ) { |
115 | | - success( combined ); |
116 | | - } |
117 | | - }, function( errorCode ) { |
118 | | - error( errorCode ); |
119 | | - }, repo |
120 | | - ); |
| 111 | + for ( repo in mw.gadgets.conf.repos ) { |
| 112 | + mw.gadgets.api.getGadgetData( null, |
| 113 | + function( data ) { |
| 114 | + combined[repo] = data; |
| 115 | + if ( ++successes === numRepos ) { |
| 116 | + success( combined ); |
| 117 | + } |
| 118 | + }, function( errorCode ) { |
| 119 | + error( errorCode ); |
| 120 | + }, repo |
| 121 | + ); |
121 | 122 | } |
122 | 123 | }, |
123 | 124 | |
— | — | @@ -133,18 +134,19 @@ |
134 | 135 | // Find out how many repos there are |
135 | 136 | // Needs to be in a separate loop because we have to have the final number ready |
136 | 137 | // before we fire the first async request |
137 | | - for ( repo in this.conf.repos ) { |
| 138 | + for ( repo in mw.gadgets.conf.repos ) { |
138 | 139 | numRepos++; |
139 | 140 | } |
140 | 141 | |
141 | | - for ( repo in this.conf.repos ) { |
142 | | - mw.gadgets.api.getGadgetCategories( function( data ) { |
143 | | - combined[repo] = data; |
144 | | - if ( ++successes === numRepos ) { |
145 | | - success( combined ); |
146 | | - } |
| 142 | + for ( repo in mw.gadgets.conf.repos ) { |
| 143 | + mw.gadgets.api.getGadgetCategories( |
| 144 | + function( data ) { |
| 145 | + combined[repo] = data; |
| 146 | + if ( ++successes === numRepos ) { |
| 147 | + success( combined ); |
| 148 | + } |
147 | 149 | }, function( errorCode ) { |
148 | | - error( errorCode ); |
| 150 | + error( errorCode ); |
149 | 151 | }, repo |
150 | 152 | ); |
151 | 153 | } |
— | — | @@ -156,9 +158,10 @@ |
157 | 159 | * @param success {Function} To be called with the gadget object or array of gadget objects as first argument. |
158 | 160 | * @param error {Function} If something went wrong (inexisting gadget, api |
159 | 161 | * error, request error), this is called with error code as first argument. |
160 | | - * @param repoName {String} Name of the repository, key in this.conf.repos |
| 162 | + * @param repoName {String} Name of the repository, key in mw.gadgets.conf.repos. Defaults to 'local' |
161 | 163 | */ |
162 | 164 | getGadgetData: function( id, success, error, repoName ) { |
| 165 | + repoName = repoName || 'local'; |
163 | 166 | // Check cache |
164 | 167 | if ( repoName in gadgetCache && gadgetCache[repoName] !== null ) { |
165 | 168 | if ( id === null ) { |
— | — | @@ -178,10 +181,10 @@ |
179 | 182 | galanguage: mw.config.get( 'wgUserLanguage' ) |
180 | 183 | }; |
181 | 184 | if ( id !== null ) { |
182 | | - data.gaids = id; |
| 185 | + queryData.gaids = id; |
183 | 186 | } |
184 | 187 | $.ajax({ |
185 | | - url: this.conf.repos[repoName].apiScript || mw.util.wikiScript( 'api' ), |
| 188 | + url: mw.gadgets.conf.repos[repoName].apiScript, |
186 | 189 | data: queryData, |
187 | 190 | type: 'GET', |
188 | 191 | dataType: 'json', |
— | — | @@ -218,10 +221,11 @@ |
219 | 222 | * |
220 | 223 | * @param success {Function} To be called with an array as first argument. |
221 | 224 | * @param error {Function} To be called with a string (error code) as first argument. |
222 | | - * @param repoName {String} Name of the repository, key in this.conf.repos |
| 225 | + * @param repoName {String} Name of the repository, key in mw.gadgets.conf.repos . Defaults to 'local' |
223 | 226 | * @return {jqXHR|Null}: Null if served from cache, otherwise the jqXHR. |
224 | 227 | */ |
225 | 228 | getGadgetCategories: function( success, error, repoName ) { |
| 229 | + repoName = repoName || 'local'; |
226 | 230 | // Check cache |
227 | 231 | if ( repoName in gadgetCategoryCache && gadgetCategoryCache[repoName] !== null ) { |
228 | 232 | success( arrClone( gadgetCategoryCache[repoName] ) ); |
— | — | @@ -229,7 +233,7 @@ |
230 | 234 | } |
231 | 235 | // Get from API if not cached |
232 | 236 | return $.ajax({ |
233 | | - url: this.conf.repos[repoName].apiScript || mw.util.wikiScript( 'api' ), |
| 237 | + url: mw.gadgets.conf.repos[repoName].apiScript, |
234 | 238 | data: { |
235 | 239 | format: 'json', |
236 | 240 | action: 'query', |