]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - themes/default/toolbar.js
Move search&replace js from body (defined in EditToolbar) to the toolbar.js. Add...
[SourceForge/phpwiki.git] / themes / default / toolbar.js
1 // Toolbar JavaScript support functions. Taken from mediawiki 
2 // $Id: toolbar.js,v 1.13 2007-01-07 18:48:10 rurban Exp $
3
4 // Un-trap us from framesets
5 if( window.top != window ) window.top.location = window.location;
6 var pullwin;
7
8 // This function generates the actual toolbar buttons with localized text
9 // We use it to avoid creating the toolbar where javascript is not enabled
10 // Not all buttons use this helper, some need special javascript treatment.
11 function addButton(imageFile, speedTip, func, args) {
12   var i;
13   speedTip=escapeQuotes(speedTip);
14   document.write("<a href=\"javascript:"+func+"(");
15   for (i=0; i<args.length; i++){
16     if (i>0) document.write(",");
17     document.write("'"+escapeQuotes(args[i])+"'");
18   }
19   //width=\"23\" height=\"22\"
20   document.write(");\"><img src=\""+imageFile+"\" width=\"18\" height=\"18\" border=\"0\" alt=\""+speedTip+"\" title=\""+speedTip+"\">");
21   document.write("</a>");
22   return;
23 }
24 function addTagButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
25   addButton(imageFile, speedTip, "insertTags", [tagOpen, tagClose, sampleText]);
26   return;
27 }
28
29 // This function generates a popup list to select from. 
30 // In an external window so far, but we really want that as acdropdown pulldown, hence the name.
31 //   plugins, pagenames, categories, templates. 
32 // Not with document.write because we cannot use self.opener then.
33 //function addPulldown(imageFile, speedTip, pages) {
34 //  addButton(imageFile, speedTip, "showPulldown", pages);
35 //  return;
36 //}
37 // pages is either an array of strings or an array of array(name,value)
38 function showPulldown(title, pages, okbutton, closebutton, fromid) {
39   height = new String(Math.min(270, 70 + (pages.length * 12))); // 270 or smaller
40   pullwin = window.open('','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,height='+height+',width=180');
41   pullwin.window.document.write('<html><head><title>'+escapeQuotes(title)+'</title><style type=\"text/css\"><'+'!'+'-- body {font-family:Tahoma,Arial,Helvetica,sans-serif;font-size:10pt;background-color:#dddddd;} input { font-weight:bold;margin-left:2px;margin-right:2px;} option {font-size:9pt} #buttons { background-color:#dddddd;padding-right:10px;width:180px;} --'+'></style></head>');
42   pullwin.window.document.write('\n<body bgcolor=\"#dddddd\"><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<select style=\"margin-top:10px;\" 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;\">');
43   for (i=0; i<pages.length; i++){
44     if (typeof pages[i] == 'string')
45       pullwin.window.document.write('<option value="'+pages[i]+'">'+escapeQuotes(pages[i])+'</option>\n');
46     else  // array=object
47       pullwin.window.document.write('<option value="'+pages[i][1]+'">'+escapeQuotes(pages[i][0])+'</option>\n');
48   }
49   pullwin.window.document.write('</select></form></body></html>');
50   pullwin.window.document.close();
51   return false;
52 }
53 function do_pulldown(value,fromid) {
54     // do special actions dependent on fromid: tb-categories
55     if (fromid == 'tb-categories') {
56         var txtarea = document.getElementById('edit-content');
57         txtarea.value += '\n'+value;
58     } else {
59         insertTags(value, '', '\n');
60     }
61     return;
62 }
63 function addInfobox(infoText) {
64   // if no support for changing selection, add a small copy & paste field
65   var clientPC = navigator.userAgent.toLowerCase(); // Get client info
66   var is_nav = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
67                 && (clientPC.indexOf('khtml') == -1));
68   if(!document.selection && !is_nav) {
69     infoText=escapeQuotesHTML(infoText);
70     document.write("<form name='infoform' id='infoform'>"+
71                    "<input size=80 id='infobox' name='infobox' value=\""+
72                    infoText+"\" readonly=\"readonly\"></form>");
73   }
74 }
75 function escapeQuotes(text) {
76   var re=new RegExp("'","g");
77   text=text.replace(re,"\\'");
78   re=new RegExp('"',"g");
79   text=text.replace(re,'&quot;');
80   re=new RegExp("\\n","g");
81   text=text.replace(re,"\\n");
82   return text;
83 }
84 function escapeQuotesHTML(text) {
85   var re=new RegExp('"',"g");
86   text=text.replace(re,"&quot;");
87   return text;
88 }
89 // apply tagOpen/tagClose to selection in textarea,
90 // use sampleText instead of selection if there is none
91 // copied and adapted from phpBB
92 function insertTags(tagOpen, tagClose, sampleText) {
93   //f=document.getElementById('editpage');
94   var txtarea = document.getElementById('edit-content');
95   // var txtarea = document.editpage.edit[content];
96   
97   // IE
98   var re=new RegExp('%0A',"g");
99   tagOpen = tagOpen.replace(re,'\n');
100   var re=new RegExp('%22',"g");
101   tagOpen = tagOpen.replace(re,'"');
102   var re=new RegExp('%27',"g");
103   tagOpen = tagOpen.replace(re,'\'');
104   var re=new RegExp('%09',"g");
105   tagOpen = tagOpen.replace(re,'    ');
106   var re=new RegExp('%7C',"g");
107   tagOpen = tagOpen.replace(re,'|');
108   var re=new RegExp('%5B',"g");
109   tagOpen = tagOpen.replace(re,'[');
110   var re=new RegExp('%5D',"g");
111   tagOpen = tagOpen.replace(re,']');
112   var re=new RegExp('%5C',"g");
113   tagOpen = tagOpen.replace(re,'\\');
114
115   if(document.selection) {
116     var theSelection = document.selection.createRange().text;
117     if(!theSelection) { theSelection=sampleText;}
118     txtarea.focus();
119     if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
120       theSelection = theSelection.substring(0, theSelection.length - 1);
121       document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
122     } else {
123       document.selection.createRange().text = tagOpen + theSelection + tagClose;
124     }
125     // Mozilla -- this induces a scrolling bug which makes it virtually unusable
126   } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
127     var startPos = txtarea.selectionStart;
128     var endPos = txtarea.selectionEnd;
129     var scrollTop=txtarea.scrollTop;
130     var myText = (txtarea.value).substring(startPos, endPos);
131     if(!myText) { myText=sampleText;}
132     if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
133       subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " "; 
134     } else {
135       subst = tagOpen + myText + tagClose; 
136     }
137     txtarea.value = txtarea.value.substring(0, startPos) + subst + txtarea.value.substring(endPos, txtarea.value.length);
138     txtarea.focus();
139     var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
140     txtarea.selectionStart=cPos;
141     txtarea.selectionEnd=cPos;
142     txtarea.scrollTop=scrollTop;
143     // All others
144   } else {
145     // Append at the end: Some people find that annoying
146     txtarea.value += tagOpen + sampleText + tagClose;
147     //txtarea.focus();
148     //var re=new RegExp("\\n","g");
149     //tagOpen=tagOpen.replace(re,"");
150     //tagClose=tagClose.replace(re,"");
151     //document.infoform.infobox.value=tagOpen+sampleText+tagClose;
152     txtarea.focus();
153   }
154   // reposition cursor if possible
155   if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
156 }
157
158 // JS_SEARCHREPLACE from walterzorn.de
159 var f, sr_undo, replacewin, undo_buffer=new Array(), undo_buffer_index=0;
160
161 function define_f() {
162    f=document.getElementById('editpage')
163    f.editarea=document.getElementById('edit-content')
164    sr_undo=document.getElementById('sr_undo')
165    undo_enable(false)
166    f.editarea.focus()
167 }
168 function undo_enable(bool) {
169    if (bool) {
170      sr_undo.src=uri_undo_btn
171      sr_undo.alt=msg_undo_alt
172      sr_undo.disabled = false
173    } else {
174        sr_undo.src=uri_undo_d_btn
175        sr_undo.alt=msg_undo_d_alt
176        sr_undo.disabled = true
177        if(sr_undo.blur) sr_undo.blur();
178    }
179 }
180 function replace() {
181    replacewin = window.open('','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,height=90,width=450');
182    replacewin.window.document.write('<html><head><title>'+msg_repl_title+"</title><style type=\"text/css\"><'+'!'+'-- body, input {font-family:Tahoma,Arial,Helvetica,sans-serif;font-size:10pt;font-weight:bold;} td {font-size:9pt}  --'+'></style></head><body bgcolor=\"#dddddd\" onload=\"if(document.forms[0].searchinput.focus) document.forms[0].searchinput.focus(); return false;\"><form><center><table><tr><td align=\"right\">"+msg_repl_search+":</td><td align=\"left\"><input type=\"text\" name=\"searchinput\" size=\"45\" maxlength=\"500\"></td></tr><tr><td align=\"right\">"+msg_repl_replace_with+":</td><td align=\"left\"><input type=\"text\" name=\"replaceinput\" size=\"45\" maxlength=\"500\"></td></tr><tr><td colspan=\"2\" align=\"center\"><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;\"></td></tr></table></center></form></body></html>");
183    replacewin.window.document.close();
184    return false;
185 }
186 function do_replace() {
187    var txt = undo_buffer[undo_buffer_index]=f.editarea.value
188    var searchinput = new RegExp(replacewin.document.forms[0].searchinput.value,'g')
189    var replaceinput = replacewin.document.forms[0].replaceinput.value
190    if (searchinput==''||searchinput==null) {
191       if (replacewin) replacewin.window.document.forms[0].searchinput.focus();
192       return;
193    }
194    var z_repl=txt.match(searchinput)? txt.match(searchinput).length : 0;
195    txt=txt.replace(searchinput,replaceinput);
196    searchinput=searchinput.toString().substring(1,searchinput.toString().length-2);
197    msg_replfound = msg_replfound.replace('\1', searchinput).replace('\2', z_repl).replace('\3', replaceinput)
198    msg_replnot = msg_replnot.replace('%s', searchinput)
199    result(z_repl, msg_replfound, txt, msg_replnot);
200    replacewin.window.focus();
201    replacewin.window.document.forms[0].searchinput.focus();
202    return false;
203 }
204 function result(count,question,value_txt,alert_txt) {
205    if (count>0) {
206       if(window.confirm(question)==true) {
207          f.editarea.value=value_txt;
208          undo_save();
209          undo_enable(true);
210       }
211    } else {
212        alert(alert_txt);
213    }
214 }
215 function do_undo() {
216    if(undo_buffer_index==0) return;
217    else if(undo_buffer_index>0) {
218       f.editarea.value=undo_buffer[undo_buffer_index-1];
219       undo_buffer[undo_buffer_index]=null;
220       undo_buffer_index--;
221       if(undo_buffer_index==0) {
222          alert(msg_do_undo);
223          undo_enable(false);
224       }
225    }
226 }
227 //save a snapshot in the undo buffer
228 function undo_save() {
229    undo_buffer[undo_buffer_index]=f.editarea.value;
230    undo_buffer_index++;
231    undo_enable(true);
232 }