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