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 compatiblity, passing the properties listed below as separate arguments
72 * (in the listed order) is also supported.
74 * @param {Object} button Object with the following properties:
75 * @param {string} button.imageFile
76 * @param {string} button.speedTip
77 * @param {string} button.tagOpen
78 * @param {string} button.tagClose
79 * @param {string} button.sampleText
80 * @param {string} [button.imageId]
81 * @param {Function} [button.onClick]
83 addButton: function () {
85 insertButton
.apply( toolbar
, arguments
);
87 // Convert arguments list to array
88 queue
.push( slice
.call( arguments
) );
92 * Add multiple buttons to the toolbar (see also #addButton).
96 * addButtons( [ { .. }, { .. }, { .. } ] );
97 * addButtons( { .. }, { .. } );
99 * @param {Object|Array...} [buttons] An array of button objects or the first
100 * button object in a list of variadic arguments.
102 addButtons: function ( buttons
) {
103 if ( !$.isArray( buttons
) ) {
104 buttons
= slice
.call( arguments
);
107 $.each( buttons
, function () {
108 insertButton( this );
111 // Push each button into the queue
112 queue
.push
.apply( queue
, buttons
);
117 * Apply tagOpen/tagClose to selection in currently focused textarea.
119 * Uses `sampleText` if selection is empty.
121 * @param {string} tagOpen
122 * @param {string} tagClose
123 * @param {string} sampleText
125 insertTags: function ( tagOpen
, tagClose
, sampleText
) {
126 if ( $currentFocused
&& $currentFocused
.length
) {
127 $currentFocused
.textSelection(
128 'encapsulateSelection', {
137 // For backwards compatibility,
138 // Called from EditPage.php, maybe in other places as well.
142 // Legacy (for compatibility with the code previously in skins/common.edit.js)
143 mw
.log
.deprecate( window
, 'addButton', toolbar
.addButton
, 'Use mw.toolbar.addButton instead.' );
144 mw
.log
.deprecate( window
, 'insertTags', toolbar
.insertTags
, 'Use mw.toolbar.insertTags instead.' );
146 // Expose API publicly
147 mw
.toolbar
= toolbar
;
150 var i
, b
, editBox
, scrollTop
, $editForm
;
152 // Used to determine where to insert tags
153 $currentFocused
= $( '#wpTextbox1' );
155 // Populate the selector cache for $toolbar
156 $toolbar
= $( '#toolbar' );
158 for ( i
= 0; i
< queue
.length
; i
++ ) {
160 if ( $.isArray( b
) ) {
161 // Forwarded arguments array from mw.toolbar.addButton
162 insertButton
.apply( toolbar
, b
);
164 // Raw object from mw.toolbar.addButtons
172 // This causes further calls to addButton to go to insertion directly
173 // instead of to the queue.
174 // It is important that this is after the one and only loop through
178 // Make sure edit summary does not exceed byte limit
179 $( '#wpSummary' ).byteLimit( 255 );
181 // Restore the edit box scroll state following a preview operation,
182 // and set up a form submission handler to remember this state.
183 editBox
= document
.getElementById( 'wpTextbox1' );
184 scrollTop
= document
.getElementById( 'wpScrolltop' );
185 $editForm
= $( '#editform' );
186 if ( $editForm
.length
&& editBox
&& scrollTop
) {
187 if ( scrollTop
.value
) {
188 editBox
.scrollTop
= scrollTop
.value
;
190 $editForm
.submit( function () {
191 scrollTop
.value
= editBox
.scrollTop
;
195 // Apply to dynamically created textboxes as well as normal ones
196 $( document
).on( 'focus', 'textarea, input:text', function () {
197 $currentFocused
= $( this );
201 }( mediaWiki
, jQuery
) );