1 // Toolbar JavaScript support functions. Taken from mediawiki
4 var doctype = '<!DOCTYPE html>';
5 var cssfile = '<link rel="stylesheet" type="text/css" href="'+data_path+'/themes/default/toolbar.css" />';
7 // Un-trap us from framesets
8 if( window.top != window ) window.top.location = window.location;
11 // This function generates the actual toolbar buttons with localized text
12 // We use it to avoid creating the toolbar where javascript is not enabled
13 // Not all buttons use this helper, some need special javascript treatment.
14 function addButton(imageFile, speedTip, func, args) {
16 speedTip=escapeQuotes(speedTip);
17 document.write("<a href=\"javascript:"+func+"(");
18 for (i=0; i<args.length; i++){
19 if (i>0) document.write(",");
20 document.write("'"+escapeQuotes(args[i])+"'");
22 document.write(");\"><img src=\""+imageFile+"\" alt=\""+speedTip+"\" title=\""+speedTip+"\">");
23 document.write("</a>");
25 function addTagButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
26 addButton(imageFile, speedTip, "insertTags", [tagOpen, tagClose, sampleText]);
29 // This function generates a popup list to select from.
30 // plugins, pagenames, categories, templates.
31 // Not with document.write because we cannot use self.opener then.
32 //function addPulldown(imageFile, speedTip, pages) {
33 // addButton(imageFile, speedTip, "showPulldown", pages);
36 // pages is either an array of strings or an array of array(name,value)
37 function showPulldown(title, pages, okbutton, closebutton, fromid) {
38 var height = new String(Math.min(315, 80 + (pages.length * 12))); // 270 or smaller
40 var h = (screen.height-height)/2;
41 var w = (screen.width-width)/2;
42 pullwin = window.open('','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,top='+h+',left='+w+',height='+height+',width='+width);
43 // Close the window with Escape key
44 pullwin.onkeydown = function(e){
46 pullwin.window.close();
49 pullwin.window.document.writeln(doctype);
50 pullwin.window.document.writeln('<html>\n<head>\n<title>'+escapeQuotes(title)+'</title>');
51 pullwin.window.document.writeln(cssfile);
52 pullwin.window.document.writeln('</head>\n<body>');
53 pullwin.window.document.writeln('<p>\nYou can double-click to insert.\n</p>');
54 pullwin.window.document.writeln('<form><div id=\"buttons\"><input type=\"button\" value=\"'+okbutton+'\" onclick=\"if(self.opener)self.opener.do_pulldown(document.forms[0].select.value,\''+fromid+'\'); return false;\" /><input type=\"button\" value=\"'+closebutton+'\" onclick=\"self.close(); return false;\" /></div>\n<div>\n<select style=\"margin-top:10px;width:190px;\" name=\"select\" size=\"'+((pages.length>20)?'20':new String(pages.length))+'\" ondblclick=\"if(self.opener)self.opener.do_pulldown(document.forms[0].select.value,\''+fromid+'\'); return false;\">');
55 for (i=0; i<pages.length; i++){
56 if (typeof pages[i] == 'string')
57 pullwin.window.document.write('<option value="'+pages[i]+'">'+escapeQuotes(pages[i])+'</option>\n');
59 pullwin.window.document.write('<option value="'+pages[i][1]+'">'+escapeQuotes(pages[i][0])+'</option>\n');
61 pullwin.window.document.writeln('</select>\n</div>\n</form>\n</body>\n</html>');
62 pullwin.window.document.close();
65 function do_pulldown(text,fromid) {
66 // do special actions dependent on fromid: tb-categories
67 if (fromid == 'tb-categories') {
68 var txtarea = document.getElementById('edit-content');
69 text = unescapeSpecial(text);
70 txtarea.value += '\n'+text;
72 insertTags(text, '', '\n');
75 function addInfobox(infoText) {
76 // if no support for changing selection, add a small copy & paste field
77 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
78 var is_nav = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
79 && (clientPC.indexOf('khtml') == -1));
80 if(!document.selection && !is_nav) {
81 infoText=escapeQuotesHTML(infoText);
82 document.write("<form name='infoform' id='infoform'>"+
83 "<input size=80 id='infobox' name='infobox' value=\""+
84 infoText+"\" readonly=\"readonly\"></form>");
87 function escapeQuotes(text) {
88 var re=new RegExp("'","g");
89 text=text.replace(re,"\\'");
90 re=new RegExp('"',"g");
91 text=text.replace(re,'"');
92 re=new RegExp("\\n","g");
93 text=text.replace(re,"\\n");
96 function escapeQuotesHTML(text) {
97 var re=new RegExp('"',"g");
98 text=text.replace(re,""");
101 function unescapeSpecial(text) {
103 var re=new RegExp('%0A',"g");
104 text = text.replace(re,'\n');
105 var re=new RegExp('%22',"g");
106 text = text.replace(re,'"');
107 var re=new RegExp('%27',"g");
108 text = text.replace(re,'\'');
109 var re=new RegExp('%09',"g");
110 text = text.replace(re,' ');
111 var re=new RegExp('%7C',"g");
112 text = text.replace(re,'|');
113 var re=new RegExp('%5B',"g");
114 text = text.replace(re,'[');
115 var re=new RegExp('%5D',"g");
116 text = text.replace(re,']');
117 var re=new RegExp('%5C',"g");
118 text = text.replace(re,'\\');
122 // apply tagOpen/tagClose to selection in textarea,
123 // use sampleText instead of selection if there is none
124 // copied and adapted from phpBB
125 function insertTags(tagOpen, tagClose, sampleText) {
126 //f=document.getElementById('editpage');
127 var txtarea = document.getElementById('edit-content');
128 // var txtarea = document.editpage.edit[content];
129 tagOpen = unescapeSpecial(tagOpen);
131 if(document.selection) {
132 var theSelection = document.selection.createRange().text;
133 if(!theSelection) { theSelection=sampleText;}
135 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
136 theSelection = theSelection.substring(0, theSelection.length - 1);
137 document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
139 document.selection.createRange().text = tagOpen + theSelection + tagClose;
141 // Mozilla -- this induces a scrolling bug which makes it virtually unusable
142 } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
143 var startPos = txtarea.selectionStart;
144 var endPos = txtarea.selectionEnd;
145 var scrollTop=txtarea.scrollTop;
146 var myText = (txtarea.value).substring(startPos, endPos);
147 if(!myText) { myText=sampleText;}
148 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
149 subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
151 subst = tagOpen + myText + tagClose;
153 txtarea.value = txtarea.value.substring(0, startPos) + subst + txtarea.value.substring(endPos, txtarea.value.length);
155 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
156 txtarea.selectionStart=cPos;
157 txtarea.selectionEnd=cPos;
158 txtarea.scrollTop=scrollTop;
161 // Append at the end: Some people find that annoying
162 txtarea.value += tagOpen + sampleText + tagClose;
164 //var re=new RegExp("\\n","g");
165 //tagOpen=tagOpen.replace(re,"");
166 //tagClose=tagClose.replace(re,"");
167 //document.infoform.infobox.value=tagOpen+sampleText+tagClose;
170 // reposition cursor if possible
171 if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
174 // JS_SEARCHREPLACE from walterzorn.de
175 var f, sr_undo, replacewin, undo_buffer=new Array(), undo_buffer_index=0;
177 function define_f() {
178 f=document.getElementById('editpage');
179 f.editarea=document.getElementById('edit-content');
180 sr_undo=document.getElementById('sr_undo');
184 function undo_enable(bool) {
186 sr_undo.src=uri_undo_btn;
187 sr_undo.alt=msg_undo_alt;
188 sr_undo.disabled = false;
190 sr_undo.src=uri_undo_d_btn;
191 sr_undo.alt=msg_undo_d_alt;
192 sr_undo.disabled = true;
193 if(sr_undo.blur) sr_undo.blur();
199 var h = (screen.height-height)/2;
200 var w = (screen.width-width)/2;
201 replacewin = window.open('','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,top='+h+',left='+w+',height='+height+',width='+width);
202 // Close the window with Escape key
203 replacewin.onkeydown = function(e){
204 if(e.keyCode === 27){
205 replacewin.window.close();
208 replacewin.window.document.writeln(doctype);
209 replacewin.window.document.writeln('<html>\n<head>\n<title>'+msg_repl_title+'</title>');
210 replacewin.window.document.writeln(cssfile);
211 replacewin.window.document.writeln('</head>');
212 replacewin.window.document.writeln("<body onload=\"if(document.forms[0].searchinput.focus) document.forms[0].searchinput.focus(); return false;\">\n<form action=\"\">\n<center>\n<table>\n<tr>\n<td align=\"right\">"+msg_repl_search+":\n</td>\n<td align=\"left\">\n<input type=\"text\" name=\"searchinput\" size=\"45\" maxlength=\"500\" />\n</td>\n</tr>\n<tr>\n<td align=\"right\">"+msg_repl_replace_with+":\n</td>\n<td align=\"left\">\n<input type=\"text\" name=\"replaceinput\" size=\"45\" maxlength=\"500\" />\n</td>\n</tr>\n<tr>\n<td colspan=\"2\" align=\"center\">\n<input type=\"button\" value=\" "+msg_repl_ok+" \" onclick=\"if(self.opener)self.opener.do_replace(); return false;\" /> <input type=\"button\" value=\""+msg_repl_close+"\" onclick=\"self.close(); return false;\" />\n</td>\n</tr>\n</table>\n</center>\n</form>\n</body>\n</html>");
213 replacewin.window.document.close();
216 function do_replace() {
217 var txt = undo_buffer[undo_buffer_index]=f.editarea.value;
218 var searchinput = new RegExp(replacewin.document.forms[0].searchinput.value,'g');
219 var replaceinput = replacewin.document.forms[0].replaceinput.value;
220 if (searchinput==''||searchinput==null) {
221 if (replacewin) replacewin.window.document.forms[0].searchinput.focus();
224 var z_repl=txt.match(searchinput)? txt.match(searchinput).length : 0;
225 txt=txt.replace(searchinput,replaceinput);
226 searchinput=searchinput.toString().substring(1,searchinput.toString().length-2);
227 msg_replfound = msg_replfound.replace('\1', searchinput).replace('\2', z_repl).replace('\3', replaceinput);
228 msg_replnot = msg_replnot.replace('%s', searchinput);
229 result(z_repl, msg_replfound, txt, msg_replnot);
230 replacewin.window.focus();
231 replacewin.window.document.forms[0].searchinput.focus();
234 function result(count,question,value_txt,alert_txt) {
236 if(window.confirm(question)==true) {
237 f.editarea.value=value_txt;
246 if(undo_buffer_index==0) return;
247 else if(undo_buffer_index>0) {
248 f.editarea.value=undo_buffer[undo_buffer_index-1];
249 undo_buffer[undo_buffer_index]=null;
251 if(undo_buffer_index==0) {
257 //save a snapshot in the undo buffer
258 function undo_save() {
259 undo_buffer[undo_buffer_index]=f.editarea.value;