]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - themes/default/toolbar.js
Update WikiAdminSearchReplace plugin
[SourceForge/phpwiki.git] / themes / default / toolbar.js
1 // Toolbar JavaScript support functions. Taken from mediawiki 
2
3 // Some "constants"
4 var doctype = '<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
5 var cssfile = '<link rel="stylesheet" type="text/css" href="'+data_path+'/themes/default/toolbar.css" />';
6
7 // Un-trap us from framesets
8 if( window.top != window ) window.top.location = window.location;
9 var pullwin;
10
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) {
15   var i;
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])+"'");
21   }
22   //width=\"23\" height=\"22\"
23   document.write(");\"><img src=\""+imageFile+"\" width=\"18\" height=\"18\" border=\"0\" alt=\""+speedTip+"\" title=\""+speedTip+"\">");
24   document.write("</a>");
25   return;
26 }
27 function addTagButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
28   addButton(imageFile, speedTip, "insertTags", [tagOpen, tagClose, sampleText]);
29   return;
30 }
31
32 // This function generates a popup list to select from. 
33 // In an external window so far, but we really want that as acdropdown pulldown, hence the name.
34 //   plugins, pagenames, categories, templates. 
35 // Not with document.write because we cannot use self.opener then.
36 //function addPulldown(imageFile, speedTip, pages) {
37 //  addButton(imageFile, speedTip, "showPulldown", pages);
38 //  return;
39 //}
40 // pages is either an array of strings or an array of array(name,value)
41 function showPulldown(title, pages, okbutton, closebutton, fromid) {
42   var height = new String(Math.min(315, 80 + (pages.length * 12))); // 270 or smaller
43   var width = 500;
44   var h = (screen.height-height)/2;
45   var w = (screen.width-width)/2;
46   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);
47   pullwin.window.document.writeln(doctype);
48   pullwin.window.document.writeln('<html>\n<head>\n<title>'+escapeQuotes(title)+'</title>');
49   pullwin.window.document.writeln(cssfile);
50   pullwin.window.document.writeln('</head>\n<body>');
51   pullwin.window.document.writeln('<p>\nYou can double-click to insert.\n</p>');
52   pullwin.window.document.writeln('<form action=\"\"><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;\">');
53   for (i=0; i<pages.length; i++){
54     if (typeof pages[i] == 'string')
55       pullwin.window.document.write('<option value="'+pages[i]+'">'+escapeQuotes(pages[i])+'</option>\n');
56     else  // array=object
57       pullwin.window.document.write('<option value="'+pages[i][1]+'">'+escapeQuotes(pages[i][0])+'</option>\n');
58   }
59   pullwin.window.document.writeln('</select>\n</div>\n</form>\n</body>\n</html>');
60   pullwin.window.document.close();
61   return false;
62 }
63 function do_pulldown(text,fromid) {
64     // do special actions dependent on fromid: tb-categories
65     if (fromid == 'tb-categories') {
66         var txtarea = document.getElementById('edit-content');
67         text = unescapeSpecial(text);
68         txtarea.value += '\n'+text;
69     } else {
70         insertTags(text, '', '\n');
71     }
72     return;
73 }
74 function addInfobox(infoText) {
75   // if no support for changing selection, add a small copy & paste field
76   var clientPC = navigator.userAgent.toLowerCase(); // Get client info
77   var is_nav = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
78                 && (clientPC.indexOf('khtml') == -1));
79   if(!document.selection && !is_nav) {
80     infoText=escapeQuotesHTML(infoText);
81     document.write("<form name='infoform' id='infoform'>"+
82                    "<input size=80 id='infobox' name='infobox' value=\""+
83                    infoText+"\" readonly=\"readonly\"></form>");
84   }
85 }
86 function escapeQuotes(text) {
87   var re=new RegExp("'","g");
88   text=text.replace(re,"\\'");
89   re=new RegExp('"',"g");
90   text=text.replace(re,'&quot;');
91   re=new RegExp("\\n","g");
92   text=text.replace(re,"\\n");
93   return text;
94 }
95 function escapeQuotesHTML(text) {
96   var re=new RegExp('"',"g");
97   text=text.replace(re,"&quot;");
98   return text;
99 }
100 function unescapeSpecial(text) {
101     // IE
102     var re=new RegExp('%0A',"g");
103     text = text.replace(re,'\n');
104     var re=new RegExp('%22',"g");
105     text = text.replace(re,'"');
106     var re=new RegExp('%27',"g");
107     text = text.replace(re,'\'');
108     var re=new RegExp('%09',"g");
109     text = text.replace(re,'    ');
110     var re=new RegExp('%7C',"g");
111     text = text.replace(re,'|');
112     var re=new RegExp('%5B',"g");
113     text = text.replace(re,'[');
114     var re=new RegExp('%5D',"g");
115     text = text.replace(re,']');
116     var re=new RegExp('%5C',"g");
117     text = text.replace(re,'\\');
118     return text;
119 }
120
121 // apply tagOpen/tagClose to selection in textarea,
122 // use sampleText instead of selection if there is none
123 // copied and adapted from phpBB
124 function insertTags(tagOpen, tagClose, sampleText) {
125   //f=document.getElementById('editpage');
126   var txtarea = document.getElementById('edit-content');
127   // var txtarea = document.editpage.edit[content];
128   tagOpen = unescapeSpecial(tagOpen);
129
130   if(document.selection) {
131     var theSelection = document.selection.createRange().text;
132     if(!theSelection) { theSelection=sampleText;}
133     txtarea.focus();
134     if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
135       theSelection = theSelection.substring(0, theSelection.length - 1);
136       document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
137     } else {
138       document.selection.createRange().text = tagOpen + theSelection + tagClose;
139     }
140     // Mozilla -- this induces a scrolling bug which makes it virtually unusable
141   } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
142     var startPos = txtarea.selectionStart;
143     var endPos = txtarea.selectionEnd;
144     var scrollTop=txtarea.scrollTop;
145     var myText = (txtarea.value).substring(startPos, endPos);
146     if(!myText) { myText=sampleText;}
147     if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
148       subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " "; 
149     } else {
150       subst = tagOpen + myText + tagClose; 
151     }
152     txtarea.value = txtarea.value.substring(0, startPos) + subst + txtarea.value.substring(endPos, txtarea.value.length);
153     txtarea.focus();
154     var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
155     txtarea.selectionStart=cPos;
156     txtarea.selectionEnd=cPos;
157     txtarea.scrollTop=scrollTop;
158     // All others
159   } else {
160     // Append at the end: Some people find that annoying
161     txtarea.value += tagOpen + sampleText + tagClose;
162     //txtarea.focus();
163     //var re=new RegExp("\\n","g");
164     //tagOpen=tagOpen.replace(re,"");
165     //tagClose=tagClose.replace(re,"");
166     //document.infoform.infobox.value=tagOpen+sampleText+tagClose;
167     txtarea.focus();
168   }
169   // reposition cursor if possible
170   if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
171 }
172
173 // JS_SEARCHREPLACE from walterzorn.de
174 var f, sr_undo, replacewin, undo_buffer=new Array(), undo_buffer_index=0;
175
176 function define_f() {
177    f=document.getElementById('editpage');
178    f.editarea=document.getElementById('edit-content');
179    sr_undo=document.getElementById('sr_undo');
180    undo_enable(false);
181    f.editarea.focus();
182 }
183 function undo_enable(bool) {
184    if (bool) {
185      sr_undo.src=uri_undo_btn;
186      sr_undo.alt=msg_undo_alt;
187      sr_undo.disabled = false;
188    } else {
189        sr_undo.src=uri_undo_d_btn;
190        sr_undo.alt=msg_undo_d_alt;
191        sr_undo.disabled = true;
192        if(sr_undo.blur) sr_undo.blur();
193    }
194 }
195 function replace() {
196    var height = 120;
197    var width = 600;
198    var h = (screen.height-height)/2;
199    var w = (screen.width-width)/2;
200    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);
201    replacewin.window.document.writeln(doctype);
202    replacewin.window.document.writeln('<html>\n<head>\n<title>'+msg_repl_title+'</title>');
203    replacewin.window.document.writeln(cssfile);
204    replacewin.window.document.writeln('</head>');
205    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;\" />&nbsp;&nbsp;&nbsp;<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>");
206    replacewin.window.document.close();
207    return false;
208 }
209 function do_replace() {
210    var txt = undo_buffer[undo_buffer_index]=f.editarea.value;
211    var searchinput = new RegExp(replacewin.document.forms[0].searchinput.value,'g');
212    var replaceinput = replacewin.document.forms[0].replaceinput.value;
213    if (searchinput==''||searchinput==null) {
214       if (replacewin) replacewin.window.document.forms[0].searchinput.focus();
215       return;
216    }
217    var z_repl=txt.match(searchinput)? txt.match(searchinput).length : 0;
218    txt=txt.replace(searchinput,replaceinput);
219    searchinput=searchinput.toString().substring(1,searchinput.toString().length-2);
220    msg_replfound = msg_replfound.replace('\1', searchinput).replace('\2', z_repl).replace('\3', replaceinput);
221    msg_replnot = msg_replnot.replace('%s', searchinput);
222    result(z_repl, msg_replfound, txt, msg_replnot);
223    replacewin.window.focus();
224    replacewin.window.document.forms[0].searchinput.focus();
225    return false;
226 }
227 function result(count,question,value_txt,alert_txt) {
228    if (count>0) {
229       if(window.confirm(question)==true) {
230          f.editarea.value=value_txt;
231          undo_save();
232          undo_enable(true);
233       }
234    } else {
235        alert(alert_txt);
236    }
237 }
238 function do_undo() {
239    if(undo_buffer_index==0) return;
240    else if(undo_buffer_index>0) {
241       f.editarea.value=undo_buffer[undo_buffer_index-1];
242       undo_buffer[undo_buffer_index]=null;
243       undo_buffer_index--;
244       if(undo_buffer_index==0) {
245          alert(msg_do_undo);
246          undo_enable(false);
247       }
248    }
249 }
250 //save a snapshot in the undo buffer
251 function undo_save() {
252    undo_buffer[undo_buffer_index]=f.editarea.value;
253    undo_buffer_index++;
254    undo_enable(true);
255 }