Replaced all instances of <<<END (which breaks vim syntax highlighting), with a type...
[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.getElementById('wpTextbox1');
50 if (!textbox) { return false; }
51 //var textbox = document.createElement('textarea'); // abstract, don't assume wpTextbox1 is always there
52 if (!(document.selection && document.selection.createRange)
53 && textbox.selectionStart === null) {
54 return false;
55 }
56
57 for (var i = 0; i < mwEditButtons.length; i++) {
58 mwInsertEditButton(toolbar, mwEditButtons[i]);
59 }
60 for (var i = 0; i < mwCustomEditButtons.length; i++) {
61 mwInsertEditButton(toolbar, mwCustomEditButtons[i]);
62 }
63 return true;
64 }
65
66 // apply tagOpen/tagClose to selection in textarea,
67 // use sampleText instead of selection if there is none
68 function insertTags(tagOpen, tagClose, sampleText) {
69 var txtarea;
70 if (document.editform) {
71 txtarea = currentFocused;
72 } else {
73 // some alternate form? take the first one we can find
74 var areas = document.getElementsByTagName('textarea');
75 txtarea = areas[0];
76 }
77 var selText, isSample = false;
78
79 if (document.selection && document.selection.createRange) { // IE/Opera
80
81 //save window scroll position
82 if (document.documentElement && document.documentElement.scrollTop)
83 var winScroll = document.documentElement.scrollTop
84 else if (document.body)
85 var winScroll = document.body.scrollTop;
86 //get current selection
87 txtarea.focus();
88 var range = document.selection.createRange();
89 selText = range.text;
90 //insert tags
91 checkSelectedText();
92 range.text = tagOpen + selText + tagClose;
93 //mark sample text as selected
94 if (isSample && range.moveStart) {
95 if (window.opera)
96 tagClose = tagClose.replace(/\n/g,'');
97 range.moveStart('character', - tagClose.length - selText.length);
98 range.moveEnd('character', - tagClose.length);
99 }
100 range.select();
101 //restore window scroll position
102 if (document.documentElement && document.documentElement.scrollTop)
103 document.documentElement.scrollTop = winScroll
104 else if (document.body)
105 document.body.scrollTop = winScroll;
106
107 } else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla
108
109 //save textarea scroll position
110 var textScroll = txtarea.scrollTop;
111 //get current selection
112 txtarea.focus();
113 var startPos = txtarea.selectionStart;
114 var endPos = txtarea.selectionEnd;
115 selText = txtarea.value.substring(startPos, endPos);
116 //insert tags
117 checkSelectedText();
118 txtarea.value = txtarea.value.substring(0, startPos)
119 + tagOpen + selText + tagClose
120 + txtarea.value.substring(endPos, txtarea.value.length);
121 //set new selection
122 if (isSample) {
123 txtarea.selectionStart = startPos + tagOpen.length;
124 txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
125 } else {
126 txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
127 txtarea.selectionEnd = txtarea.selectionStart;
128 }
129 //restore textarea scroll position
130 txtarea.scrollTop = textScroll;
131 }
132
133 function checkSelectedText(){
134 if (!selText) {
135 selText = sampleText;
136 isSample = true;
137 } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
138 selText = selText.substring(0, selText.length - 1);
139 tagClose += ' '
140 }
141 }
142
143 }
144
145 /**
146 * Restore the edit box scroll state following a preview operation,
147 * and set up a form submission handler to remember this state
148 */
149 function scrollEditBox() {
150 var editBox = document.getElementById( 'wpTextbox1' );
151 var scrollTop = document.getElementById( 'wpScrolltop' );
152 var editForm = document.getElementById( 'editform' );
153 if( editForm && editBox && scrollTop ) {
154 if( scrollTop.value )
155 editBox.scrollTop = scrollTop.value;
156 addHandler( editForm, 'submit', function() {
157 scrollTop.value = editBox.scrollTop;
158 } );
159 }
160 }
161 hookEvent( 'load', scrollEditBox );
162 hookEvent( 'load', mwSetupToolbar );
163 hookEvent( 'load', function() {
164 currentFocused = document.getElementById( 'wpTextbox1' );
165 } );
166