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