some cleaning, tool tips added
[mediawiki.git] / stylesheets / wikibits.js
blobbfe1d8c9befa707a174352a10821a874346030e5
1 // Wikipedia JavaScript support functions
2 var noOverwrite=false; // if this is true, the toolbar will no longer overwrite the infobox when you move the mouse over individual items
3 var alertText;
4 var mozVote="";
6 // Un-trap us from framesets
7 if( window.top != window ) window.top.location = window.location;
9 // for enhanced RecentChanges
10 function toggleVisibility( _levelId, _otherId, _linkId) {
11         var thisLevel = document.getElementById( _levelId );
12         var otherLevel = document.getElementById( _otherId );
13         var linkLevel = document.getElementById( _linkId );
14         if ( thisLevel.style.display == 'none' ) {
15                 thisLevel.style.display = 'block';
16                 otherLevel.style.display = 'none';
17                 linkLevel.style.display = 'inline';
18         } else {
19                 thisLevel.style.display = 'none';
20                 otherLevel.style.display = 'inline';
21                 linkLevel.style.display = 'none';
22                 }
23         }
25 // Timezone stuff
26 // tz in format [+-]HHMM
27 function checkTimezone( tz, msg ) {
28         var localclock = new Date();
29         // returns negative offset from GMT in minutes
30         var tzRaw = localclock.getTimezoneOffset();
31         var tzHour = Math.floor( Math.abs(tzRaw) / 60);
32         var tzMin = Math.abs(tzRaw) % 60;
33         var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
34         if( tz != tzString ) {
35                 var junk = msg.split( '$1' );
36                 document.write( junk[0] + "UTC" + tzString + junk[1] );
37         }
40 // in [-]HH:MM format...
41 // won't yet work with non-even tzs
42 function fetchTimezone() {
43         // FIXME: work around Safari bug
44         var localclock = new Date();
45         // returns negative offset from GMT in minutes
46         var tzRaw = localclock.getTimezoneOffset();
47         var tzHour = Math.floor( Math.abs(tzRaw) / 60);
48         var tzMin = Math.abs(tzRaw) % 60;
49         var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
50                 ":" + ((tzMin < 10) ? "0" : "") + tzMin;
51         return tzString;
54 function guessTimezone(box) {
55         document.preferences.wpHourDiff.value = fetchTimezone();
58 function showTocToggle(show,hide) {
59         if(document.getElementById) {
60                 document.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleToc()" class="internal">' +
61                 '<span id="showlink" style="display:none;">' + show + '</span>' +
62                 '<span id="hidelink">' + hide + '</span>'
63                 + '</a>]</span>');
64         }
67 function toggleToc() {
68         var toc = document.getElementById('tocinside');
69         var showlink=document.getElementById('showlink');
70         var hidelink=document.getElementById('hidelink');
71         if(toc.style.display == 'none') {
72                 toc.style.display = tocWas;
73                 hidelink.style.display='';
74                 showlink.style.display='none';
76         } else {
77                 tocWas = toc.style.display;
78                 toc.style.display = 'none';
79                 hidelink.style.display='none';
80                 showlink.style.display='';
82         }
85 // this function generates the actual toolbar buttons with localized text
86 // we use it to avoid creating the toolbar where javascript is not enabled
87 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
89         speedTip=escapeQuotes(speedTip);
90         tagOpen=escapeQuotes(tagOpen);
91         tagClose=escapeQuotes(tagClose);
92         sampleText=escapeQuotes(sampleText);
93         var mouseOver="";
95         // we can't change the selection, so we show example texts
96         // when moving the mouse instead, until the first button is clicked
97         if(!document.selection) {
98                 // filter backslashes so it can be shown in the infobox         
99                 var re=new RegExp("\\\\n","g");
100                 tagOpen=tagOpen.replace(re,"");
101                 tagClose=tagClose.replace(re,"");
102                 mouseOver = "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen+sampleText+tagClose+"'};\"";
103         }
105         document.write("<a href=\"#\" onclick=\"javascript:insertTags");
106         document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");
108         document.write("<img width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" ALT=\""+speedTip+"\" TITLE=\""+speedTip+"\""+mouseOver+">");
109         document.write("</a>");
110         return;
113 function addInfobox(infoText,text_alert,text_moz) {
114         alertText=text_alert;
115         var clientPC = navigator.userAgent.toLowerCase(); // Get client info
116         if(clientPC.indexOf('gecko')!=-1) { mozVote=text_moz; }
118         var re=new RegExp("\\\\n","g");
119         alertText=alertText.replace(re,"\n");
120         mozVote=mozVote.replace(re,"\n");
122         // if no support for changing selection, add a small copy & paste field
123         var clientPC = navigator.userAgent.toLowerCase(); // Get client info
124         /*var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
125                 && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
126                 && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1)
127                 && (clientPC.indexOf('khtml')==-1));*/
129         // document.selection is an IE property. If it is not available, we generates
130         // an infobox used by the toolbar in other browsers.
131         if(!document.selection) {
132                 infoText=escapeQuotesHTML(infoText);
133                 document.write("<form name='infoform' id='infoform'>"+
134                         "<input size=80 id='infobox' name='infobox' value=\""+
135                         infoText+"\" READONLY></form>");
136         }
140 function escapeQuotes(text) {
141         var re=new RegExp("'","g");
142         text=text.replace(re,"\\'");
143         re=new RegExp('"',"g");
144         text=text.replace(re,'&quot;');
145         re=new RegExp("\\n","g");
146         text=text.replace(re,"\\n");
147         return text;
150 function escapeQuotesHTML(text) {
151         var re=new RegExp('"',"g");
152         text=text.replace(re,"&quot;");
153         return text;
156 // apply tagOpen/tagClose to selection in textarea,
157 // use sampleText instead of selection if there is none
158 // copied and adapted from phpBB
159 function insertTags(tagOpen, tagClose, sampleText) {
161         var txtarea = document.editform.wpTextbox1;
162         // IE
163         if(document.selection) {
164                 var theSelection = document.selection.createRange().text;
165                 if(!theSelection) { theSelection=sampleText;}
166                 txtarea.focus();
167                 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
168                         theSelection = theSelection.substring(0, theSelection.length - 1);
169                         document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
170                 } else {
171                         document.selection.createRange().text = tagOpen + theSelection + tagClose;
172                 }
173         // Mozilla -- disabled because it induces a scrolling bug which makes it virtually unusable
174         //
175         /*
176         } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
177                 var startPos = txtarea.selectionStart;
178                 var endPos = txtarea.selectionEnd;
179                 var myText = (txtarea.value).substring(startPos, endPos);
180                 if(!myText) { myText=sampleText;}
181                 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
182                         subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
183                 } else {
184                         subst = tagOpen + myText + tagClose;
185                 }
186                 txtarea.value = txtarea.value.substring(0, startPos) + subst + txtarea.value.substring(endPos, txtarea.value.length);
187                 txtarea.focus();
188                 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
189                 txtarea.selectionStart=cPos;
190                 txtarea.selectionEnd=cPos;
191         */
192         // All others
193         } else {
194                 var copy_alertText=alertText;
195                 var re1=new RegExp("\\$1","g");
196                 var re2=new RegExp("\\$2","g");
197                 copy_alertText=copy_alertText.replace(re1,sampleText);
198                 copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
199                 var text;
200                 if (sampleText) {
201                         text=prompt(copy_alertText+mozVote);
202                 } else {
203                         text="";
204                 }
205                 if(!text) { text=sampleText;}
206                 text=tagOpen+text+tagClose;
207                 document.infoform.infobox.value=text;
208                 txtarea.focus();
209                 noOverwrite=true;
210         }
211         // reposition cursor if possible
212         if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();