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