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 () {
38 toolbar.insertTags( b.tagOpen, b.tagClose, b.sampleText );
42 $toolbar.append( $image );
53 * Add buttons to the toolbar.
55 * Takes care of race conditions and time-based dependencies
56 * by placing buttons in a queue if this method is called before
57 * the toolbar is created.
59 * For compatiblity, passing the properties listed below as separate arguments
60 * (in the listed order) is also supported.
62 * @param {Object} button Object with the following properties:
63 * @param {string} button.imageFile
64 * @param {string} button.speedTip
65 * @param {string} button.tagOpen
66 * @param {string} button.tagClose
67 * @param {string} button.sampleText
68 * @param {string} [button.imageId]
70 addButton: function () {
72 insertButton.apply( toolbar, arguments );
74 // Convert arguments list to array
75 queue.push( slice.call( arguments ) );
80 * Apply tagOpen/tagClose to selection in currently focused textarea.
82 * Uses `sampleText` if selection is empty.
84 * @param {string} tagOpen
85 * @param {string} tagClose
86 * @param {string} sampleText
88 insertTags: function ( tagOpen, tagClose, sampleText ) {
89 if ( currentFocused && currentFocused.length ) {
90 currentFocused.textSelection(
91 'encapsulateSelection', {
100 // For backwards compatibility,
101 // Called from EditPage.php, maybe in other places as well.
105 // Legacy (for compatibility with the code previously in skins/common.edit.js)
106 window.addButton = toolbar.addButton;
107 window.insertTags = toolbar.insertTags;
109 // Explose API publicly
110 mw.toolbar = toolbar;
112 $( document ).ready( function () {
113 var buttons, i, b, $iframe, editBox, scrollTop, $editForm;
115 // currentFocus is used to determine where to insert tags
116 currentFocused = $( '#wpTextbox1' );
118 // Populate the selector cache for $toolbar
119 $toolbar = $( '#toolbar' );
121 // Legacy: Merge buttons from mwCustomEditButtons
122 buttons = [].concat( queue, window.mwCustomEditButtons );
125 for ( i = 0; i < buttons.length; i++ ) {
127 if ( $.isArray( b ) ) {
128 // Forwarded arguments array from mw.toolbar.addButton
129 insertButton.apply( toolbar, b );
131 // Raw object from legacy mwCustomEditButtons
136 // This causes further calls to addButton to go to insertion directly
137 // instead of to the toolbar.buttons queue.
138 // It is important that this is after the one and only loop through
139 // the the toolbar.buttons queue
142 // Make sure edit summary does not exceed byte limit
143 $( '#wpSummary' ).byteLimit( 255 );
145 // Restore the edit box scroll state following a preview operation,
146 // and set up a form submission handler to remember this state.
147 editBox = document.getElementById( 'wpTextbox1' );
148 scrollTop = document.getElementById( 'wpScrolltop' );
149 $editForm = $( '#editform' );
150 if ( $editForm.length && editBox && scrollTop ) {
151 if ( scrollTop.value ) {
152 editBox.scrollTop = scrollTop.value;
154 $editForm.submit( function () {
155 scrollTop.value = editBox.scrollTop;
159 // Apply to dynamically created textboxes as well as normal ones
160 $( document ).on( 'focus', 'textarea, input:text', function () {
161 currentFocused = $( this );
164 // HACK: make currentFocused work with the usability iframe
165 // With proper focus detection support (HTML 5!) this'll be much cleaner
166 // TODO: Get rid of this WikiEditor code from MediaWiki core!
167 $iframe = $( '.wikiEditor-ui-text iframe' );
168 if ( $iframe.length > 0 ) {
169 $( $iframe.get( 0 ).contentWindow.document )
171 .add( $iframe.get( 0 ).contentWindow.document.body )
172 .focus( function () {
173 currentFocused = $iframe;
178 }( mediaWiki, jQuery ) );