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 * See #addButton for parameter documentation.
17 function insertButton( b
, speedTip
, tagOpen
, tagClose
, sampleText
, imageId
) {
18 // Backwards compatibility
19 if ( typeof b
!== 'object' ) {
25 sampleText
: sampleText
,
29 var $image
= $( '<img>' ).attr( {
35 id
: b
.imageId
|| undefined,
36 'class': 'mw-toolbar-editbutton'
37 } ).click( function ( e
) {
38 if ( b
.onClick
!== undefined ) {
41 toolbar
.insertTags( b
.tagOpen
, b
.tagClose
, b
.sampleText
);
47 $toolbar
.append( $image
);
56 * Contains button objects (and for backwards compatibilty, it can
57 * also contains an arguments array for insertButton).
65 * Add buttons to the toolbar.
67 * Takes care of race conditions and time-based dependencies
68 * by placing buttons in a queue if this method is called before
69 * the toolbar is created.
71 * For backwards-compatibility, passing `imageFile`, `speedTip`, `tagOpen`, `tagClose`,
72 * `sampleText` and `imageId` as separate arguments (in this order) is also supported.
74 * @param {Object} button Object with the following properties.
75 * You are required to provide *either* the `onClick` parameter, or the three parameters
76 * `tagOpen`, `tagClose` and `sampleText`, but not both (they're mutually exclusive).
77 * @param {string} button.imageFile Image to use for the button.
78 * @param {string} button.speedTip Tooltip displayed when user mouses over the button.
79 * @param {Function} [button.onClick] Function to be executed when the button is clicked.
80 * @param {string} [button.tagOpen]
81 * @param {string} [button.tagClose]
82 * @param {string} [button.sampleText] Alternative to `onClick`. `tagOpen`, `tagClose` and
83 * `sampleText` together provide the markup that should be inserted into page text at
84 * current cursor position.
85 * @param {string} [button.imageId] `id` attribute of the button HTML element.
87 addButton: function () {
89 insertButton
.apply( toolbar
, arguments
);
91 // Convert arguments list to array
92 queue
.push( slice
.call( arguments
) );
96 * Add multiple buttons to the toolbar (see also #addButton).
100 * addButtons( [ { .. }, { .. }, { .. } ] );
101 * addButtons( { .. }, { .. } );
103 * @param {Object|Array...} [buttons] An array of button objects or the first
104 * button object in a list of variadic arguments.
106 addButtons: function ( buttons
) {
107 if ( !$.isArray( buttons
) ) {
108 buttons
= slice
.call( arguments
);
111 $.each( buttons
, function () {
112 insertButton( this );
115 // Push each button into the queue
116 queue
.push
.apply( queue
, buttons
);
121 * Apply tagOpen/tagClose to selection in currently focused textarea.
123 * Uses `sampleText` if selection is empty.
125 * @param {string} tagOpen
126 * @param {string} tagClose
127 * @param {string} sampleText
129 insertTags: function ( tagOpen
, tagClose
, sampleText
) {
130 if ( $currentFocused
&& $currentFocused
.length
) {
131 $currentFocused
.textSelection(
132 'encapsulateSelection', {
141 // For backwards compatibility,
142 // Called from EditPage.php, maybe in other places as well.
146 // Legacy (for compatibility with the code previously in skins/common.edit.js)
147 mw
.log
.deprecate( window
, 'addButton', toolbar
.addButton
, 'Use mw.toolbar.addButton instead.' );
148 mw
.log
.deprecate( window
, 'insertTags', toolbar
.insertTags
, 'Use mw.toolbar.insertTags instead.' );
150 // Expose API publicly
151 mw
.toolbar
= toolbar
;
154 var i
, b
, editBox
, scrollTop
, $editForm
;
156 // Used to determine where to insert tags
157 $currentFocused
= $( '#wpTextbox1' );
159 // Populate the selector cache for $toolbar
160 $toolbar
= $( '#toolbar' );
162 for ( i
= 0; i
< queue
.length
; i
++ ) {
164 if ( $.isArray( b
) ) {
165 // Forwarded arguments array from mw.toolbar.addButton
166 insertButton
.apply( toolbar
, b
);
168 // Raw object from mw.toolbar.addButtons
176 // This causes further calls to addButton to go to insertion directly
177 // instead of to the queue.
178 // It is important that this is after the one and only loop through
182 // Make sure edit summary does not exceed byte limit
183 $( '#wpSummary' ).byteLimit( 255 );
185 // Restore the edit box scroll state following a preview operation,
186 // and set up a form submission handler to remember this state.
187 editBox
= document
.getElementById( 'wpTextbox1' );
188 scrollTop
= document
.getElementById( 'wpScrolltop' );
189 $editForm
= $( '#editform' );
190 if ( $editForm
.length
&& editBox
&& scrollTop
) {
191 if ( scrollTop
.value
) {
192 editBox
.scrollTop
= scrollTop
.value
;
194 $editForm
.submit( function () {
195 scrollTop
.value
= editBox
.scrollTop
;
199 // Apply to dynamically created textboxes as well as normal ones
200 $( document
).on( 'focus', 'textarea, input:text', function () {
201 $currentFocused
= $( this );
205 }( mediaWiki
, jQuery
) );