Increase the readability of provideFormattableTimes by adding newlines
[lhc/web/wiklou.git] / resources / mediawiki.action / mediawiki.action.edit.js
1 ( function ( $, mw ) {
2 var isReady, toolbar, currentFocused;
3
4 isReady = false;
5
6 toolbar = {
7 $toolbar: false,
8 buttons: [],
9 /**
10 * If you want to add buttons, use
11 * mw.toolbar.addButton( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText );
12 */
13 addButton: function () {
14 if ( isReady ) {
15 toolbar.insertButton.apply( toolbar, arguments );
16 } else {
17 toolbar.buttons.push( [].slice.call( arguments ) );
18 }
19 },
20 insertButton: function ( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText ) {
21 var image = $('<img>', {
22 width : 23,
23 height: 22,
24 src : imageFile,
25 alt : speedTip,
26 title : speedTip,
27 id : imageId || '',
28 'class': 'mw-toolbar-editbutton'
29 } ).click( function () {
30 mw.toolbar.insertTags( tagOpen, tagClose, sampleText, selectText );
31 return false;
32 } );
33
34 toolbar.$toolbar.append( image );
35 return true;
36 },
37
38 /**
39 * apply tagOpen/tagClose to selection in textarea,
40 * use sampleText instead of selection if there is none.
41 */
42 insertTags: function ( tagOpen, tagClose, sampleText, selectText ) {
43 if ( currentFocused && currentFocused.length ) {
44 currentFocused.textSelection(
45 'encapsulateSelection', {
46 'pre': tagOpen,
47 'peri': sampleText,
48 'post': tagClose
49 }
50 );
51 }
52 },
53
54 // For backwards compatibility
55 init: function () {}
56 };
57
58 // Legacy (for compatibility with the code previously in skins/common.edit.js)
59 window.addButton = toolbar.addButton;
60 window.insertTags = toolbar.insertTags;
61
62 // Explose publicly
63 mw.toolbar = toolbar;
64
65 $( document ).ready( function () {
66 var buttons, i, c, iframe;
67
68 // currentFocus is used to determine where to insert tags
69 currentFocused = $( '#wpTextbox1' );
70
71 // Populate the selector cache for $toolbar
72 toolbar.$toolbar = $( '#toolbar' );
73
74 // Legacy: Merge buttons from mwCustomEditButtons
75 buttons = [].concat( toolbar.buttons, window.mwCustomEditButtons );
76 for ( i = 0; i < buttons.length; i++ ) {
77 if ( $.isArray( buttons[i] ) ) {
78 // Passes our button array as arguments
79 toolbar.insertButton.apply( toolbar, buttons[i] );
80 } else {
81 // Legacy mwCustomEditButtons is an object
82 c = buttons[i];
83 toolbar.insertButton( c.imageFile, c.speedTip, c.tagOpen,
84 c.tagClose, c.sampleText, c.imageId, c.selectText );
85 }
86 }
87
88 // This causes further calls to addButton to go to insertion directly
89 // instead of to the toolbar.buttons queue.
90 // It is important that this is after the one and only loop through
91 // the the toolbar.buttons queue
92 isReady = true;
93
94 // Make sure edit summary does not exceed byte limit
95 $( '#wpSummary' ).byteLimit( 255 );
96
97 /**
98 * Restore the edit box scroll state following a preview operation,
99 * and set up a form submission handler to remember this state
100 */
101 ( function scrollEditBox() {
102 var editBox, scrollTop, $editForm;
103
104 editBox = document.getElementById( 'wpTextbox1' );
105 scrollTop = document.getElementById( 'wpScrolltop' );
106 $editForm = $( '#editform' );
107 if ( $editForm.length && editBox && scrollTop ) {
108 if ( scrollTop.value ) {
109 editBox.scrollTop = scrollTop.value;
110 }
111 $editForm.submit( function () {
112 scrollTop.value = editBox.scrollTop;
113 });
114 }
115 }() );
116
117 $( 'textarea, input:text' ).focus( function () {
118 currentFocused = $(this);
119 });
120
121 // HACK: make currentFocused work with the usability iframe
122 // With proper focus detection support (HTML 5!) this'll be much cleaner
123 iframe = $( '.wikiEditor-ui-text iframe' );
124 if ( iframe.length > 0 ) {
125 $( iframe.get( 0 ).contentWindow.document )
126 // for IE
127 .add( iframe.get( 0 ).contentWindow.document.body )
128 .focus( function () {
129 currentFocused = iframe;
130 } );
131 }
132 });
133
134 }( jQuery, mediaWiki ) );