Merge "Minor documentation tweaks"
[lhc/web/wiklou.git] / resources / mediawiki.action / mediawiki.action.edit.js
index 497a424..14b845d 100644 (file)
-(function( $ ) {
-       // currentFocus is used to determine where to insert tags
-       var currentFocused = $( '#wpTextbox1' );
-
-       mw.toolbar = {
-               $toolbar : $( '#toolbar' ),
-               buttons : [],
-               // If you want to add buttons, use
-               // mw.toolbar.addButton( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText );
-               addButton : function() {
-                       this.buttons.push( [].slice.call( 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 {
+                               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
+                                       }
                                );
                        }
                },
-               init : function() {
-                       // 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
-                                       mw.toolbar.insertButton.apply( this, buttons[i] );
-                               } else {
-                                       // Legacy mwCustomEditButtons is an object
-                                       var c = buttons[i];
-                                       mw.toolbar.insertButton( c.imageFile, c.speedTip, c.tagOpen, c.tagClose, c.sampleText, c.imageId, c.selectText );
-                               }
+
+               // For backwards compatibility
+               init: function () {}
+       };
+
+       // Legacy (for compatibility with the code previously in skins/common.edit.js)
+       window.addButton = toolbar.addButton;
+       window.insertTags = toolbar.insertTags;
+
+       // 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 );
                        }
-                       return true;
                }
-       };
 
-       //Legacy
-       window.addButton =  mw.toolbar.addButton;
-       window.insertTags = mw.toolbar.insertTags;
+               // 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 );
+               // Make sure edit summary does not exceed byte limit
+               $( '#wpSummary' ).byteLimit( 255 );
 
-       $( document ).ready( function() {
                /**
                 * 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();
+               }() );
 
-               // Create button bar
-               mw.toolbar.init();
-
-               $( '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 ) );