Localisation updates from http://translatewiki.net.
[mediawiki.git] / resources / mediawiki.action / mediawiki.action.edit.js
blob2835c9cccf676b04063ab78d2346c917cdd622e9
1 ( function ( mw, $ ) {
2         var isReady, toolbar, currentFocused, queue, $toolbar, slice;
4         isReady = false;
5         queue = [];
6         $toolbar = false;
7         slice = Array.prototype.slice;
9         /**
10          * Internal helper that does the actual insertion
11          * of the button into the toolbar.
12          * See mw.toolbar.addButton for parameter documentation.
13          */
14         function insertButton( b /* imageFile */, speedTip, tagOpen, tagClose, sampleText, imageId, selectText ) {
15                 // Backwards compatibility
16                 if ( typeof b !== 'object' ) {
17                         b = {
18                                 imageFile: b,
19                                 speedTip: speedTip,
20                                 tagOpen: tagOpen,
21                                 tagClose: tagClose,
22                                 sampleText: sampleText,
23                                 imageId: imageId,
24                                 selectText: selectText
25                         };
26                 }
27                 var $image = $( '<img>', {
28                         width : 23,
29                         height: 22,
30                         src   : b.imageFile,
31                         alt   : b.speedTip,
32                         title : b.speedTip,
33                         id    : b.imageId || undefined,
34                         'class': 'mw-toolbar-editbutton'
35                 } ).click( function () {
36                         toolbar.insertTags( b.tagOpen, b.tagClose, b.sampleText, b.selectText );
37                         return false;
38                 } );
40                 $toolbar.append( $image );
41                 return true;
42         }
44         toolbar = {
45                 /**
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:
51                  * - imageFile
52                  * - speedTip
53                  * - tagOpen
54                  * - tagClose
55                  * - sampleText
56                  * - imageId
57                  * - selectText
58                  * For compatiblity, passing the above as separate arguments
59                  * (in the listed order) is also supported.
60                  */
61                 addButton: function () {
62                         if ( isReady ) {
63                                 insertButton.apply( toolbar, arguments );
64                         } else {
65                                 // Convert arguments list to array
66                                 queue.push( slice.call( arguments ) );
67                         }
68                 },
70                 /**
71                  * Apply tagOpen/tagClose to selection in textarea,
72                  * use sampleText instead of selection if there is none.
73                  */
74                 insertTags: function ( tagOpen, tagClose, sampleText ) {
75                         if ( currentFocused && currentFocused.length ) {
76                                 currentFocused.textSelection(
77                                         'encapsulateSelection', {
78                                                 'pre': tagOpen,
79                                                 'peri': sampleText,
80                                                 'post': tagClose
81                                         }
82                                 );
83                         }
84                 },
86                 // For backwards compatibility,
87                 // Called from EditPage.php, maybe in other places as well.
88                 init: function () {}
89         };
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
96         mw.toolbar = toolbar;
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 );
109                 // Clear queue
110                 queue.length = 0;
111                 for ( i = 0; i < buttons.length; i++ ) {
112                         b = buttons[i];
113                         if ( $.isArray( b ) ) {
114                                 // Forwarded arguments array from mw.toolbar.addButton
115                                 insertButton.apply( toolbar, b );
116                         } else {
117                                 // Raw object from legacy mwCustomEditButtons
118                                 insertButton( b );
119                         }
120                 }
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
126                 isReady = true;
128                 // Make sure edit summary does not exceed byte limit
129                 $( '#wpSummary' ).byteLimit( 255 );
131                 /**
132                  * Restore the edit box scroll state following a preview operation,
133                  * and set up a form submission handler to remember this state
134                  */
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;
144                                 }
145                                 $editForm.submit( function () {
146                                         scrollTop.value = editBox.scrollTop;
147                                 });
148                         }
149                 }() );
151                 $( 'textarea, input:text' ).focus( function () {
152                         currentFocused = $(this);
153                 });
155                 // HACK: make currentFocused work with the usability iframe
156                 // With proper focus detection support (HTML 5!) this'll be much cleaner
157                 // TODO: Get rid of this WikiEditor code from MediaWiki core!
158                 $iframe = $( '.wikiEditor-ui-text iframe' );
159                 if ( $iframe.length > 0 ) {
160                         $( $iframe.get( 0 ).contentWindow.document )
161                                 // for IE
162                                 .add( $iframe.get( 0 ).contentWindow.document.body )
163                                 .focus( function () {
164                                         currentFocused = $iframe;
165                                 } );
166                 }
167         });
169 }( mediaWiki, jQuery ) );