17bf11378564aae42b5564a5baa19beca4f61ff9
[lhc/web/wiklou.git] / skins / common / edit.js
1 var currentFocused;
2
3 // this function generates the actual toolbar buttons with localized text
4 // we use it to avoid creating the toolbar where javascript is not enabled
5 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText, imageId) {
6 // Don't generate buttons for browsers which don't fully
7 // support it.
8 mwEditButtons.push(
9 {"imageId": imageId,
10 "imageFile": imageFile,
11 "speedTip": speedTip,
12 "tagOpen": tagOpen,
13 "tagClose": tagClose,
14 "sampleText": sampleText});
15 }
16
17 // this function generates the actual toolbar buttons with localized text
18 // we use it to avoid creating the toolbar where javascript is not enabled
19 function mwInsertEditButton(parent, item) {
20 var image = document.createElement("img");
21 image.width = 23;
22 image.height = 22;
23 image.className = "mw-toolbar-editbutton";
24 if (item.imageId) image.id = item.imageId;
25 image.src = item.imageFile;
26 image.border = 0;
27 image.alt = item.speedTip;
28 image.title = item.speedTip;
29 image.style.cursor = "pointer";
30 image.onclick = function() {
31 insertTags(item.tagOpen, item.tagClose, item.sampleText);
32 //click tracking
33 if ( ( $j != undefined ) && ( $j.trackAction != undefined ) ) {
34 $j.trackAction("oldedit." + item.speedTip.replace(/ /g, "-"));
35 }
36 return false;
37 };
38
39 parent.appendChild(image);
40 return true;
41 }
42
43 function mwSetupToolbar() {
44 var toolbar = document.getElementById('toolbar');
45 if (!toolbar) { return false; }
46
47 // Don't generate buttons for browsers which don't fully
48 // support it.
49 var textbox = document.createElement('textarea'); // abstract, don't assume wpTextbox1 is always there
50 if (!(document.selection && document.selection.createRange)
51 && textbox.selectionStart === null) {
52 return false;
53 }
54
55 for (var i = 0; i < mwEditButtons.length; i++) {
56 mwInsertEditButton(toolbar, mwEditButtons[i]);
57 }
58 for (var i = 0; i < mwCustomEditButtons.length; i++) {
59 mwInsertEditButton(toolbar, mwCustomEditButtons[i]);
60 }
61 return true;
62 }
63
64 // apply tagOpen/tagClose to selection in textarea,
65 // use sampleText instead of selection if there is none
66 function insertTags(tagOpen, tagClose, sampleText) {
67 var txtarea;
68 if (document.editform) {
69 txtarea = currentFocused;
70 } else {
71 // some alternate form? take the first one we can find
72 var areas = document.getElementsByTagName('textarea');
73 txtarea = areas[0];
74 }
75 var selText, isSample = false;
76
77 if (document.selection && document.selection.createRange) { // IE/Opera
78
79 //save window scroll position
80 if (document.documentElement && document.documentElement.scrollTop)
81 var winScroll = document.documentElement.scrollTop
82 else if (document.body)
83 var winScroll = document.body.scrollTop;
84 //get current selection
85 txtarea.focus();
86 var range = document.selection.createRange();
87 selText = range.text;
88 //insert tags
89 checkSelectedText();
90 range.text = tagOpen + selText + tagClose;
91 //mark sample text as selected
92 if (isSample && range.moveStart) {
93 if (window.opera)
94 tagClose = tagClose.replace(/\n/g,'');
95 range.moveStart('character', - tagClose.length - selText.length);
96 range.moveEnd('character', - tagClose.length);
97 }
98 range.select();
99 //restore window scroll position
100 if (document.documentElement && document.documentElement.scrollTop)
101 document.documentElement.scrollTop = winScroll
102 else if (document.body)
103 document.body.scrollTop = winScroll;
104
105 } else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla
106
107 //save textarea scroll position
108 var textScroll = txtarea.scrollTop;
109 //get current selection
110 txtarea.focus();
111 var startPos = txtarea.selectionStart;
112 var endPos = txtarea.selectionEnd;
113 selText = txtarea.value.substring(startPos, endPos);
114 //insert tags
115 checkSelectedText();
116 txtarea.value = txtarea.value.substring(0, startPos)
117 + tagOpen + selText + tagClose
118 + txtarea.value.substring(endPos, txtarea.value.length);
119 //set new selection
120 if (isSample) {
121 txtarea.selectionStart = startPos + tagOpen.length;
122 txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
123 } else {
124 txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
125 txtarea.selectionEnd = txtarea.selectionStart;
126 }
127 //restore textarea scroll position
128 txtarea.scrollTop = textScroll;
129 }
130
131 function checkSelectedText(){
132 if (!selText) {
133 selText = sampleText;
134 isSample = true;
135 } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
136 selText = selText.substring(0, selText.length - 1);
137 tagClose += ' '
138 }
139 }
140
141 }
142
143 /**
144 * Restore the edit box scroll state following a preview operation,
145 * and set up a form submission handler to remember this state
146 */
147 function scrollEditBox() {
148 var editBox = document.getElementById( 'wpTextbox1' );
149 var scrollTop = document.getElementById( 'wpScrolltop' );
150 var editForm = document.getElementById( 'editform' );
151 if( editForm && editBox && scrollTop ) {
152 if( scrollTop.value )
153 editBox.scrollTop = scrollTop.value;
154 addHandler( editForm, 'submit', function() {
155 scrollTop.value = editBox.scrollTop;
156 } );
157 }
158 }
159 hookEvent( 'load', scrollEditBox );
160 hookEvent( 'load', mwSetupToolbar );
161 hookEvent( 'load', function() {
162 currentFocused = document.getElementById( 'wpTextbox1' );
163 } );
164