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