2 var isReady
, toolbar
, currentFocused
, queue
, $toolbar
, slice
;
7 slice
= Array
.prototype.slice
;
10 * Internal helper that does the actual insertion
11 * of the button into the toolbar.
12 * See mw.toolbar.addButton for parameter documentation.
14 function insertButton( b
/* imageFile */, speedTip
, tagOpen
, tagClose
, sampleText
, imageId
, selectText
) {
15 // Backwards compatibility
16 if ( typeof b
!== 'object' ) {
22 sampleText
: sampleText
,
24 selectText
: selectText
27 var $image
= $( '<img>', {
33 id
: b
.imageId
|| undefined,
34 'class': 'mw-toolbar-editbutton'
35 } ).click( function () {
36 toolbar
.insertTags( b
.tagOpen
, b
.tagClose
, b
.sampleText
, b
.selectText
);
40 $toolbar
.append( $image
);
46 * Add buttons to the toolbar.
47 * Takes care of race conditions and time-based dependencies
48 * by placing buttons in a queue if this method is called before
49 * the toolbar is created.
50 * @param {Object} button: Object with the following properties:
58 * For compatiblity, passing the above as separate arguments
59 * (in the listed order) is also supported.
61 addButton: function () {
63 insertButton
.apply( toolbar
, arguments
);
65 // Convert arguments list to array
66 queue
.push( slice
.call( arguments
) );
71 * Apply tagOpen/tagClose to selection in textarea,
72 * use sampleText instead of selection if there is none.
74 insertTags: function ( tagOpen
, tagClose
, sampleText
) {
75 if ( currentFocused
&& currentFocused
.length
) {
76 currentFocused
.textSelection(
77 'encapsulateSelection', {
86 // For backwards compatibility,
87 // Called from EditPage.php, maybe in other places as well.
91 // Legacy (for compatibility with the code previously in skins/common.edit.js)
92 window
.addButton
= toolbar
.addButton
;
93 window
.insertTags
= toolbar
.insertTags
;
95 // Explose API publicly
98 $( document
).ready( function () {
99 var buttons
, i
, b
, $iframe
;
101 // currentFocus is used to determine where to insert tags
102 currentFocused
= $( '#wpTextbox1' );
104 // Populate the selector cache for $toolbar
105 $toolbar
= $( '#toolbar' );
107 // Legacy: Merge buttons from mwCustomEditButtons
108 buttons
= [].concat( queue
, window
.mwCustomEditButtons
);
111 for ( i
= 0; i
< buttons
.length
; i
++ ) {
113 if ( $.isArray( b
) ) {
114 // Forwarded arguments array from mw.toolbar.addButton
115 insertButton
.apply( toolbar
, b
);
117 // Raw object from legacy mwCustomEditButtons
122 // This causes further calls to addButton to go to insertion directly
123 // instead of to the toolbar.buttons queue.
124 // It is important that this is after the one and only loop through
125 // the the toolbar.buttons queue
128 // Make sure edit summary does not exceed byte limit
129 $( '#wpSummary' ).byteLimit( 255 );
132 * Restore the edit box scroll state following a preview operation,
133 * and set up a form submission handler to remember this state
135 ( function scrollEditBox() {
136 var editBox
, scrollTop
, $editForm
;
138 editBox
= document
.getElementById( 'wpTextbox1' );
139 scrollTop
= document
.getElementById( 'wpScrolltop' );
140 $editForm
= $( '#editform' );
141 if ( $editForm
.length
&& editBox
&& scrollTop
) {
142 if ( scrollTop
.value
) {
143 editBox
.scrollTop
= scrollTop
.value
;
145 $editForm
.submit( function () {
146 scrollTop
.value
= editBox
.scrollTop
;
151 // Apply to dynamically created textboxes as well as normal ones
152 $( document
).on( 'focus', 'textarea, input:text', function () {
153 currentFocused
= $( this );
156 // HACK: make currentFocused work with the usability iframe
157 // With proper focus detection support (HTML 5!) this'll be much cleaner
158 // TODO: Get rid of this WikiEditor code from MediaWiki core!
159 $iframe
= $( '.wikiEditor-ui-text iframe' );
160 if ( $iframe
.length
> 0 ) {
161 $( $iframe
.get( 0 ).contentWindow
.document
)
163 .add( $iframe
.get( 0 ).contentWindow
.document
.body
)
164 .focus( function () {
165 currentFocused
= $iframe
;
170 }( mediaWiki
, jQuery
) );