]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - themes/default/toolbar.js
fixed Calendar link
[SourceForge/phpwiki.git] / themes / default / toolbar.js
1 // Toolbar JavaScript support functions from mediawiki 
2 // $Id: toolbar.js,v 1.2 2004-05-27 17:55:38 rurban Exp $ 
3
4 // Un-trap us from framesets
5 if( window.top != window ) window.top.location = window.location;
6
7 // This function generates the actual toolbar buttons with localized text
8 // We use it to avoid creating the toolbar where javascript is not enabled
9 // Not all buttons use this helper, some need special javascript treatment.
10 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
11         speedTip=escapeQuotes(speedTip);
12         tagOpen=escapeQuotes(tagOpen);
13         tagClose=escapeQuotes(tagClose);
14         sampleText=escapeQuotes(sampleText);
15         document.write("<a href=\"javascript:insertTags");
16         document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");
17         document.write("<img width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" alt=\""+speedTip+"\" title=\""+speedTip+"\">");
18         document.write("</a>");
19         return;
20 }
21
22 function addInfobox(infoText) {
23         // if no support for changing selection, add a small copy & paste field
24         var clientPC = navigator.userAgent.toLowerCase(); // Get client info
25         var is_nav = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
26                 && (clientPC.indexOf('khtml') == -1));
27         if(!document.selection && !is_nav) {
28                 infoText=escapeQuotesHTML(infoText);
29                 document.write("<form name='infoform' id='infoform'>"+
30                         "<input size=80 id='infobox' name='infobox' value=\""+
31                         infoText+"\" readonly=\"readonly\"></form>");
32         }
33 }
34
35 function escapeQuotes(text) {
36         var re=new RegExp("'","g");
37         text=text.replace(re,"\\'");
38         re=new RegExp('"',"g");
39         text=text.replace(re,'&quot;');
40         re=new RegExp("\\n","g");
41         text=text.replace(re,"\\n");
42         return text;
43 }
44
45 function escapeQuotesHTML(text) {
46         var re=new RegExp('"',"g");
47         text=text.replace(re,"&quot;");
48         return text;
49 }
50
51 // apply tagOpen/tagClose to selection in textarea,
52 // use sampleText instead of selection if there is none
53 // copied and adapted from phpBB
54 function insertTags(tagOpen, tagClose, sampleText) {
55         //f=document.getElementById('editpage');
56         var txtarea = document.getElementById('edit[content]');
57         // var txtarea = document.editpage.edit[content];
58
59         // IE
60         if(document.selection) {
61                 var theSelection = document.selection.createRange().text;
62                 if(!theSelection) { theSelection=sampleText;}
63                 txtarea.focus();
64                 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
65                         theSelection = theSelection.substring(0, theSelection.length - 1);
66                         document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
67                 } else {
68                         document.selection.createRange().text = tagOpen + theSelection + tagClose;
69                 }
70         // Mozilla -- disabled because it induces a scrolling bug which makes it virtually unusable
71         } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
72                 var startPos = txtarea.selectionStart;
73                 var endPos = txtarea.selectionEnd;
74                 var scrollTop=txtarea.scrollTop;
75                 var myText = (txtarea.value).substring(startPos, endPos);
76                 if(!myText) { myText=sampleText;}
77                 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
78                         subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " "; 
79                 } else {
80                         subst = tagOpen + myText + tagClose; 
81                 }
82                 txtarea.value = txtarea.value.substring(0, startPos) + subst + txtarea.value.substring(endPos, txtarea.value.length);
83                 txtarea.focus();
84                 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
85                 txtarea.selectionStart=cPos;
86                 txtarea.selectionEnd=cPos;
87                 txtarea.scrollTop=scrollTop;
88         // All others
89         } else {
90                 // Append at the end: Some people find that annoying
91                 //txtarea.value += tagOpen + sampleText + tagClose;
92                 //txtarea.focus();
93                 var re=new RegExp("\\n","g");
94                 tagOpen=tagOpen.replace(re,"");
95                 tagClose=tagClose.replace(re,"");
96                 document.infoform.infobox.value=tagOpen+sampleText+tagClose;
97                 txtarea.focus();
98         }
99         // reposition cursor if possible
100         if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
101 }