r87251 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r87250‎ | r87251 | r87252 >
Date:16:13, 2 May 2011
Author:maxsem
Status:ok
Tags:
Comment:
JS conventions compliance
Modified paths:
  • /trunk/extensions/ApiSandbox/ext.apiSandbox.js (modified) (history)

Diff [purge]

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 ) {
55 return;
66 }
7 - content.show();
 7+ $content.show();
88
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' );
2420
 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+
2528 // load namespaces
2629 $.getJSON( apiPhp,
2730 { format: 'json', action: 'query', meta: 'siteinfo', siprop: 'namespaces' },
@@ -37,18 +40,18 @@
3841 namespaces.push( { key: id, value: ns } );
3942 }
4043 } else {
41 - showLoadError( further, 'apisb-namespaces-error' );
 44+ showLoadError( $further, 'apisb-namespaces-error' );
4245 }
4346 }
4447 );
4548
46 - action.change( updateBasics );
47 - prop.change( updateBasics );
 49+ $action.change( updateBasics );
 50+ $prop.change( updateBasics );
4851
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();
5356 }
5457 url += '&format=json'; // @todo:
5558 var params = '';
@@ -66,14 +69,14 @@
6770 params += '&' + name + '=' + encodeURIComponent( value );
6871 }
6972 }
70 - showLoading( output );
 73+ showLoading( $output );
7174 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();
7578 } else {
76 - requestUrl.val( url + params );
77 - postRow.hide();
 79+ $requestUrl.val( url + params );
 80+ $postRow.hide();
7881 }
7982 url = url.replace( /(&format=[^&]+)/, '$1fm' );
8083 var data = {
@@ -83,10 +86,10 @@
8487 type: isset( currentInfo.mustbeposted ) ? 'POST' : 'GET',
8588 success: function( data ) {
8689 data = data.match( /<pre>[\s\S]*<\/pre>/ )[0];
87 - output.html( data );
 90+ $output.html( data );
8891 },
8992 error: function( jqXHR, textStatus, errorThrown ) {
90 - showLoadError( output, 'apisb-request-error' );
 93+ showLoadError( $output, 'apisb-request-error' );
9194 }
9295 };
9396 $.ajax( data );
@@ -111,12 +114,12 @@
112115 }
113116
114117 function parseParamInfo( data ) {
115 - further.text( '' );
 118+ $further.text( '' );
116119 if ( !isset( data.paraminfo )
117120 || ( !isset( data.paraminfo.modules ) && !isset( data.paraminfo.querymodules ) )
118121 )
119122 {
120 - showLoadError( further, 'apisb-load-error' );
 123+ showLoadError( $further, 'apisb-load-error' );
121124 return;
122125 }
123126 if ( isset( data.paraminfo.modules ) ) {
@@ -126,13 +129,13 @@
127130 propCache[data.paraminfo.querymodules[0].name] = data.paraminfo.querymodules[0];
128131 createInputs( propCache[data.paraminfo.querymodules[0].name] );
129132 }
130 - submit.removeAttr( 'disabled' );
 133+ $submit.removeAttr( 'disabled' );
131134 }
132135
133136 function getQueryInfo( action, prop ) {
134137 var isQuery = action == 'query';
135138 if ( action == '-' || ( isQuery && prop == '-' ) ) {
136 - submit.attr( 'disabled', 'disabled' );
 139+ $submit.attr( 'disabled', 'disabled' );
137140 return;
138141 }
139142 var cached;
@@ -142,7 +145,7 @@
143146 cached = actionCache[action];
144147 }
145148 if ( typeof cached != 'object' ) { // stupid FF adds watch() everywhere
146 - showLoading( further );
 149+ showLoading( $further );
147150 var data = {
148151 format: 'json',
149152 action: 'paraminfo'
@@ -152,14 +155,14 @@
153156 } else {
154157 data.modules = action;
155158 }
156 - submit.attr( 'disabled', 'disabled' );
 159+ $submit.attr( 'disabled', 'disabled' );
157160 $.getJSON(
158161 mw.config.get( 'wgScriptPath' ) + '/api' + mw.config.get( 'wgScriptExtension' ),
159162 data,
160163 parseParamInfo
161164 );
162165 } else {
163 - submit.removeAttr( 'disabled' );
 166+ $submit.removeAttr( 'disabled' );
164167 createInputs( cached );
165168 }
166169 }
@@ -176,23 +179,23 @@
177180
178181 function createInputs( info ) {
179182 currentInfo = info;
180 - help.html( smartEscape( info.description ) );
 183+ $help.html( smartEscape( info.description ) );
181184 var s = '<table class="api-sandbox-options">\n<tbody>';
182185 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;
185188
186189 s += '<tr><td class="api-sandbox-label"><label for="param-' + name + '">' + name + '=</label></td>'
187190 + '<td class="api-sandbox-value">' + input( param, name )
188191 + '</td><td>' + smartEscape( param.description ) + '</td></tr>';
189192 }
190193 s += '\n</tbody>\n</table>\n';
191 - further.html( s );
 194+ $further.html( s );
192195 }
193196
194197 function input( param, name ) {
195 - var s;
196 - var value = '';
 198+ var s,
 199+ value = '';
197200 switch ( param.type ) {
198201 case 'limit':
199202 value = 10;
@@ -210,8 +213,8 @@
211214 param.type = namespaces;
212215 default:
213216 if ( typeof param.type == 'object' ) {
214 - var id = 'param-' + name;
215 - var attributes = { 'id': id };
 217+ var id = 'param-' + name,
 218+ attributes = { 'id': id };
216219 if ( isset( param.multi ) ) {
217220 attributes.multiple = 'multiple';
218221 s = select( param.type, attributes, false );
@@ -238,9 +241,9 @@
239242 selected = [];
240243 }
241244 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 };
245248 if ( $.inArray( value, selected ) >= 0 ) {
246249 attrs.selected = 'selected';
247250 }
@@ -251,16 +254,16 @@
252255 }
253256
254257 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';
258261 if ( isQuery ) {
259 - propRow.show();
 262+ $propRow.show();
260263 } else {
261 - propRow.hide();
 264+ $propRow.hide();
262265 }
263 - further.text( '' );
264 - help.text( '' );
 266+ $further.text( '' );
 267+ $help.text( '' );
265268 getQueryInfo( a, p );
266269 }
267270

Status & tagging log