r82009 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82008‎ | r82009 | r82010 >
Date:09:33, 12 February 2011
Author:aaron
Status:deferred
Tags:
Comment:
Refactored AJAX review code in review.js and removed mediawiki.legacy.ajax dependency
Modified paths:
  • /trunk/extensions/FlaggedRevs/FlaggedRevs.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/client/review.js (modified) (history)

Diff [purge]

Index: trunk/extensions/FlaggedRevs/FlaggedRevs.php
@@ -377,7 +377,7 @@
378378 'revreview-submit-reviewed', 'revreview-submit-unreviewed',
379379 'revreview-submitting', 'actioncomplete', 'actionfailed'
380380 ),
381 - 'dependencies' => array( 'mediawiki.legacy.ajax' ),
 381+ 'dependencies' => array( 'jquery' ),
382382 'localBasePath' => $dir,
383383 'remoteExtPath' => 'FlaggedRevs'
384384 );
Index: trunk/extensions/FlaggedRevs/client/review.js
@@ -60,7 +60,7 @@
6161 }
6262
6363 // Update colors of <select>
64 - this.updateRatingFormColors();
 64+ FlaggedRevsReview.updateRatingFormColors( ratingform );
6565 },
6666
6767 /*
@@ -87,246 +87,237 @@
8888 * NOTE: this is used so that they can be re-enabled if a rating changes
8989 */
9090 'maybeDisableAcceptButton': function() {
91 - if ( typeof(jsReviewNeedsChange) != 'undefined' && jsReviewNeedsChange == 1 ) {
 91+ if( typeof(jsReviewNeedsChange) != 'undefined' && jsReviewNeedsChange == 1 ) {
9292 var asubmit = document.getElementById('mw-fr-submit-accept');
9393 if( asubmit ) {
9494 asubmit.disabled = 'disabled';
9595 }
9696 }
9797 },
98 -};
99 -
100 -// @TODO: use jQuery AJAX
101 -// dependencies:
102 -// * ajax.js:
103 - /*extern sajax_init_object, sajax_do_call */
104 -// * wikibits.js:
105 - /*extern jsMsg */
106 -// These should have been initialized in the generated js
107 -if( typeof wgAjaxReview === "undefined" || !wgAjaxReview ) {
108 - wgAjaxReview = {};
109 -}
110 -
111 -wgAjaxReview.supported = true; // supported on current page and by browser
112 -wgAjaxReview.inprogress = false; // ajax request in progress
113 -wgAjaxReview.timeoutID = null; // see wgAjaxReview.ajaxCall
114 -
115 -// Args build-up from radios/checkboxes based on patch by Daniel Arnold (bug 13744)
116 -wgAjaxReview.ajaxCall = function() {
117 - if( !wgAjaxReview.supported ) {
118 - return true;
119 - } else if( wgAjaxReview.inprogress ) {
120 - return false;
121 - }
122 - if( !wfSupportsAjax() ) {
123 - // Lazy initialization so we don't toss up
124 - // ActiveX warnings on initial page load
125 - // for IE 6 users with security settings.
126 - wgAjaxReview.supported = false;
127 - return true;
128 - }
129 - var form = document.getElementById("mw-fr-reviewform");
130 - var notes = document.getElementById("wpNotes");
131 - var reason = document.getElementById("wpReason");
132 - if( !form ) {
133 - return false;
134 - }
135 - wgAjaxReview.inprogress = true;
136 - // Build up arguments
137 - var args = [];
138 - var inputs = form.getElementsByTagName("input");
139 - for( var i=0; i < inputs.length; i++) {
140 - // Different input types may occur depending on tags...
141 - if( inputs[i].name == "title" || inputs[i].name == "action" ) {
142 - continue; // No need to send these...
143 - } else if( inputs[i].type == "submit" ) {
144 - if( inputs[i].id == this.id ) {
145 - args.push( inputs[i].name + "|1" );
146 - // Show that we are submitting via this button
147 - inputs[i].value = mediaWiki.msg('revreview-submitting');
 98+
 99+ /*
 100+ * Enable AJAX-based submit functionality to the review form on this page
 101+ */
 102+ 'enableAjaxReview': function() {
 103+ var asubmit = document.getElementById("mw-fr-submit-accept");
 104+ if( asubmit ) {
 105+ asubmit.onclick = FlaggedRevsReview.submitRevisionReview;
 106+ }
 107+ var usubmit = document.getElementById("mw-fr-submit-unaccept");
 108+ if( usubmit ) {
 109+ usubmit.onclick = FlaggedRevsReview.submitRevisionReview;
 110+ }
 111+ },
 112+
 113+ /*
 114+ * Lock review form from submissions (using during AJAX requests)
 115+ */
 116+ 'lockReviewForm': function( form ) {
 117+ var inputs = form.getElementsByTagName("input");
 118+ for( var i=0; i < inputs.length; i++) {
 119+ inputs[i].disabled = "disabled";
 120+ }
 121+ var textareas = document.getElementsByTagName("textarea");
 122+ for( var i=0; i < textareas.length; i++) {
 123+ textareas[i].disabled = "disabled";
 124+ }
 125+ var selects = form.getElementsByTagName("select");
 126+ for( var i=0; i < selects.length; i++) {
 127+ selects[i].disabled = "disabled";
 128+ }
 129+ },
 130+
 131+ /*
 132+ * Unlock review form from submissions (using after AJAX requests)
 133+ */
 134+ 'unlockReviewForm': function( form ) {
 135+ var inputs = form.getElementsByTagName("input");
 136+ for( var i=0; i < inputs.length; i++) {
 137+ if( inputs[i].type != 'submit' ) { // not all buttons can be enabled
 138+ inputs[i].disabled = "";
 139+ } else {
 140+ inputs[i].blur(); // focus off element (bug 24013)
148141 }
149 - } else if( inputs[i].type == "checkbox" ) {
150 - args.push( inputs[i].name + "|" + (inputs[i].checked ? inputs[i].value : 0) );
151 - } else if( inputs[i].type == "radio" ) {
152 - if( inputs[i].checked ) { // must be checked
153 - args.push( inputs[i].name + "|" + inputs[i].value );
154 - }
155 - } else {
156 - args.push( inputs[i].name + "|" + inputs[i].value ); // text/hiddens...
157142 }
158 - inputs[i].disabled = "disabled";
159 - }
160 - if( notes ) {
161 - args.push( notes.name + "|" + notes.value );
162 - notes.disabled = "disabled";
163 - }
164 - var selects = form.getElementsByTagName("select");
165 - for( var i=0; i < selects.length; i++) {
166 - // Get the selected tag level...
167 - if( selects[i].selectedIndex >= 0 ) {
168 - var soption = selects[i].getElementsByTagName("option")[selects[i].selectedIndex];
169 - args.push( selects[i].name + "|" + soption.value );
 143+ var textareas = document.getElementsByTagName("textarea");
 144+ for( var i=0; i < textareas.length; i++) {
 145+ textareas[i].disabled = "";
170146 }
171 - selects[i].disabled = "disabled";
172 - }
173 - // Send!
174 - var old = sajax_request_type;
175 - sajax_request_type = "POST";
176 - sajax_do_call( "RevisionReview::AjaxReview", args, wgAjaxReview.processResult );
177 - sajax_request_type = old;
178 - // If the request isn't done in 30 seconds, allow user to try again
179 - wgAjaxReview.timeoutID = window.setTimeout(
180 - function() { wgAjaxReview.inprogress = false; wgAjaxReview.unlockForm(); },
181 - 30000
182 - );
183 - return false;
184 -};
185 -
186 -wgAjaxReview.unlockForm = function() {
187 - var form = document.getElementById("mw-fr-reviewform");
188 - var notes = document.getElementById("wpNotes");
189 - var reason = document.getElementById("wpReason");
190 - if( !form ) {
191 - return false;
192 - }
193 - var inputs = form.getElementsByTagName("input");
194 - for( var i=0; i < inputs.length; i++) {
195 - if( inputs[i].type != 'submit' ) {
196 - inputs[i].disabled = "";
197 - } else {
198 - inputs[i].blur(); // focus off element (bug 24013)
 147+ var selects = form.getElementsByTagName("select");
 148+ for( var i=0; i < selects.length; i++) {
 149+ selects[i].disabled = "";
199150 }
200 - }
201 - if( notes ) {
202 - notes.disabled = "";
203 - }
204 - if( reason ) {
205 - reason.disabled = "";
206 - }
207 - var selects = form.getElementsByTagName("select");
208 - for( var i=0; i < selects.length; i++) {
209 - selects[i].disabled = "";
210 - }
211 - return true;
212 -};
213 -
214 -wgAjaxReview.processResult = function(request) {
215 - if( !wgAjaxReview.supported ) {
216 - return;
217 - }
218 - wgAjaxReview.inprogress = false;
219 - if( wgAjaxReview.timeoutID ) {
220 - window.clearTimeout(wgAjaxReview.timeoutID);
221 - }
222 - var response = request.responseText; // full response text
223 - var msg = response.substr(6); // remove <err#> or <suc#>
224 - // Read new "last change time" timestamp for conflict handling
225 - var m = msg.match(/^<lct#(\d*)>(.+)/m);
226 - if( m ) msg = m[2]; // remove tag from msg
227 - var changeTime = m ? m[1] : null; // MW TS
228 - // Some form elements...
229 - var asubmit = document.getElementById('mw-fr-submit-accept');
230 - var usubmit = document.getElementById('mw-fr-submit-unaccept');
231 - var legend = document.getElementById('mw-fr-reviewformlegend');
232 - var diffNotice = document.getElementById('mw-fr-difftostable');
233 - var tagBox = document.getElementById('mw-fr-revisiontag');
234 - // On success...
235 - if( response.indexOf('<suc#>') == 0 ) {
236 - // (a) Update document title and form buttons...
237 - document.title = mediaWiki.msg('actioncomplete');
238 - if( asubmit && usubmit ) {
239 - // Revision was flagged
240 - if( asubmit.value == mediaWiki.msg('revreview-submitting') ) {
241 - asubmit.value = mediaWiki.msg('revreview-submit-reviewed'); // done!
242 - asubmit.style.fontWeight = 'bold';
243 - // Unlock and reset *unflag* button
244 - usubmit.value = mediaWiki.msg('revreview-submit-unreview');
245 - usubmit.removeAttribute( 'style' ); // back to normal
246 - usubmit.disabled = '';
247 - // Revision was unflagged
248 - } else if( usubmit.value == mediaWiki.msg('revreview-submitting') ) {
249 - usubmit.value = mediaWiki.msg('revreview-submit-unreviewed'); // done!
250 - usubmit.style.fontWeight = 'bold';
251 - // Unlock and reset *flag* button
252 - asubmit.value = mediaWiki.msg('revreview-submit-review');
253 - asubmit.removeAttribute( 'style' ); // back to normal
254 - asubmit.disabled = '';
 151+ },
 152+
 153+ /*
 154+ * Submit a revision review via AJAX and update form elements.
 155+ *
 156+ * Note: requestArgs build-up from radios/checkboxes
 157+ * based on patch by Daniel Arnold (bug 13744)
 158+ */
 159+ 'submitRevisionReview': function() {
 160+ var form = document.getElementById("mw-fr-reviewform");
 161+ if( !form ) {
 162+ return true; // do normal behavoir (shouldn't happen)
 163+ }
 164+ FlaggedRevsReview.lockReviewForm( form ); // disallow submissions
 165+
 166+ var notes = document.getElementById("wpNotes");
 167+ var reason = document.getElementById("wpReason");
 168+ // Build up arguments array and update submit button text...
 169+ var requestArgs = []; // array of strings of the format <"pname|pval">.
 170+ var inputs = form.getElementsByTagName("input");
 171+ for( var i=0; i < inputs.length; i++) {
 172+ // Different input types may occur depending on tags...
 173+ if( inputs[i].name == "title" || inputs[i].name == "action" ) {
 174+ continue; // No need to send these...
 175+ } else if( inputs[i].type == "submit" ) {
 176+ if( inputs[i].id == this.id ) {
 177+ requestArgs.push( inputs[i].name + "|1" );
 178+ // Show that we are submitting via this button
 179+ inputs[i].value = mediaWiki.msg('revreview-submitting');
 180+ }
 181+ } else if( inputs[i].type == "checkbox" ) {
 182+ requestArgs.push( inputs[i].name + "|" +
 183+ (inputs[i].checked ? inputs[i].value : 0) );
 184+ } else if( inputs[i].type == "radio" ) {
 185+ if( inputs[i].checked ) { // must be checked
 186+ requestArgs.push( inputs[i].name + "|" + inputs[i].value );
 187+ }
 188+ } else {
 189+ requestArgs.push( inputs[i].name + "|" + inputs[i].value ); // text/hiddens...
255190 }
256191 }
257 - // (b) Remove review tag from drafts
258 - if( tagBox ) tagBox.style.display = 'none';
259 - // (c) Update diff-related items...
260 - var diffUIParams = document.getElementById('mw-fr-diff-dataform');
261 - if ( diffUIParams ) {
262 - // Hide "review this" box on diffs
263 - if( diffNotice ) diffNotice.style.display = 'none';
264 - // Update the contents of the mw-fr-diff-headeritems div
265 - wgAjaxReview.inprogress = true;
266 - var args = []; // <oldid, newid>
267 - args.push( diffUIParams.getElementsByTagName('input')[0].value );
268 - args.push( diffUIParams.getElementsByTagName('input')[1].value );
269 - // Send!
270 - var old = sajax_request_type;
271 - sajax_request_type = "GET";
272 - sajax_do_call( "FlaggedArticleView::AjaxBuildDiffHeaderItems",
273 - args, wgAjaxReview.processDiffHeaderItemsResult );
274 - sajax_request_type = old;
 192+ if( notes ) {
 193+ requestArgs.push( notes.name + "|" + notes.value );
275194 }
276 - // On failure...
277 - } else {
278 - // (a) Update document title and form buttons...
279 - document.title = mediaWiki.msg('actionfailed');
280 - if( asubmit && usubmit ) {
281 - // Revision was flagged
282 - if( asubmit.value == mediaWiki.msg('revreview-submitting') ) {
283 - asubmit.value = mediaWiki.msg('revreview-submit-review'); // back to normal
284 - asubmit.disabled = ''; // unlock flag button
285 - // Revision was unflagged
286 - } else if( usubmit.value == mediaWiki.msg('revreview-submitting') ) {
287 - usubmit.value = mediaWiki.msg('revreview-submit-unreview'); // back to normal
288 - usubmit.disabled = ''; // unlock
 195+ var selects = form.getElementsByTagName("select");
 196+ for( var i=0; i < selects.length; i++) {
 197+ // Get the selected tag level...
 198+ if( selects[i].selectedIndex >= 0 ) {
 199+ var soption = selects[i].getElementsByTagName("option")[selects[i].selectedIndex];
 200+ requestArgs.push( selects[i].name + "|" + soption.value );
289201 }
290202 }
291 - // (b) Output any error response message
292 - if ( response.indexOf('<err#>') == 0 ) {
293 - jsMsg( msg, 'review' ); // failure notice
 203+ // Send encoded function plus all arguments...
 204+ post_data = 'action=ajax&rs=RevisionReview::AjaxReview';
 205+ for( var i=0; i<requestArgs.length; i++ ) {
 206+ post_data += '&rsargs[]=' + encodeURIComponent( requestArgs[i] );
 207+ }
 208+ // Send POST request via AJAX!
 209+ var call = jQuery.ajax({
 210+ url : wgScriptPath + '/index.php',
 211+ type : "POST",
 212+ data : post_data,
 213+ dataType: "html", // response type
 214+ success : function( response ) {
 215+ FlaggedRevsReview.updateReviewForm( form, response ); },
 216+ timeout : 5000, // 5 second timeout
 217+ error : function( response ) {
 218+ FlaggedRevsReview.unlockReviewForm( form ); }
 219+ });
 220+ return false; // don't do normal non-AJAX submit
 221+ },
 222+
 223+ 'updateReviewForm': function( form, response ) {
 224+ var msg = response.substr(6); // remove <err#> or <suc#>
 225+ // Read new "last change time" timestamp for conflict handling
 226+ var m = msg.match(/^<lct#(\d*)>(.+)/m);
 227+ if( m ) msg = m[2]; // remove tag from msg
 228+ var changeTime = m ? m[1] : null; // MW TS
 229+ // Some form elements...
 230+ var asubmit = document.getElementById('mw-fr-submit-accept');
 231+ var usubmit = document.getElementById('mw-fr-submit-unaccept');
 232+ var legend = document.getElementById('mw-fr-reviewformlegend');
 233+ var diffNotice = document.getElementById('mw-fr-difftostable');
 234+ var tagBox = document.getElementById('mw-fr-revisiontag');
 235+ // On success...
 236+ if( response.indexOf('<suc#>') == 0 ) {
 237+ // (a) Update document title and form buttons...
 238+ document.title = mediaWiki.msg('actioncomplete');
 239+ if( asubmit && usubmit ) {
 240+ // Revision was flagged
 241+ if( asubmit.value == mediaWiki.msg('revreview-submitting') ) {
 242+ asubmit.value = mediaWiki.msg('revreview-submit-reviewed'); // done!
 243+ asubmit.style.fontWeight = 'bold';
 244+ // Unlock and reset *unflag* button
 245+ usubmit.value = mediaWiki.msg('revreview-submit-unreview');
 246+ usubmit.removeAttribute( 'style' ); // back to normal
 247+ usubmit.disabled = '';
 248+ // Revision was unflagged
 249+ } else if( usubmit.value == mediaWiki.msg('revreview-submitting') ) {
 250+ usubmit.value = mediaWiki.msg('revreview-submit-unreviewed'); // done!
 251+ usubmit.style.fontWeight = 'bold';
 252+ // Unlock and reset *flag* button
 253+ asubmit.value = mediaWiki.msg('revreview-submit-review');
 254+ asubmit.removeAttribute( 'style' ); // back to normal
 255+ asubmit.disabled = '';
 256+ }
 257+ }
 258+ // (b) Remove review tag from drafts
 259+ if( tagBox ) tagBox.style.display = 'none';
 260+ // (c) Update diff-related items...
 261+ var diffUIParams = document.getElementById('mw-fr-diff-dataform');
 262+ if( diffUIParams ) {
 263+ // Hide "review this" box on diffs
 264+ if( diffNotice ) diffNotice.style.display = 'none';
 265+ // Update the contents of the mw-fr-diff-headeritems div
 266+ var requestArgs = []; // <oldid, newid>
 267+ requestArgs.push( diffUIParams.getElementsByTagName('input')[0].value );
 268+ requestArgs.push( diffUIParams.getElementsByTagName('input')[1].value );
 269+ // Send encoded function plus all arguments...
 270+ url_pars = '?action=ajax&rs=FlaggedArticleView::AjaxBuildDiffHeaderItems';
 271+ for( var i=0; i<requestArgs.length; i++ ) {
 272+ url_pars += '&rsargs[]=' + encodeURIComponent(requestArgs[i]);
 273+ }
 274+ // Send GET request via AJAX!
 275+ var call = jQuery.ajax({
 276+ url : wgScriptPath + '/index.php' + url_pars,
 277+ type : "GET",
 278+ dataType: "html", // response type
 279+ success : FlaggedRevsReview.processDiffHeaderItemsResult
 280+ });
 281+ }
 282+ // On failure...
294283 } else {
295 - jsMsg( request.responseText, 'review' ); // fatal notice
 284+ // (a) Update document title and form buttons...
 285+ document.title = mediaWiki.msg('actionfailed');
 286+ if( asubmit && usubmit ) {
 287+ // Revision was flagged
 288+ if( asubmit.value == mediaWiki.msg('revreview-submitting') ) {
 289+ asubmit.value = mediaWiki.msg('revreview-submit-review'); // back to normal
 290+ asubmit.disabled = ''; // unlock flag button
 291+ // Revision was unflagged
 292+ } else if( usubmit.value == mediaWiki.msg('revreview-submitting') ) {
 293+ usubmit.value = mediaWiki.msg('revreview-submit-unreview'); // back to normal
 294+ usubmit.disabled = ''; // unlock
 295+ }
 296+ }
 297+ // (b) Output any error response message
 298+ if( response.indexOf('<err#>') == 0 ) {
 299+ jsMsg( msg, 'review' ); // failure notice
 300+ } else {
 301+ jsMsg( request.responseText, 'review' ); // fatal notice
 302+ }
 303+ window.scroll(0,0); // scroll up to notice
296304 }
297 - window.scroll(0,0); // scroll up to notice
 305+ // Update changetime for conflict handling
 306+ if( changeTime != null ) {
 307+ document.getElementById('mw-fr-input-changetime').value = changeTime;
 308+ }
 309+ FlaggedRevsReview.unlockReviewForm( form );
 310+ },
 311+
 312+ // update the contents of the mw-fr-diff-headeritems div
 313+ 'processDiffHeaderItemsResult': function( response ) {
 314+ var diffHeaderItems = document.getElementById("mw-fr-diff-headeritems");
 315+ if( diffHeaderItems && response != '' ) {
 316+ diffHeaderItems.innerHTML = response;
 317+ }
298318 }
299 - // Update changetime for conflict handling
300 - if ( changeTime != null ) {
301 - document.getElementById('mw-fr-input-changetime').value = changeTime;
302 - }
303 - wgAjaxReview.unlockForm();
304319 };
305320
306 -// update the contents of the mw-fr-diff-headeritems div
307 -wgAjaxReview.processDiffHeaderItemsResult = function(request) {
308 - if( !wgAjaxReview.supported ) {
309 - return;
310 - }
311 - wgAjaxReview.inprogress = false;
312 - var response = request.responseText;
313 - var diffHeaderItems = document.getElementById("mw-fr-diff-headeritems");
314 - if( diffHeaderItems && response != '' ) {
315 - diffHeaderItems.innerHTML = response;
316 - }
317 -};
318 -
319 -wgAjaxReview.onLoad = function() {
320 - var asubmit = document.getElementById("mw-fr-submit-accept");
321 - if( asubmit ) {
322 - asubmit.onclick = wgAjaxReview.ajaxCall;
323 - }
324 - var usubmit = document.getElementById("mw-fr-submit-unaccept");
325 - if( usubmit ) {
326 - usubmit.onclick = wgAjaxReview.ajaxCall;
327 - }
328 -};
329 -
330321 // Perform some onload (which is when this script is included) events:
331322 FlaggedRevsReview.maybeDisableAcceptButton();
332323 FlaggedRevsReview.updateRatingFormColors();
333 -wgAjaxReview.onLoad();
 324+FlaggedRevsReview.enableAjaxReview();

Follow-up revisions

RevisionCommit summaryAuthorDate
r82010* Removed the last mediawiki.legacy dependencies...aaron09:49, 12 February 2011

Status & tagging log