Merge "Update SkinLegacy to use newer methods to create the file link."
[lhc/web/wiklou.git] / resources / mediawiki.action / mediawiki.action.edit.js
1 ( function ( mw, $ ) {
2 var isReady, toolbar, currentFocused, queue, $toolbar, slice;
3
4 isReady = false;
5 queue = [];
6 $toolbar = false;
7 slice = Array.prototype.slice;
8
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 } );
39
40 $toolbar.append( $image );
41 return true;
42 }
43
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 },
69
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, selectText ) {
75 if ( currentFocused && currentFocused.length ) {
76 currentFocused.textSelection(
77 'encapsulateSelection', {
78 'pre': tagOpen,
79 'peri': sampleText,
80 'post': tagClose
81 }
82 );
83 }
84 },
85
86 // For backwards compatibility,
87 // Called from EditPage.php, maybe in other places as well.
88 init: function () {}
89 };
90
91 // Legacy (for compatibility with the code previously in skins/common.edit.js)
92 window.addButton = toolbar.addButton;
93 window.insertTags = toolbar.insertTags;
94
95 // Explose API publicly
96 mw.toolbar = toolbar;
97
98 $( document ).ready( function () {
99 var buttons, i, b, iframe;
100
101 // currentFocus is used to determine where to insert tags
102 currentFocused = $( '#wpTextbox1' );
103
104 // Populate the selector cache for $toolbar
105 $toolbar = $( '#toolbar' );
106
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 }
121
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;
127
128 // Make sure edit summary does not exceed byte limit
129 $( '#wpSummary' ).byteLimit( 255 );
130
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;
137
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 }() );
150
151 $( 'textarea, input:text' ).focus( function () {
152 currentFocused = $(this);
153 });
154
155 // HACK: make currentFocused work with the usability iframe
156 // With proper focus detection support (HTML 5!) this'll be much cleaner
157 iframe = $( '.wikiEditor-ui-text iframe' );
158 if ( iframe.length > 0 ) {
159 $( iframe.get( 0 ).contentWindow.document )
160 // for IE
161 .add( iframe.get( 0 ).contentWindow.document.body )
162 .focus( function () {
163 currentFocused = iframe;
164 } );
165 }
166 });
167
168 }( mediaWiki, jQuery ) );