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 ( !Array
.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 toolbar
.init
= $.noop
;
169 // Expose API publicly
170 // @deprecated since MW 1.29
171 mw
.log
.deprecate( mw
, 'toolbar', toolbar
, null, 'mw.toolbar' );
176 // Used to determine where to insert tags
177 $currentFocused
= $( '#wpTextbox1' );
179 // Populate the selector cache for $toolbar
180 $toolbar
= $( '#toolbar' );
182 for ( i
= 0; i
< queue
.length
; i
++ ) {
184 if ( Array
.isArray( button
) ) {
185 // Forwarded arguments array from mw.toolbar.addButton
186 insertButton
.apply( toolbar
, button
);
188 // Raw object from mw.toolbar.addButtons
189 insertButton( button
);
196 // This causes further calls to addButton to go to insertion directly
197 // instead of to the queue.
198 // It is important that this is after the one and only loop through
202 // Apply to dynamically created textboxes as well as normal ones
203 $( document
).on( 'focus', 'textarea, input:text', function () {
204 $currentFocused
= $( this );
208 }( mediaWiki
, jQuery
) );