http://www.mediawiki.org/wiki/Special:Code/MediaWiki/98096

Revision: 98096
Author:   krinkle
Date:     2011-09-25 23:16:19 +0000 (Sun, 25 Sep 2011)
Log Message:
-----------
[flaggedrevs.js]: Code cleanup
* Apply whitespace conventions
* Apply best practices and other code conventions (strict comparison, combined 
var statements, local function closure)
* Alias jQuery to $ locally
* Use $.fn.prop() instead of $.fn.attr() to access the 'checked' property. 
$.fn.attr() still supports this for backwards compatibility (it reroutes to 
$.fn.prop if keys like 'checked' are given), but it should not be used in new 
code. 'title' is an attribute however.
* Add function documentation where missing. Making it obvious that the context 
("this") of the functions called from the mouseout/click handlers is the jQuery 
object. Also emphasizing that the 'e' parameter passed is an instance of 
jQuery.Event, which is a normalized version of the native object thrown by the 
browser.
* Remove cross-browser compatibility check of e.relatedTarget as jQuery.Event 
normalizes this and sets e.relatedTarget to e.toElement in IE.
* Remove bogus comment "//event bubbling" in isMouseOutBubble. Although it is 
called from an event handler, it is not the event handler itself. Returning 
true or false from isMouseOutBubble has no influence on the event handler. The 
function is called from an if-statement in onBoxMouseOut().
* Make use of prototype chaining instead of re-referencing the jQuery object 
where possible (ie. "save.val( .. ); save.attr( .. );" -> "save.val( .. ).attr( 
.. );")
* Make a local variable definition and expose it globally later instead of 
declaring it into window and referring to it without "window." which makes 
refers to an implied global instead of to the global directly. Another way to 
solve that is to use "window.FlaggedRevs" everywhere, but choose to use a local 
variable instead to make it shorter.
--
(This revision follows-up r98078)

Modified Paths:
--------------
    trunk/extensions/FlaggedRevs/presentation/modules/flaggedrevs.js

Modified: trunk/extensions/FlaggedRevs/presentation/modules/flaggedrevs.js
===================================================================
--- trunk/extensions/FlaggedRevs/presentation/modules/flaggedrevs.js    
2011-09-25 23:07:34 UTC (rev 98095)
+++ trunk/extensions/FlaggedRevs/presentation/modules/flaggedrevs.js    
2011-09-25 23:16:19 UTC (rev 98096)
@@ -3,164 +3,210 @@
  * @author Aaron Schulz
  * @author Krinkle <[email protected]> 2011
  */
