r108552 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108551‎ | r108552 | r108553 >
Date:21:27, 10 January 2012
Author:krinkle
Status:ok
Tags:
Comment:
[MoodBar] More clean up
* Hoist all variables together to the top of each scope, to prevent more/future leakage of globals such as fixed in r108533
-- JavaScript does this at run-time if not already, helps debugging when written the way the language works. One var statement per function, at the top.

* Using $.ajax instead of $.post().error()

* Cache repetitive query in $fbdFiltersCheck, no need to query that again and again (if it is live, then it also be bound live, which it isn't)

* Re-use cached selector through chain for $( '#fbd-list' ).

* Using <a target> _blank instead of _new. "_new" does not exist in any modern browser. "_blank" is a magic word that always forced a new window for the target. "_new" is actually a named window that is not found and usually opens a new window, except when another link with _new is clicked, which will then inconveniently hijack the other window that was opened via a _new link.

* Follows-up r108533
Modified paths:
  • /trunk/extensions/MoodBar/modules/ext.moodBar.dashboard/ext.moodBar.dashboard.js (modified) (history)

Diff [purge]

Index: trunk/extensions/MoodBar/modules/ext.moodBar.dashboard/ext.moodBar.dashboard.js
@@ -2,10 +2,12 @@
33 * AJAX code for Special:MoodBarFeedback
44 */
55 jQuery( document ).ready( function ( $ ) {
 6+ var formState, filterType, $fbdFiltersCheck;
 7+
68 /**
79 * Saved form state
810 */
9 - var formState = {
 11+ formState = {
1012 types: [],
1113 username: '',
1214 myresponse: null,
@@ -149,6 +151,8 @@
150152 url: mw.util.wikiScript( 'api' ),
151153 data: reqData,
152154 success: function ( data ) {
 155+ var comments, len, $ul, moreResults, i;
 156+
153157 // Remove the spinner and restore the "More" link
154158 $( '#fbd-list-more' )
155159 .removeClass( 'mw-ajax-loader' )
@@ -160,11 +164,11 @@
161165 return;
162166 }
163167
164 - var comments = data.query.moodbarcomments,
165 - len = comments.length,
166 - $ul = $( '#fbd-list' ),
167 - moreResults = false,
168 - i;
 168+ comments = data.query.moodbarcomments;
 169+ len = comments.length;
 170+ $ul = $( '#fbd-list' );
 171+ moreResults = false;
 172+
169173 if ( len === 0 ) {
170174 if ( mode === 'more' ) {
171175 showMessage( mw.msg( 'moodbar-feedback-nomore' ) );
@@ -212,6 +216,7 @@
213217 */
214218 function reloadItem( $item, show ) {
215219 var cont, request;
 220+
216221 cont = $item.data( 'mbccontinue' );
217222
218223 request = {
@@ -227,8 +232,10 @@
228233 request.mbcprop = 'formatted|hidden';
229234 }
230235
231 - $.post( mw.util.wikiScript( 'api' ), request,
232 - function ( data ) {
 236+ $.ajax( {
 237+ url: mw.util.wikiScript( 'api' ),
 238+ data: request,
 239+ success: function ( data ) {
233240 if ( data && data.query && data.query.moodbarcomments &&
234241 data.query.moodbarcomments.length > 0
235242 ) {
@@ -240,17 +247,22 @@
241248 $item.find( '.mw-ajax-loader' ).remove();
242249 showItemError( $item );
243250 }
244 - }, 'json' )
245 - .error( function () { showItemError( $item ); } );
 251+ },
 252+ dataType: 'json',
 253+ error: function () {
 254+ showItemError( $item );
 255+ }
 256+ } );
246257 }
247258
248259 /**
249260 * Show a hidden comment
250261 */
251262 function showHiddenItem(e) {
252 - var $item = $(this).closest( '.fbd-item' );
 263+ var $item, $spinner;
253264
254 - var $spinner = $( '<span class="mw-ajax-loader">&nbsp;</span>' );
 265+ $item = $(this).closest( '.fbd-item' );
 266+ $spinner = $( '<span class="mw-ajax-loader">&nbsp;</span>' );
255267 $item.find( '.fbd-item-show' ).empty().append( $spinner );
256268
257269 reloadItem( $item, true );
@@ -276,17 +288,19 @@
277289 * @param $item jQuery item containing the .fbd-item
278290 */
279291 function confirmAction( params, $item ) {
 292+ var inlineForm, storedParams, $storedItem;
280293
281 - var inlineForm = $( '<span>' ).attr( 'class', 'fbd-item-reason' )
282 - .append( $( '<span>' ).text(mw.msg( 'moodbar-action-reason' )) )
283 - .append( $( '<input>' ).attr({'class':'fbd-action-reason', 'name':'fbd-action-reason'}) )
284 - .append( $( '<button>' ).attr( 'class', 'fbd-action-confirm' ).text( mw.msg( 'moodbar-feedback-action-confirm' )) )
285 - .append( $( '<button>' ).attr( 'class', 'fbd-action-cancel' ).text( mw.msg( 'moodbar-feedback-action-cancel' )) )
286 - .append( $( '<span>' ).attr( 'class', 'fbd-item-reason-msg' ) )
 294+ inlineForm = $( '<span>' )
 295+ .attr( 'class', 'fbd-item-reason' )
 296+ .append( $( '<span>' ).text( mw.msg( 'moodbar-action-reason' ) ) )
 297+ .append( $( '<input>' ).attr({'class':'fbd-action-reason', 'name':'fbd-action-reason'} ) )
 298+ .append( $( '<button>' ).attr( 'class', 'fbd-action-confirm' ).text( mw.msg( 'moodbar-feedback-action-confirm' ) ) )
 299+ .append( $( '<button>' ).attr( 'class', 'fbd-action-cancel' ).text( mw.msg( 'moodbar-feedback-action-cancel' ) ) )
 300+ .append( $( '<span>' ).attr( 'class', 'fbd-item-reason-msg' ) )
287301 .append( $( '<div>' ).attr( 'class', 'fbd-item-reason-msg' ) );
288302
289 - var storedParams = params;
290 - var $storedItem = $item;
 303+ storedParams = params;
 304+ $storedItem = $item;
291305
292306 $item.find( '.fbd-item-hide, .fbd-item-restore, .fbd-item-permalink' )
293307 .empty();
@@ -318,23 +332,26 @@
319333 * @param $item jQuery item containing the .fbd-item
320334 */
321335 function doAction( params, $item ) {
322 - var item_id = $item.data( 'mbccontinue' ).split( '|' )[1];
 336+ var item_id, $spinner;
323337
324 - var errorHandler = function ( error_str ) {
 338+ function errorHandler ( error_str ) {
325339 showItemError( $item, error_str );
326 - };
327 -
328 - var $spinner = $( '<span class="mw-ajax-loader">&nbsp;</span>' );
 340+ }
 341+
 342+ item_id = $item.data( 'mbccontinue' ).split( '|' )[1];
 343+ $spinner = $( '<span class="mw-ajax-loader">&nbsp;</span>' );
329344 $item.find( '.fbd-item-hide' ).empty().append( $spinner );
330345
331 - $.post( mw.util.wikiScript( 'api' ),
332 - $.extend( {
 346+ $.ajax( {
 347+ type: 'POST',
 348+ url: mw.util.wikiScript( 'api' ),
 349+ data: $.extend( {
333350 action: 'feedbackdashboard',
334351 token: mw.user.tokens.get( 'editToken' ),
335352 item: item_id,
336353 format: 'json'
337354 }, params ),
338 - function ( response ) {
 355+ success: function ( response ) {
339356 if ( response && response.feedbackdashboard ) {
340357 if ( response.feedbackdashboard.result === 'success' ) {
341358 reloadItem( $item );
@@ -346,8 +363,9 @@
347364 } else {
348365 errorHandler();
349366 }
350 - } )
351 - .error( errorHandler );
 367+ },
 368+ error: errorHandler
 369+ } );
352370 }
353371
354372 /**
@@ -367,6 +385,7 @@
368386 */
369387 function hideItem( e ) {
370388 var $item = $(this).closest( '.fbd-item' );
 389+
371390 closeAllResponders(); // If any are open
372391 confirmAction( { mbaction: 'hide' }, $item );
373392 e.preventDefault();
@@ -379,8 +398,8 @@
380399 function closeAllResponders() {
381400
382401 $( '.fbd-item' ).each( function () {
 402+ var $link = $( this ).find( '.fbd-respond-link' );
383403
384 - var $link = $( this ).find( '.fbd-respond-link' );
385404 if ( $link.hasClass( 'responder-expanded' ) ) {
386405
387406 $link.find( '.fbd-item-response-expanded' )
@@ -403,6 +422,7 @@
404423 * @param e {jQuery.Event}
405424 */
406425 function showResponseForm( e ) {
 426+ var termsLink, ula, inlineForm, $item;
407427
408428 if ( $(this).hasClass( 'responder-expanded' ) ) {
409429
@@ -411,18 +431,18 @@
412432 } else {
413433
414434 // Init terms of use link
415 - var termsLink = mw.html.element ( 'a', {
416 - 'href': mw.msg( 'moodbar-response-url' ),
417 - 'title': mw.msg( 'moodbar-response-link' ),
418 - 'target': '_new'
419 - }, mw.msg( 'moodbar-response-link' ) );
 435+ termsLink = mw.html.element ( 'a', {
 436+ href: mw.msg( 'moodbar-response-url' ),
 437+ title: mw.msg( 'moodbar-response-link' ),
 438+ target: '_blank'
 439+ }, mw.msg( 'moodbar-response-link' ) );
420440
421441 // ULA
422 - var ula = mw.msg( 'moodbar-response-terms' )
 442+ ula = mw.msg( 'moodbar-response-terms' )
423443 .replace ( /\$1/g, termsLink );
424444
425445 // Build form
426 - var inlineForm = $( '<div>' ).attr( 'class', 'fbd-response-form' )
 446+ inlineForm = $( '<div>' ).attr( 'class', 'fbd-response-form' )
427447 .append(
428448 $( '<b>' ).text( mw.msg( 'moodbar-response-add' ) )
429449 ).append(
@@ -462,7 +482,8 @@
463483 );
464484
465485 // Get the feedbackItem
466 - var $item = $(this).closest( '.fbd-item' );
 486+ $item = $(this).closest( '.fbd-item' );
 487+
467488 // Close any open responders prior to opening this one.
468489 closeAllResponders();
469490
@@ -650,51 +671,53 @@
651672
652673 // Handle response submit
653674 $( '.fbd-response-submit' ).live( 'click', function () {
654 - var $item = $(this).parent().parent();
655 - var fbResponse = $item.find( '.fbd-response-text' ).val();
656 - if ( fbResponse ) {
657 - var clientData = $.client.profile(),
658 - item_id = $item.data( 'mbccontinue' ).split( '|' )[1],
659 - resData = {
660 - action: 'feedbackdashboardresponse',
661 - useragent: clientData.name + '/' + clientData.versionNumber,
662 - system: clientData.platform,
663 - token: mw.user.tokens.get( 'editToken' ),
664 - response: fbResponse,
665 - feedback: item_id,
666 - format: 'json'
667 - };
 675+ var $item, fbResponse, clientData, item_id, resData, $responseStatus, $responseForm;
668676
669 - var $responseStatus = $( '<div>' ).attr( 'class', 'fbd-ajax-response' )
670 - .append( $( '<span>' ).attr( 'class','mw-ajax-loader fbd-item-response-icon' ).html( '&nbsp;' ) )
671 - .append( $( '<div>' )
672 - .append( $( '<span>' ).attr( 'class', 'fbd-ajax-heading' ).text( mw.msg( 'response-ajax-action-head' ) ) )
673 - .append( $( '<span>' ).attr( 'class', 'fbd-ajax-text' ).text( mw.msg( 'response-ajax-action-body' ) ) )
674 - );
 677+ $item = $(this).parent().parent();
 678+ fbResponse = $item.find( '.fbd-response-text' ).val();
 679+ if ( fbResponse ) {
 680+ clientData = $.client.profile();
 681+ item_id = $item.data( 'mbccontinue' ).split( '|' )[1];
 682+ resData = {
 683+ action: 'feedbackdashboardresponse',
 684+ useragent: clientData.name + '/' + clientData.versionNumber,
 685+ system: clientData.platform,
 686+ token: mw.user.tokens.get( 'editToken' ),
 687+ response: fbResponse,
 688+ feedback: item_id,
 689+ format: 'json'
 690+ };
675691
676 - var $responseForm = $item.find( '.fbd-response-form' );
677 - $responseForm.empty().append( $responseStatus );
 692+ $responseStatus = $( '<div>' ).attr( 'class', 'fbd-ajax-response' )
 693+ .append( $( '<span>' ).attr( 'class','mw-ajax-loader fbd-item-response-icon' ).html( '&nbsp;' ) )
 694+ .append( $( '<div>' )
 695+ .append( $( '<span>' ).attr( 'class', 'fbd-ajax-heading' ).text( mw.msg( 'response-ajax-action-head' ) ) )
 696+ .append( $( '<span>' ).attr( 'class', 'fbd-ajax-text' ).text( mw.msg( 'response-ajax-action-body' ) ) )
 697+ );
678698
679 - // Send response
680 - $.ajax( {
681 - type: 'POST',
682 - url: mw.util.wikiScript( 'api' ),
683 - data: resData,
684 - success: function (data) {
685 - // If rejected
686 - if ( data.error !== undefined ) {
687 - responseMessage( $item, 'error', mw.msg( 'response-ajax-error-head' ), data.error.info );
688 - } else if ( data.feedbackdashboardresponse !== undefined ) {
689 - responseMessage( $item, 'success', mw.msg( 'response-ajax-success-head' ), mw.msg( 'response-ajax-success-body' ) );
690 - }
691 - },
692 - error: function ( jqXHR, textStatus, errorThrown ) {
693 - responseMessage( $item, 'error', mw.msg( 'response-ajax-error-head' ), mw.msg( 'response-ajax-error-body' ) );
694 - },
695 - dataType: 'json'
696 - } );
 699+ $responseForm = $item.find( '.fbd-response-form' );
 700+ $responseForm.empty().append( $responseStatus );
697701
698 - }
 702+ // Send response
 703+ $.ajax( {
 704+ type: 'POST',
 705+ url: mw.util.wikiScript( 'api' ),
 706+ data: resData,
 707+ success: function (data) {
 708+ // If rejected
 709+ if ( data.error !== undefined ) {
 710+ responseMessage( $item, 'error', mw.msg( 'response-ajax-error-head' ), data.error.info );
 711+ } else if ( data.feedbackdashboardresponse !== undefined ) {
 712+ responseMessage( $item, 'success', mw.msg( 'response-ajax-success-head' ), mw.msg( 'response-ajax-success-body' ) );
 713+ }
 714+ },
 715+ error: function ( jqXHR, textStatus, errorThrown ) {
 716+ responseMessage( $item, 'error', mw.msg( 'response-ajax-error-head' ), mw.msg( 'response-ajax-error-body' ) );
 717+ },
 718+ dataType: 'json'
 719+ } );
 720+
 721+ }
699722 });
700723
701724 $( '#fbd-filters' ).children( 'form' ).submit( function ( e ) {
@@ -712,31 +735,32 @@
713736 loadComments( 'more' );
714737 } );
715738
716 - $( '#fbd-filters-types input[type=checkbox]' ).click( function () {
 739+ $( '#fbd-filters-types input[type="checkbox"]' ).click( function () {
717740 var types = getSelectedTypes();
718741 if ( types.length === 0 ) { //check for 0 because onclick it will already have unchecked itself.
719742 $(this).prop( 'checked', true);
720743 }
721744 });
722745
723 - //only allow one of the secondary filters to be checked
724 - $( 'input[type=checkbox].fbd-filters-check' ).click(function () {
725 - $( 'input[type=checkbox].fbd-filters-check' ).not( this ).prop( 'checked', false );
 746+ // Only allow one of the secondary filters to be checked
 747+ $fbdFiltersCheck = $( 'input.fbd-filters-check[type="checkbox"]' ).click(function () {
 748+ $fbdFiltersCheck.not( this ).prop( 'checked', false );
726749 });
727750
728 - $( '#fbd-list' ).delegate( '.fbd-item', 'hover', function () {
729 - $(this).toggleClass( 'fbd-hover' );
730 - });
 751+ $( '#fbd-list' )
 752+ .delegate( '.fbd-item', 'hover', function () {
 753+ $(this).toggleClass( 'fbd-hover' );
 754+ })
 755+ .delegate( '.fbd-item', 'mouseleave', function () {
 756+ $(this).removeClass( 'fbd-hover' );
 757+ });
731758
732 - $( '#fbd-list' ).delegate( '.fbd-item', 'mouseleave', function () {
733 - $(this).removeClass( 'fbd-hover' );
734 - });
735 -
736759 //zebra stripe leaderboard
737760 $( '.fbd-leaderboard li:nth-child(even)' ).addClass( 'even' );
738761
739762 saveFormState();
740 - var filterType = $( '#fbd-filters' ).children( 'form' ).data( 'filtertype' );
 763+
 764+ filterType = $( '#fbd-filters' ).children( 'form' ).data( 'filtertype' );
741765 // If filtering already happened on the PHP side, don't load the form state from cookies
742766 if ( filterType !== 'filtered' ) {
743767 // Don't do an AJAX filter if we're on an ID view, or if the form is still blank after loadFromCookies()

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r108533[MoodBar] Clean up + minor bug fix...krinkle19:14, 10 January 2012

Status & tagging log