Index: trunk/extensions/ApiSandbox/ext.apiSandbox.js |
— | — | @@ -1,26 +1,29 @@ |
2 | | -$( document ).ready( function() { |
3 | | - var content = $( '#api-sandbox-content' ); |
4 | | - if ( !content.length ) { |
| 2 | +jQuery( function( $ ) { |
| 3 | + var $content = $( '#api-sandbox-content' ); |
| 4 | + if ( !$content.length ) { |
5 | 5 | return; |
6 | 6 | } |
7 | | - content.show(); |
| 7 | + $content.show(); |
8 | 8 | |
9 | | - var action = $( '#api-sandbox-action' ); |
10 | | - var prop = $( '#api-sandbox-prop' ); |
11 | | - var propRow = $( '#api-sandbox-prop-row' ); |
12 | | - var help = $( '#api-sandbox-help' ); |
13 | | - var further = $( '#api-sandbox-further-inputs' ); |
14 | | - var submit = $( '#api-sandbox-submit' ); |
15 | | - var requestUrl = $( '#api-sandbox-url' ); |
16 | | - var requestPost = $( '#api-sandbox-post' ); |
17 | | - var output = $( '#api-sandbox-output' ); |
18 | | - var postRow = $( '#api-sandbox-post-row' ); |
19 | | - var actionCache = []; |
20 | | - var propCache = []; |
21 | | - var namespaces = []; |
22 | | - var currentInfo = {}; |
23 | | - var apiPhp = mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api' + mw.config.get( 'wgScriptExtension' ); |
| 9 | + // page elements |
| 10 | + var $action = $( '#api-sandbox-action' ), |
| 11 | + $prop = $( '#api-sandbox-prop' ), |
| 12 | + $propRow = $( '#api-sandbox-prop-row' ), |
| 13 | + $help = $( '#api-sandbox-help' ), |
| 14 | + $further = $( '#api-sandbox-further-inputs' ), |
| 15 | + $submit = $( '#api-sandbox-submit' ), |
| 16 | + $requestUrl = $( '#api-sandbox-url' ), |
| 17 | + $requestPost = $( '#api-sandbox-post' ), |
| 18 | + $output = $( '#api-sandbox-output' ), |
| 19 | + $postRow = $( '#api-sandbox-post-row' ); |
24 | 20 | |
| 21 | + // cached stuff |
| 22 | + var actionCache = [], |
| 23 | + propCache = [], |
| 24 | + namespaces = [], |
| 25 | + currentInfo = {}, |
| 26 | + apiPhp = mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api' + mw.config.get( 'wgScriptExtension' ); |
| 27 | + |
25 | 28 | // load namespaces |
26 | 29 | $.getJSON( apiPhp, |
27 | 30 | { format: 'json', action: 'query', meta: 'siteinfo', siprop: 'namespaces' }, |
— | — | @@ -37,18 +40,18 @@ |
38 | 41 | namespaces.push( { key: id, value: ns } ); |
39 | 42 | } |
40 | 43 | } else { |
41 | | - showLoadError( further, 'apisb-namespaces-error' ); |
| 44 | + showLoadError( $further, 'apisb-namespaces-error' ); |
42 | 45 | } |
43 | 46 | } |
44 | 47 | ); |
45 | 48 | |
46 | | - action.change( updateBasics ); |
47 | | - prop.change( updateBasics ); |
| 49 | + $action.change( updateBasics ); |
| 50 | + $prop.change( updateBasics ); |
48 | 51 | |
49 | | - submit.click( function() { |
50 | | - var url = apiPhp + '?action=' + action.val(); |
51 | | - if ( action.val() == 'query' ) { |
52 | | - url += '&prop=' + prop.val(); |
| 52 | + $submit.click( function() { |
| 53 | + var url = apiPhp + '?action=' + $action.val(); |
| 54 | + if ( $action.val() == 'query' ) { |
| 55 | + url += '&prop=' + $prop.val(); |
53 | 56 | } |
54 | 57 | url += '&format=json'; // @todo: |
55 | 58 | var params = ''; |
— | — | @@ -66,14 +69,14 @@ |
67 | 70 | params += '&' + name + '=' + encodeURIComponent( value ); |
68 | 71 | } |
69 | 72 | } |
70 | | - showLoading( output ); |
| 73 | + showLoading( $output ); |
71 | 74 | if ( isset( currentInfo.mustbeposted ) ) { |
72 | | - requestUrl.val( url ); |
73 | | - requestPost.val( params ); |
74 | | - postRow.show(); |
| 75 | + $requestUrl.val( url ); |
| 76 | + $requestPost.val( params ); |
| 77 | + $postRow.show(); |
75 | 78 | } else { |
76 | | - requestUrl.val( url + params ); |
77 | | - postRow.hide(); |
| 79 | + $requestUrl.val( url + params ); |
| 80 | + $postRow.hide(); |
78 | 81 | } |
79 | 82 | url = url.replace( /(&format=[^&]+)/, '$1fm' ); |
80 | 83 | var data = { |
— | — | @@ -83,10 +86,10 @@ |
84 | 87 | type: isset( currentInfo.mustbeposted ) ? 'POST' : 'GET', |
85 | 88 | success: function( data ) { |
86 | 89 | data = data.match( /<pre>[\s\S]*<\/pre>/ )[0]; |
87 | | - output.html( data ); |
| 90 | + $output.html( data ); |
88 | 91 | }, |
89 | 92 | error: function( jqXHR, textStatus, errorThrown ) { |
90 | | - showLoadError( output, 'apisb-request-error' ); |
| 93 | + showLoadError( $output, 'apisb-request-error' ); |
91 | 94 | } |
92 | 95 | }; |
93 | 96 | $.ajax( data ); |
— | — | @@ -111,12 +114,12 @@ |
112 | 115 | } |
113 | 116 | |
114 | 117 | function parseParamInfo( data ) { |
115 | | - further.text( '' ); |
| 118 | + $further.text( '' ); |
116 | 119 | if ( !isset( data.paraminfo ) |
117 | 120 | || ( !isset( data.paraminfo.modules ) && !isset( data.paraminfo.querymodules ) ) |
118 | 121 | ) |
119 | 122 | { |
120 | | - showLoadError( further, 'apisb-load-error' ); |
| 123 | + showLoadError( $further, 'apisb-load-error' ); |
121 | 124 | return; |
122 | 125 | } |
123 | 126 | if ( isset( data.paraminfo.modules ) ) { |
— | — | @@ -126,13 +129,13 @@ |
127 | 130 | propCache[data.paraminfo.querymodules[0].name] = data.paraminfo.querymodules[0]; |
128 | 131 | createInputs( propCache[data.paraminfo.querymodules[0].name] ); |
129 | 132 | } |
130 | | - submit.removeAttr( 'disabled' ); |
| 133 | + $submit.removeAttr( 'disabled' ); |
131 | 134 | } |
132 | 135 | |
133 | 136 | function getQueryInfo( action, prop ) { |
134 | 137 | var isQuery = action == 'query'; |
135 | 138 | if ( action == '-' || ( isQuery && prop == '-' ) ) { |
136 | | - submit.attr( 'disabled', 'disabled' ); |
| 139 | + $submit.attr( 'disabled', 'disabled' ); |
137 | 140 | return; |
138 | 141 | } |
139 | 142 | var cached; |
— | — | @@ -142,7 +145,7 @@ |
143 | 146 | cached = actionCache[action]; |
144 | 147 | } |
145 | 148 | if ( typeof cached != 'object' ) { // stupid FF adds watch() everywhere |
146 | | - showLoading( further ); |
| 149 | + showLoading( $further ); |
147 | 150 | var data = { |
148 | 151 | format: 'json', |
149 | 152 | action: 'paraminfo' |
— | — | @@ -152,14 +155,14 @@ |
153 | 156 | } else { |
154 | 157 | data.modules = action; |
155 | 158 | } |
156 | | - submit.attr( 'disabled', 'disabled' ); |
| 159 | + $submit.attr( 'disabled', 'disabled' ); |
157 | 160 | $.getJSON( |
158 | 161 | mw.config.get( 'wgScriptPath' ) + '/api' + mw.config.get( 'wgScriptExtension' ), |
159 | 162 | data, |
160 | 163 | parseParamInfo |
161 | 164 | ); |
162 | 165 | } else { |
163 | | - submit.removeAttr( 'disabled' ); |
| 166 | + $submit.removeAttr( 'disabled' ); |
164 | 167 | createInputs( cached ); |
165 | 168 | } |
166 | 169 | } |
— | — | @@ -176,23 +179,23 @@ |
177 | 180 | |
178 | 181 | function createInputs( info ) { |
179 | 182 | currentInfo = info; |
180 | | - help.html( smartEscape( info.description ) ); |
| 183 | + $help.html( smartEscape( info.description ) ); |
181 | 184 | var s = '<table class="api-sandbox-options">\n<tbody>'; |
182 | 185 | for ( var i = 0; i < info.parameters.length; i++ ) { |
183 | | - var param = info.parameters[i]; |
184 | | - var name = info.prefix + param.name; |
| 186 | + var param = info.parameters[i], |
| 187 | + name = info.prefix + param.name; |
185 | 188 | |
186 | 189 | s += '<tr><td class="api-sandbox-label"><label for="param-' + name + '">' + name + '=</label></td>' |
187 | 190 | + '<td class="api-sandbox-value">' + input( param, name ) |
188 | 191 | + '</td><td>' + smartEscape( param.description ) + '</td></tr>'; |
189 | 192 | } |
190 | 193 | s += '\n</tbody>\n</table>\n'; |
191 | | - further.html( s ); |
| 194 | + $further.html( s ); |
192 | 195 | } |
193 | 196 | |
194 | 197 | function input( param, name ) { |
195 | | - var s; |
196 | | - var value = ''; |
| 198 | + var s, |
| 199 | + value = ''; |
197 | 200 | switch ( param.type ) { |
198 | 201 | case 'limit': |
199 | 202 | value = 10; |
— | — | @@ -210,8 +213,8 @@ |
211 | 214 | param.type = namespaces; |
212 | 215 | default: |
213 | 216 | if ( typeof param.type == 'object' ) { |
214 | | - var id = 'param-' + name; |
215 | | - var attributes = { 'id': id }; |
| 217 | + var id = 'param-' + name, |
| 218 | + attributes = { 'id': id }; |
216 | 219 | if ( isset( param.multi ) ) { |
217 | 220 | attributes.multiple = 'multiple'; |
218 | 221 | s = select( param.type, attributes, false ); |
— | — | @@ -238,9 +241,9 @@ |
239 | 242 | selected = []; |
240 | 243 | } |
241 | 244 | for ( var i = 0; i < values.length; i++ ) { |
242 | | - var value = typeof values[i] == 'object' ? values[i].key : values[i]; |
243 | | - var face = typeof values[i] == 'object' ? values[i].value : values[i]; |
244 | | - var attrs = { 'value': value }; |
| 245 | + var value = typeof values[i] == 'object' ? values[i].key : values[i], |
| 246 | + face = typeof values[i] == 'object' ? values[i].value : values[i], |
| 247 | + attrs = { 'value': value }; |
245 | 248 | if ( $.inArray( value, selected ) >= 0 ) { |
246 | 249 | attrs.selected = 'selected'; |
247 | 250 | } |
— | — | @@ -251,16 +254,16 @@ |
252 | 255 | } |
253 | 256 | |
254 | 257 | function updateBasics() { |
255 | | - var a = action.val(); |
256 | | - var p = prop.val(); |
257 | | - var isQuery = a == 'query'; |
| 258 | + var a = $action.val(), |
| 259 | + p = $prop.val(), |
| 260 | + isQuery = a == 'query'; |
258 | 261 | if ( isQuery ) { |
259 | | - propRow.show(); |
| 262 | + $propRow.show(); |
260 | 263 | } else { |
261 | | - propRow.hide(); |
| 264 | + $propRow.hide(); |
262 | 265 | } |
263 | | - further.text( '' ); |
264 | | - help.text( '' ); |
| 266 | + $further.text( '' ); |
| 267 | + $help.text( '' ); |
265 | 268 | getQueryInfo( a, p ); |
266 | 269 | } |
267 | 270 | |