2 * Interface for the classic edit toolbar.
8 var toolbar, isReady, $toolbar, queue, slice, $currentFocused;
11 * Internal helper that does the actual insertion of the button into the toolbar.
13 * For backwards-compatibility, passing `imageFile`, `speedTip`, `tagOpen`, `tagClose`,
14 * `sampleText` and `imageId` as separate arguments (in this order) is also supported.
18 * @param {Object} button Object with the following properties.
19 * You are required to provide *either* the `onClick` parameter, or the three parameters
20 * `tagOpen`, `tagClose` and `sampleText`, but not both (they're mutually exclusive).
21 * @param {string} [button.imageFile] Image to use for the button.
22 * @param {string} button.speedTip Tooltip displayed when user mouses over the button.
23 * @param {Function} [button.onClick] Function to be executed when the button is clicked.
24 * @param {string} [button.tagOpen]
25 * @param {string} [button.tagClose]
26 * @param {string} [button.sampleText] Alternative to `onClick`. `tagOpen`, `tagClose` and
27 * `sampleText` together provide the markup that should be inserted into page text at
28 * current cursor position.
29 * @param {string} [button.imageId] `id` attribute of the button HTML element. Can be
30 * used to define the image with CSS if it's not provided as `imageFile`.
31 * @param {string} [speedTip]
32 * @param {string} [tagOpen]
33 * @param {string} [tagClose]
34 * @param {string} [sampleText]
35 * @param {string} [imageId]
37 function insertButton( button, speedTip, tagOpen, tagClose, sampleText, imageId ) {
40 // Backwards compatibility
41 if ( typeof button !== 'object' ) {
47 sampleText: sampleText,
52 if ( button.imageFile ) {
53 $button = $( '<img>' ).attr( {
54 src: button.imageFile,
56 title: button.speedTip,
57 id: button.imageId || undefined,
58 'class': 'mw-toolbar-editbutton'
61 $button = $( '<div>' ).attr( {
62 title: button.speedTip,
63 id: button.imageId || undefined,
64 'class': 'mw-toolbar-editbutton'
68 $button.click( function ( e ) {
69 if ( button.onClick !== undefined ) {
72 toolbar.insertTags( button.tagOpen, button.tagClose, button.sampleText );
78 $toolbar.append( $button );
87 * Contains button objects (and for backwards compatibilty, it can
88 * also contains an arguments array for insertButton).
96 * Add buttons to the toolbar.
98 * Takes care of race conditions and time-based dependencies by placing buttons in a queue if
99 * this method is called before the toolbar is created.
101 * For backwards-compatibility, passing `imageFile`, `speedTip`, `tagOpen`, `tagClose`,
102 * `sampleText` and `imageId` as separate arguments (in this order) is also supported.
104 * @inheritdoc #insertButton
106 addButton: function () {
108 insertButton.apply( toolbar, arguments );
110 // Convert arguments list to array
111 queue.push( slice.call( arguments ) );
116 * Add multiple buttons to the toolbar (see also #addButton).
120 * addButtons( [ { .. }, { .. }, { .. } ] );
121 * addButtons( { .. }, { .. } );
123 * @param {...Object|Array} [buttons] An array of button objects or the first
124 * button object in a list of variadic arguments.
126 addButtons: function ( buttons ) {
127 if ( !$.isArray( buttons ) ) {
128 buttons = slice.call( arguments );
131 $.each( buttons, function () {
132 insertButton( this );
135 // Push each button into the queue
136 queue.push.apply( queue, buttons );
141 * Apply tagOpen/tagClose to selection in currently focused textarea.
143 * Uses `sampleText` if selection is empty.
145 * @param {string} tagOpen
146 * @param {string} tagClose
147 * @param {string} sampleText
149 insertTags: function ( tagOpen, tagClose, sampleText ) {
150 if ( $currentFocused && $currentFocused.length ) {
151 $currentFocused.textSelection(
152 'encapsulateSelection', {
162 // Legacy (for compatibility with the code previously in skins/common.edit.js)
163 mw.log.deprecate( window, 'addButton', toolbar.addButton, 'Use mw.toolbar.addButton instead.' );
164 mw.log.deprecate( window, 'insertTags', toolbar.insertTags, 'Use mw.toolbar.insertTags instead.' );
166 // For backwards compatibility. Used to be called from EditPage.php, maybe other places as well.
167 mw.log.deprecate( toolbar, 'init', $.noop );
169 // Expose API publicly
170 mw.toolbar = toolbar;
175 // Used to determine where to insert tags
176 $currentFocused = $( '#wpTextbox1' );
178 // Populate the selector cache for $toolbar
179 $toolbar = $( '#toolbar' );
181 for ( i = 0; i < queue.length; i++ ) {
183 if ( $.isArray( button ) ) {
184 // Forwarded arguments array from mw.toolbar.addButton
185 insertButton.apply( toolbar, button );
187 // Raw object from mw.toolbar.addButtons
188 insertButton( button );
195 // This causes further calls to addButton to go to insertion directly
196 // instead of to the queue.
197 // It is important that this is after the one and only loop through
201 // Apply to dynamically created textboxes as well as normal ones
202 $( document ).on( 'focus', 'textarea, input:text', function () {
203 $currentFocused = $( this );
207 }( mediaWiki, jQuery ) );