]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - themes/default/toolbar.js
add AddPlugin support
[SourceForge/phpwiki.git] / themes / default / toolbar.js
1 // Toolbar JavaScript support functions. Taken from mediawiki 
2 // $Id: toolbar.js,v 1.9 2005-01-25 07:17:32 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 // This function generates a pulldown list to select from.
29 // plugins, pagenames, categories. 
30 // not with document.write because we cannot use self.opener then.
31 //function addPulldown(imageFile, speedTip, pages) {
32 //  addButton(imageFile, speedTip, "showPulldown", pages);
33 //  return;
34 //}
35 // pages is either an array of strings or an array of array(name,value)
36 function showPulldown(title, pages, okbutton, closebutton) {
37    pullwin = window.open('','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,height=270,width=180');
38    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;} input {font-weight:bold} option {font-size:9pt}  --'+'></style></head><body bgcolor=\"#dddddd\"><form><select name=\"select\" size=\"'+((pages.length>20)?'20':new String(pages.length))+'\" ondblclick=\"if(self.opener)self.opener.do_pulldown(document.forms[0].select.value); return false;\">');
39    for (i=0; i<pages.length; i++){
40      if (pages[i][1])
41        pullwin.window.document.write('<option value="'+pages[i][1]+'">'+escapeQuotes(pages[i][0])+'</option>\n');
42      else
43        pullwin.window.document.write('<option value="'+pages[i]+'">'+escapeQuotes(pages[i])+'</option>\n');
44    }
45    pullwin.window.document.write('</select><br /><input type=\"button\" value=\"'+okbutton+'\" onclick=\"if(self.opener)self.opener.do_pulldown(document.forms[0].select.value); return false;\"><input type=\"button\" value=\"'+closebutton+'\" onclick=\"self.close(); return false;\"></form></body></html>');
46    pullwin.window.document.close();
47    return false;
48 }
49 function do_pulldown(value) {
50   insertTags(value, '', '\n');
51   return;
52 }
53 function addInfobox(infoText) {
54   // if no support for changing selection, add a small copy & paste field
55   var clientPC = navigator.userAgent.toLowerCase(); // Get client info
56   var is_nav = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
57                 && (clientPC.indexOf('khtml') == -1));
58   if(!document.selection && !is_nav) {
59     infoText=escapeQuotesHTML(infoText);
60     document.write("<form name='infoform' id='infoform'>"+
61                    "<input size=80 id='infobox' name='infobox' value=\""+
62                    infoText+"\" readonly=\"readonly\"></form>");
63   }
64 }
65 function escapeQuotes(text) {
66   var re=new RegExp("'","g");
67   text=text.replace(re,"\\'");
68   re=new RegExp('"',"g");
69   text=text.replace(re,'&quot;');
70   re=new RegExp("\\n","g");
71   text=text.replace(re,"\\n");
72   return text;
73 }
74 function escapeQuotesHTML(text) {
75   var re=new RegExp('"',"g");
76   text=text.replace(re,"&quot;");
77   return text;
78 }
79 // apply tagOpen/tagClose to selection in textarea,
80 // use sampleText instead of selection if there is none
81 // copied and adapted from phpBB
82 function insertTags(tagOpen, tagClose, sampleText) {
83   //f=document.getElementById('editpage');
84   var txtarea = document.getElementById('edit[content]');
85   // var txtarea = document.editpage.edit[content];
86   
87   // IE
88   if(document.selection) {
89     var theSelection = document.selection.createRange().text;
90     if(!theSelection) { theSelection=sampleText;}
91     txtarea.focus();
92     if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
93       theSelection = theSelection.substring(0, theSelection.length - 1);
94       document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
95     } else {
96       document.selection.createRange().text = tagOpen + theSelection + tagClose;
97     }
98     // Mozilla -- disabled because it induces a scrolling bug which makes it virtually unusable
99   } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
100     var startPos = txtarea.selectionStart;
101     var endPos = txtarea.selectionEnd;
102     var scrollTop=txtarea.scrollTop;
103     var myText = (txtarea.value).substring(startPos, endPos);
104     if(!myText) { myText=sampleText;}
105     if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
106       subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " "; 
107     } else {
108       subst = tagOpen + myText + tagClose; 
109     }
110     txtarea.value = txtarea.value.substring(0, startPos) + subst + txtarea.value.substring(endPos, txtarea.value.length);
111     txtarea.focus();
112     var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
113     txtarea.selectionStart=cPos;
114     txtarea.selectionEnd=cPos;
115     txtarea.scrollTop=scrollTop;
116     // All others
117   } else {
118     // Append at the end: Some people find that annoying
119     txtarea.value += tagOpen + sampleText + tagClose;
120     //txtarea.focus();
121     //var re=new RegExp("\\n","g");
122     //tagOpen=tagOpen.replace(re,"");
123     //tagClose=tagClose.replace(re,"");
124     //document.infoform.infobox.value=tagOpen+sampleText+tagClose;
125     txtarea.focus();
126   }
127   // reposition cursor if possible
128   if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
129 }