Merge "Change "permissions errors" message to "permission error""
[lhc/web/wiklou.git] / resources / mediawiki.action / mediawiki.action.edit.js
1 /**
2 * Interface for the classic edit toolbar.
3 *
4 * @class mw.toolbar
5 * @singleton
6 */
7 ( function ( mw, $ ) {
8 var toolbar, isReady, $toolbar, queue, slice, currentFocused;
9
10 /**
11 * Internal helper that does the actual insertion of the button into the toolbar.
12 *
13 * See #addButton for parameter documentation.
14 *
15 * @private
16 */
17 function insertButton( b, speedTip, tagOpen, tagClose, sampleText, imageId ) {
18 // Backwards compatibility
19 if ( typeof b !== 'object' ) {
20 b = {
21 imageFile: b,
22 speedTip: speedTip,
23 tagOpen: tagOpen,
24 tagClose: tagClose,
25 sampleText: sampleText,
26 imageId: imageId
27 };
28 }
29 var $image = $( '<img>' ).attr( {
30 width : 23,
31 height: 22,
32 src : b.imageFile,
33 alt : b.speedTip,
34 title : b.speedTip,
35 id : b.imageId || undefined,
36 'class': 'mw-toolbar-editbutton'
37 } ).click( function () {
38 toolbar.insertTags( b.tagOpen, b.tagClose, b.sampleText );
39 return false;
40 } );
41
42 $toolbar.append( $image );
43 }
44
45 isReady = false;
46 $toolbar = false;
47 queue = [];
48 slice = queue.slice;
49
50 toolbar = {
51
52 /**
53 * Add buttons to the toolbar.
54 *
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.
58 *
59 * For compatiblity, passing the properties listed below as separate arguments
60 * (in the listed order) is also supported.
61 *
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]
69 */
70 addButton: function () {
71 if ( isReady ) {
72 insertButton.apply( toolbar, arguments );
73 } else {
74 // Convert arguments list to array
75 queue.push( slice.call( arguments ) );
76 }
77 },
78
79 /**
80 * Apply tagOpen/tagClose to selection in currently focused textarea.
81 *
82 * Uses `sampleText` if selection is empty.
83 *
84 * @param {string} tagOpen
85 * @param {string} tagClose
86 * @param {string} sampleText
87 */
88 insertTags: function ( tagOpen, tagClose, sampleText ) {
89 if ( currentFocused && currentFocused.length ) {
90 currentFocused.textSelection(
91 'encapsulateSelection', {
92 'pre': tagOpen,
93 'peri': sampleText,
94 'post': tagClose
95 }
96 );
97 }
98 },
99
100 // For backwards compatibility,
101 // Called from EditPage.php, maybe in other places as well.
102 init: function () {}
103 };
104
105 // Legacy (for compatibility with the code previously in skins/common.edit.js)
106 window.addButton = toolbar.addButton;
107 window.insertTags = toolbar.insertTags;
108
109 // Explose API publicly
110 mw.toolbar = toolbar;
111
112 $( document ).ready( function () {
113 var buttons, i, b, $iframe, editBox, scrollTop, $editForm;
114
115 // currentFocus is used to determine where to insert tags
116 currentFocused = $( '#wpTextbox1' );
117
118 // Populate the selector cache for $toolbar
119 $toolbar = $( '#toolbar' );
120
121 // Legacy: Merge buttons from mwCustomEditButtons
122 buttons = [].concat( queue, window.mwCustomEditButtons );
123 // Clear queue
124 queue.length = 0;
125 for ( i = 0; i < buttons.length; i++ ) {
126 b = buttons[i];
127 if ( $.isArray( b ) ) {
128 // Forwarded arguments array from mw.toolbar.addButton
129 insertButton.apply( toolbar, b );
130 } else {
131 // Raw object from legacy mwCustomEditButtons
132 insertButton( b );
133 }
134 }
135
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
140 isReady = true;
141
142 // Make sure edit summary does not exceed byte limit
143 $( '#wpSummary' ).byteLimit( 255 );
144
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;
153 }
154 $editForm.submit( function () {
155 scrollTop.value = editBox.scrollTop;
156 });
157 }
158
159 // Apply to dynamically created textboxes as well as normal ones
160 $( document ).on( 'focus', 'textarea, input:text', function () {
161 currentFocused = $( this );
162 } );
163
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 )
170 // for IE
171 .add( $iframe.get( 0 ).contentWindow.document.body )
172 .focus( function () {
173 currentFocused = $iframe;
174 } );
175 }
176 });
177
178 }( mediaWiki, jQuery ) );