]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - themes/default/toolbar.js
svn propdel svn:executable
[SourceForge/phpwiki.git] / themes / default / toolbar.js
1 // Toolbar JavaScript support functions. Taken from mediawiki 
2 // $Id$
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(315, 80 + (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=200');
41   pullwin.window.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><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 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<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;\">');
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(text,fromid) {
54     // do special actions dependent on fromid: tb-categories
55     if (fromid == 'tb-categories') {
56         var txtarea = document.getElementById('edit-content');
57         text = unescapeSpecial(text)
58         txtarea.value += '\n'+text;
59     } else {
60         insertTags(text, '', '\n');
61     }
62     return;
63 }
64 function addInfobox(infoText) {
65   // if no support for changing selection, add a small copy & paste field
66   var clientPC = navigator.userAgent.toLowerCase(); // Get client info
67   var is_nav = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
68                 && (clientPC.indexOf('khtml') == -1));
69   if(!document.selection && !is_nav) {
70     infoText=escapeQuotesHTML(infoText);
71     document.write("<form name='infoform' id='infoform'>"+
72                    "<input size=80 id='infobox' name='infobox' value=\""+
73                    infoText+"\" readonly=\"readonly\"></form>");
74   }
75 }
76 function escapeQuotes(text) {
77   var re=new RegExp("'","g");
78   text=text.replace(re,"\\'");
79   re=new RegExp('"',"g");
80   text=text.replace(re,'&quot;');
81   re=new RegExp("\\n","g");
82   text=text.replace(re,"\\n");
83   return text;
84 }
85 function escapeQuotesHTML(text) {
86   var re=new RegExp('"',"g");
87   text=text.replace(re,"&quot;");
88   return text;
89 }
90 function unescapeSpecial(text) {
91     // IE
92     var re=new RegExp('%0A',"g");
93     text = text.replace(re,'\n');
94     var re=new RegExp('%22',"g");
95     text = text.replace(re,'"');
96     var re=new RegExp('%27',"g");
97     text = text.replace(re,'\'');
98     var re=new RegExp('%09',"g");
99     text = text.replace(re,'    ');
100     var re=new RegExp('%7C',"g");
101     text = text.replace(re,'|');
102     var re=new RegExp('%5B',"g");
103     text = text.replace(re,'[');
104     var re=new RegExp('%5D',"g");
105     text = text.replace(re,']');
106     var re=new RegExp('%5C',"g");
107     text = text.replace(re,'\\');
108     return text;
109 }
110
111 // apply tagOpen/tagClose to selection in textarea,
112 // use sampleText instead of selection if there is none
113 // copied and adapted from phpBB
114 function insertTags(tagOpen, tagClose, sampleText) {
115   //f=document.getElementById('editpage');
116   var txtarea = document.getElementById('edit-content');
117   // var txtarea = document.editpage.edit[content];
118   tagOpen = unescapeSpecial(tagOpen)
119
120   if(document.selection) {
121     var theSelection = document.selection.createRange().text;
122     if(!theSelection) { theSelection=sampleText;}
123     txtarea.focus();
124     if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
125       theSelection = theSelection.substring(0, theSelection.length - 1);
126       document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
127     } else {
128       document.selection.createRange().text = tagOpen + theSelection + tagClose;
129     }
130     // Mozilla -- this induces a scrolling bug which makes it virtually unusable
131   } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
132     var startPos = txtarea.selectionStart;
133     var endPos = txtarea.selectionEnd;
134     var scrollTop=txtarea.scrollTop;
135     var myText = (txtarea.value).substring(startPos, endPos);
136     if(!myText) { myText=sampleText;}
137     if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
138       subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " "; 
139     } else {
140       subst = tagOpen + myText + tagClose; 
141     }
142     txtarea.value = txtarea.value.substring(0, startPos) + subst + txtarea.value.substring(endPos, txtarea.value.length);
143     txtarea.focus();
144     var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
145     txtarea.selectionStart=cPos;
146     txtarea.selectionEnd=cPos;
147     txtarea.scrollTop=scrollTop;
148     // All others
149   } else {
150     // Append at the end: Some people find that annoying
151     txtarea.value += tagOpen + sampleText + tagClose;
152     //txtarea.focus();
153     //var re=new RegExp("\\n","g");
154     //tagOpen=tagOpen.replace(re,"");
155     //tagClose=tagClose.replace(re,"");
156     //document.infoform.infobox.value=tagOpen+sampleText+tagClose;
157     txtarea.focus();
158   }
159   // reposition cursor if possible
160   if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
161 }
162
163 // JS_SEARCHREPLACE from walterzorn.de
164 var f, sr_undo, replacewin, undo_buffer=new Array(), undo_buffer_index=0;
165
166 function define_f() {
167    f=document.getElementById('editpage')
168    f.editarea=document.getElementById('edit-content')
169    sr_undo=document.getElementById('sr_undo')
170    undo_enable(false)
171    f.editarea.focus()
172 }
173 function undo_enable(bool) {
174    if (bool) {
175      sr_undo.src=uri_undo_btn
176      sr_undo.alt=msg_undo_alt
177      sr_undo.disabled = false
178    } else {
179        sr_undo.src=uri_undo_d_btn
180        sr_undo.alt=msg_undo_d_alt
181        sr_undo.disabled = true
182        if(sr_undo.blur) sr_undo.blur();
183    }
184 }
185 function replace() {
186    replacewin = window.open('','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,height=90,width=450');
187    replacewin.window.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><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 action=\"\"><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>");
188    replacewin.window.document.close();
189    return false;
190 }
191 function do_replace() {
192    var txt = undo_buffer[undo_buffer_index]=f.editarea.value
193    var searchinput = new RegExp(replacewin.document.forms[0].searchinput.value,'g')
194    var replaceinput = replacewin.document.forms[0].replaceinput.value
195    if (searchinput==''||searchinput==null) {
196       if (replacewin) replacewin.window.document.forms[0].searchinput.focus();
197       return;
198    }
199    var z_repl=txt.match(searchinput)? txt.match(searchinput).length : 0;
200    txt=txt.replace(searchinput,replaceinput);
201    searchinput=searchinput.toString().substring(1,searchinput.toString().length-2);
202    msg_replfound = msg_replfound.replace('\1', searchinput).replace('\2', z_repl).replace('\3', replaceinput)
203    msg_replnot = msg_replnot.replace('%s', searchinput)
204    result(z_repl, msg_replfound, txt, msg_replnot);
205    replacewin.window.focus();
206    replacewin.window.document.forms[0].searchinput.focus();
207    return false;
208 }
209 function result(count,question,value_txt,alert_txt) {
210    if (count>0) {
211       if(window.confirm(question)==true) {
212          f.editarea.value=value_txt;
213          undo_save();
214          undo_enable(true);
215       }
216    } else {
217        alert(alert_txt);
218    }
219 }
220 function do_undo() {
221    if(undo_buffer_index==0) return;
222    else if(undo_buffer_index>0) {
223       f.editarea.value=undo_buffer[undo_buffer_index-1];
224       undo_buffer[undo_buffer_index]=null;
225       undo_buffer_index--;
226       if(undo_buffer_index==0) {
227          alert(msg_do_undo);
228          undo_enable(false);
229       }
230    }
231 }
232 //save a snapshot in the undo buffer
233 function undo_save() {
234    undo_buffer[undo_buffer_index]=f.editarea.value;
235    undo_buffer_index++;
236    undo_enable(true);
237 }