From b92260c614f99c74a23809131d171b209ab07f85 Mon Sep 17 00:00:00 2001 From: "helder.wiki" Date: Wed, 26 Sep 2012 11:52:57 -0300 Subject: [PATCH] Implement mw.toolbar.addButtons Example: ``` var skinPath; if ( mw.toolbar ) { skinPath = mw.config.get( 'stylepath' ); mw.toolbar.addButtons( [ { imageFile: skinPath + '/common/images/button_bold.png', speedTip: 'add foo tag', tagOpen: '', tagClose: '', sampleText: 'foo here', imageId: 'foo-id' }, { // .. } ] ); mw.toolbar.addButtons( { imageFile: skinPath + '/common/images/button_bold.png', speedTip: 'add foo tag', tagOpen: '', tagClose: '', sampleText: 'foo here', imageId: 'foo-id' }, { // .. } ); mw.toolbar.addButton( { imageFile: skinPath + '/common/images/button_link.png', speedTip: 'add baz tag', tagOpen: '', tagClose: '', sampleText: 'baz here', imageId: 'baz-id' } ); mw.toolbar.addButton( skinPath + '/common/images/button_link.png', 'add baz2 tag', '', '', 'baz2 here', 'baz2-id' ); } ``` Clean up: * Rename currentFocused to $currentFocused (we are using jQuery); * Add missing dependency 'mediawiki.legacy.wikibits', since this module uses the array window.mwCustomEditButtons. Bug: 40518 Change-Id: I76d1f753cb9e1c119d9b5e7f565acd684f5516d1 --- RELEASE-NOTES-1.22 | 2 + resources/Resources.php | 1 + .../mediawiki.action/mediawiki.action.edit.js | 54 ++++++++++++++----- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/RELEASE-NOTES-1.22 b/RELEASE-NOTES-1.22 index ca653efa6b..09321555ec 100644 --- a/RELEASE-NOTES-1.22 +++ b/RELEASE-NOTES-1.22 @@ -115,6 +115,8 @@ production. * (bug 46513) Vector: Add the collapsibleTabs script from the Vector extension. * Added $wgRecentChangesFlags for defining new flags for RecentChanges and watchlists. +* (bug 40518) mw.toolbar: Implemented mw.toolbar.addButtons for adding multiple + button objects in one call. === Bug fixes in 1.22 === * Disable Special:PasswordReset when $wgEnableEmail is false. Previously one diff --git a/resources/Resources.php b/resources/Resources.php index f931ad891f..d6852c3e30 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -719,6 +719,7 @@ return array( 'mediawiki.action.edit' => array( 'scripts' => 'resources/mediawiki.action/mediawiki.action.edit.js', 'dependencies' => array( + 'mediawiki.legacy.wikibits', 'jquery.textSelection', 'jquery.byteLimit', ), diff --git a/resources/mediawiki.action/mediawiki.action.edit.js b/resources/mediawiki.action/mediawiki.action.edit.js index 1c5a018e6c..d29ee42575 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 } ); } @@ -113,7 +140,7 @@ var buttons, 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' ); @@ -122,21 +149,22 @@ buttons = [].concat( queue, window.mwCustomEditButtons ); // Clear queue queue.length = 0; + for ( i = 0; i < buttons.length; i++ ) { b = buttons[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 or mwCustomEditButtons insertButton( b ); } } // 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 +186,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 +198,7 @@ // for IE .add( $iframe.get( 0 ).contentWindow.document.body ) .focus( function () { - currentFocused = $iframe; + $currentFocused = $iframe; } ); } }); -- 2.20.1