Move edit.js stuff to mediawiki.action.edit.js, and remove wikibits dependency. Wrap...
[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 addButton : function( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText ) {
8 var image = $('<img>', {
9 width : 23,
10 height : 23,
11 src : imageFile,
12 alt : speedTip,
13 title : speedTip,
14 id : imageId || '',
15 'class': 'mw-toolbar-editbutton'
16 } ).click( function() {
17 mw.toolbar.insertTags( tagOpen, tagClose, sampleText, selectText );
18 return false;
19 } );
20
21 this.$toolbar.append( image );
22 return true;
23 },
24
25 // apply tagOpen/tagClose to selection in textarea,
26 // use sampleText instead of selection if there is none
27 insertTags : function( tagOpen, tagClose, sampleText, selectText) {
28 if ( currentFocused.length ) {
29 currentFocused.textSelection(
30 'encapsulateSelection', { 'pre': tagOpen, 'peri': sampleText, 'post': tagClose }
31 );
32 return;
33 }
34 },
35 init : function() {
36 // Legacy
37 // Print out buttons from mwCustomEditButtons
38 // If you want to add buttons, use
39 // $( document ).ready( function () { mw.toolbar.addButton( imageFile, speedTip, tagOpen, tagClose, sampleText, imageId, selectText ) } );
40 var c;
41 for ( i = 0; i < window.mwCustomEditButtons.length; i++ ) {
42 c = window.mwCustomEditButtons[i];
43 mw.toolbar.addButton( c.imageFile, c.speedTip, c.tagOpen, c.tagClose, c.sampleText, c.imageId, c.selectText );
44 }
45 return true;
46 }
47 };
48
49 //Legacy
50 window.addButton = mw.toolbar.addButton;
51 window.insertTags = mw.toolbar.insertTags;
52
53 //make sure edit summary does not exceed byte limit
54 $( '#wpSummary' ).attr( 'maxLength', 250 ).keypress( function( e ) {
55 // first check to see if this is actually a character key
56 // being pressed.
57 // Based on key-event info from http://unixpapa.com/js/key.html
58 // jQuery should also normalize e.which to be consistent cross-browser,
59 // however the same check is still needed regardless of jQuery.
60
61 // Note: At the moment, for some older opera versions (~< 10.5)
62 // some special keys won't be recognized (aka left arrow key).
63 // Backspace will be, so not big issue.
64
65 if ( e.which === 0 || e.charCode === 0 || e.which === 8 ||
66 e.ctrlKey || e.altKey || e.metaKey )
67 {
68 return true; //a special key (backspace, etc) so don't interfere.
69 }
70
71 // This basically figures out how many bytes a UTF-16 string (which is what js sees)
72 // will take in UTF-8 by replacing a 2 byte character with 2 *'s, etc, and counting that.
73 // Note, surrogate (\uD800-\uDFFF) characters are counted as 2 bytes, since there's two of them
74 // and the actual character takes 4 bytes in UTF-8 (2*2=4). Might not work perfectly in edge cases
75 // such as illegal sequences, but that should never happen.
76
77 var len = this.value.replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' ).replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' ).length;
78 //247 as this doesn't count character about to be inserted.
79 if ( len > 247 ) {
80 e.preventDefault();
81 }
82 });
83
84
85 $( document ).ready( function() {
86 /**
87 * Restore the edit box scroll state following a preview operation,
88 * and set up a form submission handler to remember this state
89 */
90 var scrollEditBox = function() {
91 var editBox = document.getElementById( 'wpTextbox1' );
92 var scrollTop = document.getElementById( 'wpScrolltop' );
93 var $editForm = $( '#editform' );
94 if( $editForm.length && editBox && scrollTop ) {
95 if( scrollTop.value ) {
96 editBox.scrollTop = scrollTop.value;
97 }
98 $editForm.submit( function() {
99 scrollTop.value = editBox.scrollTop;
100 });
101 }
102 };
103 scrollEditBox();
104
105 $( '#wpSummary, #wpTextbox1' ).focus( function() {
106 currentFocused = $(this);
107 });
108
109 // HACK: make currentFocused work with the usability iframe
110 // With proper focus detection support (HTML 5!) this'll be much cleaner
111 var iframe = $( '.wikiEditor-ui-text iframe' );
112 if ( iframe.length > 0 ) {
113 $( iframe.get( 0 ).contentWindow.document )
114 .add( iframe.get( 0 ).contentWindow.document.body ) // for IE
115 .focus( function() { currentFocused = iframe; } );
116 }
117 });
118 })(jQuery);