+( function( $ ) {
 
-window.FlaggedRevs = {
+var fr = {
        /* Dropdown collapse timer */
        'boxCollapseTimer': null,
 
        /* Startup function */
        'init': function() {
                // Enables rating detail box
-               var toggle = $('#mw-fr-revisiontoggle');
+               var toggle = $( '#mw-fr-revisiontoggle' );
+
                if ( toggle.length ) {
-                       toggle.css('display','inline'); /* show toggle control 
*/
-                       FlaggedRevs.hideBoxDetails(); /* hide the initially 
displayed ratings */
+                       toggle.css( 'display', 'inline' ); // show toggle 
control
+                       fr.hideBoxDetails(); // hide the initially displayed 
ratings
                }
+
                // Bar UI: Toggle the box when the toggle is clicked
-               $('.fr-toggle-symbol#mw-fr-revisiontoggle').click( 
FlaggedRevs.toggleBoxDetails );
+               $( '.fr-toggle-symbol#mw-fr-revisiontoggle' ).click( 
fr.toggleBoxDetails );
+
                // Simple UI: Show the box on mouseOver
-               $('.fr-toggle-arrow#mw-fr-revisiontoggle').mouseover( 
FlaggedRevs.onBoxMouseOver );
-               $('.flaggedrevs_short#mw-fr-revisiontag').mouseout( 
FlaggedRevs.onBoxMouseOut );
-               
+               $( '.fr-toggle-arrow#mw-fr-revisiontoggle' ).mouseover( 
fr.onBoxMouseOver );
+               $( '.flaggedrevs_short#mw-fr-revisiontag' ).mouseout( 
fr.onBoxMouseOut );
+
                // Enables diff detail box and toggle
-               toggle = $('#mw-fr-difftoggle');
+               toggle = $( '#mw-fr-difftoggle' );
                if ( toggle.length ) {
-                       toggle.css('display','inline'); /* show toggle control 
*/
-                       $('#mw-fr-stablediff').hide();
+                       toggle.css( 'display', 'inline' ); // show toggle 
control
+                       $( '#mw-fr-stablediff' ).hide();
                }
-               toggle.children('a').click( FlaggedRevs.toggleDiff );
-               
+               toggle.children( 'a' ).click( fr.toggleDiff );
+
                // Enables log detail box and toggle
-               toggle = $('#mw-fr-logtoggle');
+               toggle = $( '#mw-fr-logtoggle' );
                if ( toggle.length ) {
-                       toggle.css('display','inline'); /* show toggle control 
*/
-                       $('#mw-fr-logexcerpt').hide();
+                       toggle.css( 'display', 'inline' ); // show toggle 
control
+                       $( '#mw-fr-logexcerpt' ).hide();
                }
-               toggle.children('a').click( FlaggedRevs.toggleLog );
-               
+               toggle.children( 'a' ).click( fr.toggleLog );
+
                // Enables changing of save button when "review this" checkbox 
changes
-               $('#wpReviewEdit').click( FlaggedRevs.updateSaveButton );
+               $( '#wpReviewEdit' ).click( fr.updateSaveButton );
        },
 
        /* Expands flag info box details */
        'showBoxDetails': function() {
-               $('#mw-fr-revisiondetails').css('display','block');
+               $( '#mw-fr-revisiondetails' ).css( 'display', 'block' );
        },
 
        /* Collapses flag info box details */
-       'hideBoxDetails': function( event ) {
-               $('#mw-fr-revisiondetails').css('display','none');
+       'hideBoxDetails': function() {
+               $( '#mw-fr-revisiondetails' ).css( 'display', 'none' );
        },
 
-       /* Toggles flag info box details for (+/-) control */
-       'toggleBoxDetails': function() {
-               var toggle = $('#mw-fr-revisiontoggle');
-               var ratings = $('#mw-fr-revisiondetails');
+       /**
+        * Toggles flag info box details for (+/-) control
+        * @context {jQuery}
+        * @param e {jQuery.Event}
+        */
+       'toggleBoxDetails': function( e ) {
+               var     toggle = $( '#mw-fr-revisiontoggle' ),
+                       ratings = $( '#mw-fr-revisiondetails' );
+
                if ( toggle.length && ratings.length ) {
                        // Collapsed -> expand
-                       if ( ratings.css('display') == 'none' ) {
-                               FlaggedRevs.showBoxDetails();
-                               toggle.text( mw.msg('revreview-toggle-hide') );
+                       if ( ratings.css( 'display' ) === 'none' ) {
+                               fr.showBoxDetails();
+                               toggle.text( mw.msg( 'revreview-toggle-hide' ) 
);
                        // Expanded -> collapse
                        } else {
-                               FlaggedRevs.hideBoxDetails();
-                               toggle.text( mw.msg('revreview-toggle-show') );
+                               fr.hideBoxDetails();
+                               toggle.text( mw.msg( 'revreview-toggle-show' ) 
);
                        }
                }
        },
 
-       /* Expands flag info box details on mouseOver */
-       'onBoxMouseOver': function( event ) {
-               window.clearTimeout( FlaggedRevs.boxCollapseTimer );
-               FlaggedRevs.boxCollapseTimer = null;
-               FlaggedRevs.showBoxDetails();
+       /**
+        * Expands flag info box details on mouseOver
+        * @context {jQuery}
+        * @param e {jQuery.Event}
+        */
+       'onBoxMouseOver': function( e ) {
+               window.clearTimeout( fr.boxCollapseTimer );
+               fr.boxCollapseTimer = null;
+               fr.showBoxDetails();
        },
 
-       /* Hides flag info box details on mouseOut *except* for event bubbling 
*/
-       'onBoxMouseOut': function( event ) {
-               if ( !FlaggedRevs.isMouseOutBubble( event, 'mw-fr-revisiontag' 
) ) {
-                       FlaggedRevs.boxCollapseTimer = window.setTimeout( 
FlaggedRevs.hideBoxDetails, 150 );
+       /**
+        * Hides flag info box details on mouseOut *except* for event bubbling
+        * @context {jQuery}
+        * @param e {jQuery.Event}
+        */
+       'onBoxMouseOut': function( e ) {
+               if ( !fr.isMouseOutBubble( e, 'mw-fr-revisiontag' ) ) {
+                       fr.boxCollapseTimer = window.setTimeout( 
fr.hideBoxDetails, 150 );
                }
        },
 
-       /* Checks if mouseOut event is for a child of parentId */
-       'isMouseOutBubble': function( event, parentId ) {
-               var toNode = null;
-               if ( event.relatedTarget !== undefined ) {
-                       toNode = event.relatedTarget; // FF/Opera/Safari
-               } else {
-                       toNode = event.toElement; // IE
-               }
+       /**
+        * Checks if mouseOut event is for a child of parentId
+        * @param e {jQuery.Event}
+        * @param parentId {String}
+        * @return {Boolean} True if given event object originated from a 
(direct or indirect)
+        * child element of an element with an id of parentId.
+        */
+       'isMouseOutBubble': function( e, parentId ) {
+               var toNode = e.relatedTarget;
+
                if ( toNode ) {
                        var nextParent = toNode.parentNode;
                        while ( nextParent ) {
-                               if ( nextParent.id == parentId ) {
-                                       return true; // event bubbling
+                               if ( nextParent.id === parentId ) {
+                                       return true;
                                }
-                               nextParent = nextParent.parentNode; // next up
+                               // next up
+                               nextParent = nextParent.parentNode;
                        }
                }
                return false;
        },
 
-       /* Toggles diffs */
-       'toggleDiff': function() {
-               var diff = $('#mw-fr-stablediff');
-               var toggle = $('#mw-fr-difftoggle');
+       /**
+        * Toggles diffs
+        * @context {jQuery}
+        * @param e {jQuery.Event}
+        */
+       'toggleDiff': function( e ) {
+               var     diff = $( '#mw-fr-stablediff' ),
+                       toggle = $( '#mw-fr-difftoggle' );
+
                if ( diff.length && toggle.length ) {
-                       if ( diff.css('display') == 'none' ) {
+                       if ( diff.css( 'display' ) === 'none' ) {
                                diff.show( 'slow' );
-                               toggle.children('a').text( 
mw.msg('revreview-diff-toggle-hide') );
+                               toggle.children( 'a' ).text( mw.msg( 
'revreview-diff-toggle-hide' ) );
                        } else {
                                diff.hide( 'slow' );
-                               toggle.children('a').text( 
mw.msg('revreview-diff-toggle-show') );
+                               toggle.children( 'a' ).text( mw.msg( 
'revreview-diff-toggle-show' ) );
                        }
                }
        },
 
-       /* Toggles log excerpts */
-       'toggleLog': function() {
-               var log = $('#mw-fr-logexcerpt');
-               var toggle = $('#mw-fr-logtoggle');
+       /**
+        * Toggles log excerpts
+        * @context {jQuery}
+        * @param e {jQuery.Event}
+        */
+       'toggleLog': function( e ) {
+               var     hideMsg, showMsg,
+                       log = $( '#mw-fr-logexcerpt' ),
+                       toggle = $( '#mw-fr-logtoggle' );
+
                if ( log.length && toggle.length ) {
                        // Two different message sets used here...
-                       if ( toggle.hasClass('fr-logtoggle-details') ) {
-                               var hideMsg = 
mw.msg('revreview-log-details-hide');
-                               var showMsg = 
mw.msg('revreview-log-details-show');
+                       if ( toggle.hasClass( 'fr-logtoggle-details' ) ) {
+                               hideMsg = mw.msg( 'revreview-log-details-hide' 
);
+                               showMsg = mw.msg( 'revreview-log-details-show' 
);
                        } else {
-                               var hideMsg = 
mw.msg('revreview-log-toggle-hide');
-                               var showMsg = 
mw.msg('revreview-log-toggle-show');
+                               hideMsg = mw.msg( 'revreview-log-toggle-hide' );
+                               showMsg = mw.msg( 'revreview-log-toggle-show' );
                        }
-                       if ( log.css('display') == 'none' ) {
+
+                       if ( log.css( 'display' ) === 'none' ) {
                                log.show();
-                               toggle.children('a').text( hideMsg );
+                               toggle.children( 'a' ).text( hideMsg );
                        } else {
                                log.hide();
-                               toggle.children('a').text( showMsg );
+                               toggle.children( 'a' ).text( showMsg );
                        }
                }
        },
 
-       /* Update save button when "review this" checkbox changes */
+       /**
+        * Update save button when "review this" checkbox changes
+        * @context {jQuery}
+        * @param e {jQuery.Event}
+        */
        'updateSaveButton': function() {
-               var save = $('#wpSave');
-               var checkbox = $('#wpReviewEdit');
-               if ( save.length && checkbox.length ) {
+               var     $save = $( '#wpSave' ),
+                       $checkbox = $( '#wpReviewEdit' );
+
+               if ( $save.length && $checkbox.length ) {
                        // Review pending changes
-                       if ( checkbox.attr('checked') ) {
-                               save.val( mw.msg('savearticle') );
-                               save.attr( 'title',
-                                       mw.msg('tooltip-save') + ' [' + 
mw.msg('accesskey-save') + ']' );
+                       if ( $checkbox.prop( 'checked' ) ) {
+                               $save
+                                       .val( mw.msg( 'savearticle' ) )
+                                       .attr( 'title',
+                                               mw.msg( 'tooltip-save' ) + ' [' 
+ mw.msg( 'accesskey-save' ) + ']'
+                                       );
                        // Submit for review
                        } else {
-                               save.val( mw.msg('revreview-submitedit') );
-                               save.attr( 'title',
-                                       mw.msg('revreview-submitedit-title') + 
' [' + mw.msg('accesskey-save') + ']' );
+                               $save
+                                       .val( mw.msg( 'revreview-submitedit' ) )
+                                       .attr( 'title',
+                                               mw.msg( 
'revreview-submitedit-title' ) + ' [' + mw.msg( 'accesskey-save' ) + ']'
+                                       );
                        }
                }
-               mw.util.updateTooltipAccessKeys( [ save ] ); // update 
accesskey in save.title
+               mw.util.updateTooltipAccessKeys( [ $save ] ); // update 
accesskey in save.title
        }
 };
 
 // Perform some onload (which is when this script is included) events:
-FlaggedRevs.init();
+fr.init();
+
+// Expose globally
+window.FlaggedRevs = fr;
+
+})( jQuery );
\ No newline at end of file


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to