X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=resources%2Fmediawiki.action%2Fmediawiki.action.edit.js;h=ba711aae0cd6928718c5eddeb7401d02ddf70cca;hb=c25e767d6924cd7fbc58ab7b4cb66b8835bd5600;hp=1c5a018e6cef9bfb22f38db7cfbd6e7c436fa1a8;hpb=5cfec935722d8f4015dc24cda1de8bfc7f330537;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/mediawiki.action/mediawiki.action.edit.js b/resources/mediawiki.action/mediawiki.action.edit.js index 1c5a018e6c..ba711aae0c 100644 --- a/resources/mediawiki.action/mediawiki.action.edit.js +++ b/resources/mediawiki.action/mediawiki.action.edit.js @@ -5,7 +5,7 @@ * @singleton */ ( function ( mw, $ ) { - var toolbar, isReady, $toolbar, queue, slice, currentFocused; + var toolbar, isReady, $toolbar, queue, slice, $currentFocused; /** * Internal helper that does the actual insertion of the button into the toolbar. @@ -44,6 +44,12 @@ isReady = false; $toolbar = false; + /** + * @private + * @property {Array} + * Contains button objects (and for backwards compatibilty, it can + * also contains an arguments array for insertButton). + */ queue = []; slice = queue.slice; @@ -75,6 +81,27 @@ queue.push( slice.call( arguments ) ); } }, + /** + * Example usage: + * addButtons( [ { .. }, { .. }, { .. } ] ); + * addButtons( { .. }, { .. } ); + * + * @param {Object|Array} [buttons...] An array of button objects or the first + * button object in a list of variadic arguments. + */ + addButtons: function ( buttons ) { + if ( !$.isArray( buttons ) ) { + buttons = slice.call( arguments ); + } + if ( isReady ) { + $.each( buttons, function () { + insertButton( this ); + } ); + } else { + // Push each button into the queue + queue.push.apply( queue, buttons ); + } + }, /** * Apply tagOpen/tagClose to selection in currently focused textarea. @@ -86,12 +113,12 @@ * @param {string} sampleText */ insertTags: function ( tagOpen, tagClose, sampleText ) { - if ( currentFocused && currentFocused.length ) { - currentFocused.textSelection( + if ( $currentFocused && $currentFocused.length ) { + $currentFocused.textSelection( 'encapsulateSelection', { - 'pre': tagOpen, - 'peri': sampleText, - 'post': tagClose + pre: tagOpen, + peri: sampleText, + post: tagClose } ); } @@ -109,34 +136,33 @@ // Explose API publicly mw.toolbar = toolbar; - $( document ).ready( function () { - var buttons, i, b, $iframe, editBox, scrollTop, $editForm; + $( function () { + var i, b, $iframe, editBox, scrollTop, $editForm; // currentFocus is used to determine where to insert tags - currentFocused = $( '#wpTextbox1' ); + $currentFocused = $( '#wpTextbox1' ); // Populate the selector cache for $toolbar $toolbar = $( '#toolbar' ); - // Legacy: Merge buttons from mwCustomEditButtons - buttons = [].concat( queue, window.mwCustomEditButtons ); - // Clear queue - queue.length = 0; - for ( i = 0; i < buttons.length; i++ ) { - b = buttons[i]; + for ( i = 0; i < queue.length; i++ ) { + b = queue[i]; if ( $.isArray( b ) ) { // Forwarded arguments array from mw.toolbar.addButton insertButton.apply( toolbar, b ); } else { - // Raw object from legacy mwCustomEditButtons + // Raw object from mw.toolbar.addButtons insertButton( b ); } } + // Clear queue + queue.length = 0; + // This causes further calls to addButton to go to insertion directly - // instead of to the toolbar.buttons queue. + // instead of to the queue. // It is important that this is after the one and only loop through - // the the toolbar.buttons queue + // the the queue isReady = true; // Make sure edit summary does not exceed byte limit @@ -158,10 +184,10 @@ // Apply to dynamically created textboxes as well as normal ones $( document ).on( 'focus', 'textarea, input:text', function () { - currentFocused = $( this ); + $currentFocused = $( this ); } ); - // HACK: make currentFocused work with the usability iframe + // HACK: make $currentFocused work with the usability iframe // With proper focus detection support (HTML 5!) this'll be much cleaner // TODO: Get rid of this WikiEditor code from MediaWiki core! $iframe = $( '.wikiEditor-ui-text iframe' ); @@ -170,7 +196,7 @@ // for IE .add( $iframe.get( 0 ).contentWindow.document.body ) .focus( function () { - currentFocused = $iframe; + $currentFocused = $iframe; } ); } });