]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - themes/default/toolbar.js
Declare Javascript variables
[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 {
65         insertTags(text, '', '\n');
66     }
67 }
68 function escapeQuotes(text) {
69   var re=new RegExp("'","g");
70   text=text.replace(re,"\\'");
71   re=new RegExp('"',"g");
72   text=text.replace(re,'&quot;');
73   re=new RegExp("\\n","g");
74   text=text.replace(re,"\\n");
75   return text;
76 }
77 function unescapeSpecial(text) {
78     // IE
79     var re=new RegExp('%0A',"g");
80     text = text.replace(re,'\n');
81     re=new RegExp('%22',"g");
82     text = text.replace(re,'"');
83     re=new RegExp('%27',"g");
84     text = text.replace(re,'\'');
85     re=new RegExp('%09',"g");
86     text = text.replace(re,'    ');
87     re=new RegExp('%7C',"g");
88     text = text.replace(re,'|');
89     re=new RegExp('%5B',"g");
90     text = text.replace(re,'[');
91     re=new RegExp('%5D',"g");
92     text = text.replace(re,']');
93     re=new RegExp('%5C',"g");
94     text = text.replace(re,'\\');
95     return text;
96 }
97
98 // apply tagOpen/tagClose to selection in textarea,
99 // use sampleText instead of selection if there is none
100 // copied and adapted from phpBB
101 function insertTags(tagOpen, tagClose, sampleText) {
102   //f=document.getElementById('editpage');
103   var txtarea = document.getElementById('edit-content');
104   // var txtarea = document.editpage.edit[content];
105   tagOpen = unescapeSpecial(tagOpen);
106
107   if(document.selection) {
108     var theSelection = document.selection.createRange().text;
109     if(!theSelection) { theSelection=sampleText;}
110     txtarea.focus();
111     if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
112       theSelection = theSelection.substring(0, theSelection.length - 1);
113       document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
114     } else {
115       document.selection.createRange().text = tagOpen + theSelection + tagClose;
116     }
117     // Mozilla -- this induces a scrolling bug which makes it virtually unusable
118   } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
119     var startPos = txtarea.selectionStart;
120     var endPos = txtarea.selectionEnd;
121     var scrollTop=txtarea.scrollTop;
122     var myText = (txtarea.value).substring(startPos, endPos);
123     if(!myText) { myText=sampleText;}
124     var subst;
125     if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
126       subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
127     } else {
128       subst = tagOpen + myText + tagClose;
129     }
130     txtarea.value = txtarea.value.substring(0, startPos) + subst + txtarea.value.substring(endPos, txtarea.value.length);
131     txtarea.focus();
132     var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
133     txtarea.selectionStart=cPos;
134     txtarea.selectionEnd=cPos;
135     txtarea.scrollTop=scrollTop;
136     // All others
137   } else {
138     // Append at the end: Some people find that annoying
139     txtarea.value += tagOpen + sampleText + tagClose;
140     //txtarea.focus();
141     //var re=new RegExp("\\n","g");
142     //tagOpen=tagOpen.replace(re,"");
143     //tagClose=tagClose.replace(re,"");
144     //document.infoform.infobox.value=tagOpen+sampleText+tagClose;
145     txtarea.focus();
146   }
147   // reposition cursor if possible
148   if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
149 }
150
151 // JS_SEARCHREPLACE from walterzorn.de
152 var f, sr_undo, replacewin, undo_buffer=new Array(), undo_buffer_index=0;
153
154 function define_f() {
155    f=document.getElementById('editpage');
156    f.editarea=document.getElementById('edit-content');
157    sr_undo=document.getElementById('sr_undo');
158    undo_enable(false);
159    f.editarea.focus();
160 }
161 function undo_enable(bool) {
162    if (bool) {
163      sr_undo.src=uri_undo_btn;
164      sr_undo.alt=msg_undo_alt;
165      sr_undo.disabled = false;
166    } else {
167        sr_undo.src=uri_undo_d_btn;
168        sr_undo.alt=msg_undo_d_alt;
169        sr_undo.disabled = true;
170        if(sr_undo.blur) sr_undo.blur();
171    }
172 }
173 function replace() {
174    var height = 120;
175    var width = 600;
176    var h = (screen.height-height)/2;
177    var w = (screen.width-width)/2;
178    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);
179    // Close the window with Escape key
180    replacewin.onkeydown = function(e){
181        if(e.keyCode === 27){
182            replacewin.window.close();
183        }
184    };
185    replacewin.window.document.writeln(doctype);
186    replacewin.window.document.writeln('<html>\n<head>\n<title>'+msg_repl_title+'</title>');
187    replacewin.window.document.writeln(cssfile);
188    replacewin.window.document.writeln('</head>');
189    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>");
190    replacewin.window.document.close();
191    return false;
192 }
193 function do_replace() {
194    var txt = undo_buffer[undo_buffer_index]=f.editarea.value;
195    var searchinput = new RegExp(replacewin.document.forms[0].searchinput.value,'g');
196    var replaceinput = replacewin.document.forms[0].replaceinput.value;
197    if (searchinput==''||searchinput==null) {
198       if (replacewin) replacewin.window.document.forms[0].searchinput.focus();
199       return;
200    }
201    var z_repl=txt.match(searchinput)? txt.match(searchinput).length : 0;
202    txt=txt.replace(searchinput,replaceinput);
203    searchinput=searchinput.toString().substring(1,searchinput.toString().length-2);
204    var msg_replfound = msg_replfound.replace('\1', searchinput).replace('\2', z_repl).replace('\3', replaceinput);
205    var msg_replnot = msg_replnot.replace('%s', searchinput);
206    result(z_repl, msg_replfound, txt, msg_replnot);
207    replacewin.window.focus();
208    replacewin.window.document.forms[0].searchinput.focus();
209    return false;
210 }
211 function result(count,question,value_txt,alert_txt) {
212    if (count>0) {
213       if(window.confirm(question)==true) {
214          f.editarea.value=value_txt;
215          undo_save();
216          undo_enable(true);
217       }
218    } else {
219        alert(alert_txt);
220    }
221 }
222 function do_undo() {
223    if(undo_buffer_index>0) {
224       f.editarea.value=undo_buffer[undo_buffer_index-1];
225       undo_buffer[undo_buffer_index]=null;
226       undo_buffer_index--;
227       if(undo_buffer_index==0) {
228          alert(msg_do_undo);
229          undo_enable(false);
230       }
231    }
232 }
233 //save a snapshot in the undo buffer
234 function undo_save() {
235    undo_buffer[undo_buffer_index]=f.editarea.value;
236    undo_buffer_index++;
237    undo_enable(true);
238 }