[mediawiki.action.edit] Clean up and bug fixes
authorKrinkle <krinkle@users.mediawiki.org>
Tue, 28 Feb 2012 03:17:05 +0000 (03:17 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Tue, 28 Feb 2012 03:17:05 +0000 (03:17 +0000)
* Bugfix: Locally alias the toolbar object and replace usage of 'this' with 'toolbar'. Calls to window.addButton were failing due to it referring to the dynamic 'this' context which changes when the function is a member of a different object.
* Bugfux: Move currentFocused = $( '#wpTextbox1' ) into the dom-ready hook. When executed before dom-ready the element doesn't exist yet. r111983 did this for $toolbar, but not for currentFocused (bug 34538)

* Move onReady and isReady to the local scope (introduced recently in r111983, not used or meant to be used publicly)
* Merge onReady with the $(document).ready hook function, same purpose
* JS conventions (closure arguments, var hoisting, whitespace)

* Touch r111983, r112451, r112567

resources/mediawiki.action/mediawiki.action.edit.js

index 6b10485..e685ca9 100644 (file)
@@ -1,78 +1,95 @@
-(function( $ ) {
-       // currentFocus is used to determine where to insert tags
-       var currentFocused = $( '#wpTextbox1' );
-
-       mw.toolbar = {
-               $toolbar : false,
-               buttons : [],
-               isReady : false,
-               // If you want to add buttons, use
-               // mw.toolbar.addButton( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText );
-               addButton : function() {
-                       if ( this.isReady ) {
-                               this.insertButton.apply( this, arguments );
+( function ( $, mw ) {
+       var isReady, toolbar, currentFocused;
+
+       isReady = false;
+
+       toolbar = {
+               $toolbar: false,
+               buttons: [],
+               /**
+                * If you want to add buttons, use
+                * mw.toolbar.addButton( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText );
+                */
+               addButton: function () {
+                       if ( isReady ) {
+                               toolbar.insertButton.apply( toolbar, arguments );
                        } else {
-                               this.buttons.push( [].slice.call( arguments ) );
+                               toolbar.buttons.push( [].slice.call( arguments ) );
                        }       
                },
-               insertButton : function( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText ) {
+               insertButton: function ( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText ) {
                        var image = $('<img>', {
-                               width  : 23,
-                               height : 22,
-                               src    : imageFile,
-                               alt    : speedTip,
-                               title  : speedTip,
-                               id     : imageId || '',
+                               width : 23,
+                               height: 22,
+                               src   : imageFile,
+                               alt   : speedTip,
+                               title : speedTip,
+                               id    : imageId || '',
                                'class': 'mw-toolbar-editbutton'
-                       } ).click( function() {
+                       } ).click( function () {
                                mw.toolbar.insertTags( tagOpen, tagClose, sampleText, selectText );
                                return false;
                        } );
 
-                       this.$toolbar.append( image );
+                       toolbar.$toolbar.append( image );
                        return true;
                },
 
-               // apply tagOpen/tagClose to selection in textarea,
-               // use sampleText instead of selection if there is none
-               insertTags : function( tagOpen, tagClose, sampleText, selectText) {
-                       if ( currentFocused.length ) {
+               /**
+                * apply tagOpen/tagClose to selection in textarea,
+                * use sampleText instead of selection if there is none.
+                */
+               insertTags: function ( tagOpen, tagClose, sampleText, selectText ) {
+                       if ( currentFocused && currentFocused.length ) {
                                currentFocused.textSelection(
-                                       'encapsulateSelection', { 'pre': tagOpen, 'peri': sampleText, 'post': tagClose }
+                                       'encapsulateSelection', {
+                                               'pre': tagOpen,
+                                               'peri': sampleText,
+                                               'post': tagClose
+                                       }
                                );
                        }
                },
 
                // For backwards compatibility
-               init : function() {},
-
-               onReady : function() {
-                       this.$toolbar = $( '#toolbar' );
-                       this.isReady = true;
-                       // Legacy
-                       // Merge buttons from mwCustomEditButtons
-                       var buttons = [].concat( this.buttons, window.mwCustomEditButtons );
-                       for ( var i = 0; i < buttons.length; i++ ) {
-                               if ( $.isArray( buttons[i] ) ) {
-                                       // Passes our button array as arguments
-                                       this.insertButton.apply( this, buttons[i] );
-                               } else {
-                                       // Legacy mwCustomEditButtons is an object
-                                       var c = buttons[i];
-                                       this.insertButton( c.imageFile, c.speedTip, c.tagOpen, 
-                                               c.tagClose, c.sampleText, c.imageId, c.selectText );
-                               }
-                       }
-                       return true;
-               }
+               init: function () {}
        };
 
-       //Legacy
-       window.addButton =  mw.toolbar.addButton;
-       window.insertTags = mw.toolbar.insertTags;
+       // Legacy (for compatibility with the code previously in skins/common.edit.js)
+       window.addButton = toolbar.addButton;
+       window.insertTags = toolbar.insertTags;
 
-       $( document ).ready( function() {
-               mw.toolbar.onReady();
+       // Explose publicly
+       mw.toolbar = toolbar;
+
+       $( document ).ready( function () {
+               var buttons, i, c, iframe;
+
+               // currentFocus is used to determine where to insert tags
+               currentFocused = $( '#wpTextbox1' );
+
+               // Populate the selector cache for $toolbar 
+               toolbar.$toolbar = $( '#toolbar' );
+
+               // Legacy: Merge buttons from mwCustomEditButtons
+               buttons = [].concat( toolbar.buttons, window.mwCustomEditButtons );
+               for ( i = 0; i < buttons.length; i++ ) {
+                       if ( $.isArray( buttons[i] ) ) {
+                               // Passes our button array as arguments
+                               toolbar.insertButton.apply( toolbar, buttons[i] );
+                       } else {
+                               // Legacy mwCustomEditButtons is an object
+                               c = buttons[i];
+                               toolbar.insertButton( c.imageFile, c.speedTip, c.tagOpen, 
+                                       c.tagClose, c.sampleText, c.imageId, c.selectText );
+                       }
+               }
+
+               // This causes further calls to addButton to go to insertion directly
+               // instead of to the toolbar.buttons queue.
+               // It is important that this is after the one and only loop through
+               // the the toolbar.buttons queue
+               isReady = true;
 
                // Make sure edit summary does not exceed byte limit
                $( '#wpSummary' ).byteLimit( 250 );
                 * Restore the edit box scroll state following a preview operation,
                 * and set up a form submission handler to remember this state
                 */
-               var scrollEditBox = function() {
-                       var editBox = document.getElementById( 'wpTextbox1' );
-                       var scrollTop = document.getElementById( 'wpScrolltop' );
-                       var $editForm = $( '#editform' );
-                       if( $editForm.length && editBox && scrollTop ) {
-                               if( scrollTop.value ) {
+               ( function scrollEditBox() {
+                       var editBox, scrollTop, $editForm;
+
+                       editBox = document.getElementById( 'wpTextbox1' );
+                       scrollTop = document.getElementById( 'wpScrolltop' );
+                       $editForm = $( '#editform' );
+                       if ( $editForm.length && editBox && scrollTop ) {
+                               if ( scrollTop.value ) {
                                        editBox.scrollTop = scrollTop.value;
                                }
-                               $editForm.submit( function() {
+                               $editForm.submit( function () {
                                        scrollTop.value = editBox.scrollTop;
                                });
                        }
-               };
-               scrollEditBox();
+               }() );
 
-               $( 'textarea, input:text' ).focus( function() {
+               $( 'textarea, input:text' ).focus( function () {
                        currentFocused = $(this);
                });
 
                // HACK: make currentFocused work with the usability iframe
                // With proper focus detection support (HTML 5!) this'll be much cleaner
-               var iframe = $( '.wikiEditor-ui-text iframe' );
+               iframe = $( '.wikiEditor-ui-text iframe' );
                if ( iframe.length > 0 ) {
                        $( iframe.get( 0 ).contentWindow.document )
-                               .add( iframe.get( 0 ).contentWindow.document.body ) // for IE
-                               .focus( function() { currentFocused = iframe; } );
+                               // for IE
+                               .add( iframe.get( 0 ).contentWindow.document.body )
+                               .focus( function () {
+                                       currentFocused = iframe;
+                               } );
                }
        });
-})(jQuery);
+
+}( jQuery, mediaWiki ) );