prefs toc/folding
[mediawiki.git] / stylesheets / wikibits.js
blob1184eb9b36991b7d7b52f4979778d5980abdcccc
1 // Wikipedia JavaScript support functions
3 // if this is true, the toolbar will no longer overwrite the infobox when you move the mouse over individual items
4 var noOverwrite=false;
5 var alertText;
6 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
7 var is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
8                 && (clientPC.indexOf('khtml') == -1));
9 var is_safari = ((clientPC.indexOf('AppleWebKit')!=-1) && (clientPC.indexOf('spoofer')==-1));
10 var is_khtml = (navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ));
11 if (clientPC.indexOf('opera')!=-1) {
12     var is_opera = true;
13     var is_opera_preseven = (window.opera && !document.childNodes);
14     var is_opera_seven = (window.opera && document.childNodes);
17 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
18 function onloadhook () {
19     // don't run anything below this for non-dom browsers
20     if(!(document.getElementById && document.getElementsByTagName)) return;
21     histrowinit();
22     tabbedprefs();
24 if (window.addEventListener) window.addEventListener("load",onloadhook,false);
25 else if (window.attachEvent) window.attachEvent("onload",onloadhook);
28 // document.write special stylesheet links
29 function addcss ( stylepath ) {
30     if (is_opera_preseven) {
31         document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'Opera6Fixes.css">');
32     } else if (is_opera_seven) {
33         document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'Opera7Fixes.css">');
34     } else if (is_khtml) {
35         document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'KHTMLFixes.css">');
36     }
37     return;
40 // Un-trap us from framesets
41 if( window.top != window ) window.top.location = window.location;
43 // for enhanced RecentChanges
44 function toggleVisibility( _levelId, _otherId, _linkId) {
45         var thisLevel = document.getElementById( _levelId );
46         var otherLevel = document.getElementById( _otherId );
47         var linkLevel = document.getElementById( _linkId );
48         if ( thisLevel.style.display == 'none' ) {
49                 thisLevel.style.display = 'block';
50                 otherLevel.style.display = 'none';
51                 linkLevel.style.display = 'inline';
52         } else {
53                 thisLevel.style.display = 'none';
54                 otherLevel.style.display = 'inline';
55                 linkLevel.style.display = 'none';
56                 }
59 // page history stuff
60 // attach event handlers to the input elements on history page
61 function histrowinit () {
62     hf = document.getElementById('pagehistory');
63     if(!hf) return;
64     lis = hf.getElementsByTagName('li');
65     for (i=0;i<lis.length;i++) {
66         inputs=lis[i].getElementsByTagName('INPUT');
67         if(inputs[0] && inputs[1]) {
68                 inputs[0].onclick = diffcheck;
69                 inputs[1].onclick = diffcheck;
70         }
71     }
72     diffcheck();
74 // check selection and tweak visibility/class onclick
75 function diffcheck() { 
76     var dli = false; // the li where the diff radio is checked
77     var oli = false; // the li where the oldid radio is checked
78     hf = document.getElementById('pagehistory');
79     if(!hf) return;
80     lis = hf.getElementsByTagName('li');
81     for (i=0;i<lis.length;i++) {
82         inputs=lis[i].getElementsByTagName('INPUT');
83         if(inputs[1] && inputs[0]) {
84             if(inputs[1].checked || inputs[0].checked) { // this row has a checked radio button
85                 if(inputs[1].checked && inputs[0].checked && inputs[0].value == inputs[1].value) return false;
86                 if(oli) { // it's the second checked radio
87                     if(inputs[1].checked) {
88                     oli.className = "selected";
89                     return false 
90                     }
91                 } else if (inputs[0].checked) {
92                     return false;
93                 }
94                 if(inputs[0].checked) dli = lis[i];
95                 if(!oli) inputs[0].style.visibility = 'hidden';
96                 if(dli) inputs[1].style.visibility = 'hidden';
97                 lis[i].className = "selected";
98                 oli = lis[i];
99             }  else { // no radio is checked in this row
100                 if(!oli) inputs[0].style.visibility = 'hidden';
101                 else inputs[0].style.visibility = 'visible';
102                 if(dli) inputs[1].style.visibility = 'hidden';
103                 else inputs[1].style.visibility = 'visible';
104                 lis[i].className = "";
105             }
106         }
107     }
110 // generate toc from prefs form, fold sections
111 // XXX: needs testing on IE/Mac and safari
112 // more comments to follow
113 function tabbedprefs() {
114     prefform = document.getElementById('preferences');
115     if(!prefform || !document.createElement) return;
116     prefform.className = prefform.className + 'jsprefs';
117     var sections = new Array();
118     children = prefform.childNodes;
119     var seci = 0;
120     for(i=0;i<children.length;i++) {
121         if(children[i].nodeName.indexOf('FIELDSET') != -1) {
122             children[i].id = 'prefsection-' + i;
123             children[i].style.marginLeft = 'auto';
124             if(is_opera) children[i].className = 'operaprefsection';
125             legends = children[i].getElementsByTagName('LEGEND');
126             sections[seci] = new Object();
127             if(legends[0] && legends[0].firstChild.nodeValue)
128                 sections[seci].text = legends[0].firstChild.nodeValue;
129             else
130                 sections[seci].text = '# ' + seci;
131             sections[seci].secid = children[i].id;
132             seci++;
133             if(sections.length != 1) children[i].style.display = 'none';
134             else var selectedid = children[i].id;
135         }
136     }
137     var toc = document.createElement('UL');
138     toc.id = 'preftoc';
139     toc.selectedid = selectedid;
140     for(i=0;i<sections.length;i++) {
141         var li = document.createElement('LI');
142         if(i == 0) li.className = 'selected';
143         var a =  document.createElement('A');
144         a.href = '#' + sections[i].secid;
145         a.onclick = uncoversection;
146         a.innerHTML = sections[i].text;
147         a.secid = sections[i].secid;
148         li.appendChild(a);
149         toc.appendChild(li);
150     }
151     prefform.insertBefore(toc, children[0]);
152     document.getElementById('prefsubmit').id = 'prefcontrol';
154 function uncoversection() {
155     oldsecid = this.parentNode.parentNode.selectedid;
156     newsec = document.getElementById(this.secid);
157     if(oldsecid != this.secid) {
158         ul = document.getElementById('preftoc');
159         document.getElementById(oldsecid).style.display = 'none';
160         newsec.style.display = 'block';
161         ul.selectedid = this.secid;
162         lis = ul.getElementsByTagName('LI');
163         for(i=0;i< lis.length;i++) {
164             lis[i].className = '';
165         }
166         this.parentNode.className = 'selected';
167     }
168     return false;
171 // Timezone stuff
172 // tz in format [+-]HHMM
173 function checkTimezone( tz, msg ) {
174         var localclock = new Date();
175         // returns negative offset from GMT in minutes
176         var tzRaw = localclock.getTimezoneOffset();
177         var tzHour = Math.floor( Math.abs(tzRaw) / 60);
178         var tzMin = Math.abs(tzRaw) % 60;
179         var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
180         if( tz != tzString ) {
181                 var junk = msg.split( '$1' );
182                 document.write( junk[0] + "UTC" + tzString + junk[1] );
183         }
186 // in [-]HH:MM format...
187 // won't yet work with non-even tzs
188 function fetchTimezone() {
189         // FIXME: work around Safari bug
190         var localclock = new Date();
191         // returns negative offset from GMT in minutes
192         var tzRaw = localclock.getTimezoneOffset();
193         var tzHour = Math.floor( Math.abs(tzRaw) / 60);
194         var tzMin = Math.abs(tzRaw) % 60;
195         var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
196                 ":" + ((tzMin < 10) ? "0" : "") + tzMin;
197         return tzString;
200 function guessTimezone(box) {
201         document.preferences.wpHourDiff.value = fetchTimezone();
204 function showTocToggle(show,hide) {
205         if(document.getElementById) {
206                 document.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleToc()" class="internal">' +
207                 '<span id="showlink" style="display:none;">' + show + '</span>' +
208                 '<span id="hidelink">' + hide + '</span>'
209                 + '</a>]</span>');
210         }
214 function toggleToc() {
215         var toc = document.getElementById('tocinside');
216         var showlink=document.getElementById('showlink');
217         var hidelink=document.getElementById('hidelink');
218         if(toc.style.display == 'none') {
219                 toc.style.display = tocWas;
220                 hidelink.style.display='';
221                 showlink.style.display='none';
223         } else {
224                 tocWas = toc.style.display;
225                 toc.style.display = 'none';
226                 hidelink.style.display='none';
227                 showlink.style.display='';
229         }
232 // this function generates the actual toolbar buttons with localized text
233 // we use it to avoid creating the toolbar where javascript is not enabled
234 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
236         speedTip=escapeQuotes(speedTip);
237         tagOpen=escapeQuotes(tagOpen);
238         tagClose=escapeQuotes(tagClose);
239         sampleText=escapeQuotes(sampleText);
240         var mouseOver="";
242         // we can't change the selection, so we show example texts
243         // when moving the mouse instead, until the first button is clicked
244         if(!document.selection && !is_gecko) {
245                 // filter backslashes so it can be shown in the infobox
246                 var re=new RegExp("\\\\n","g");
247                 tagOpen=tagOpen.replace(re,"");
248                 tagClose=tagClose.replace(re,"");
249                 mouseOver = "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen+sampleText+tagClose+"'};\"";
250         }
252         document.write("<a href=\"javascript:insertTags");
253         document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");
255         document.write("<img width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" ALT=\""+speedTip+"\" TITLE=\""+speedTip+"\""+mouseOver+">");
256         document.write("</a>");
257         return;
260 function addInfobox(infoText,text_alert) {
261         alertText=text_alert;
262         var clientPC = navigator.userAgent.toLowerCase(); // Get client info
264         var re=new RegExp("\\\\n","g");
265         alertText=alertText.replace(re,"\n");
267         // if no support for changing selection, add a small copy & paste field
268         // document.selection is an IE-only property. The full toolbar works in IE and
269         // Gecko-based browsers.
270         if(!document.selection && !is_gecko) {
271                 infoText=escapeQuotesHTML(infoText);
272                 document.write("<form name='infoform' id='infoform'>"+
273                         "<input size=80 id='infobox' name='infobox' value=\""+
274                         infoText+"\" READONLY></form>");
275         }
279 function escapeQuotes(text) {
280         var re=new RegExp("'","g");
281         text=text.replace(re,"\\'");
282         re=new RegExp('"',"g");
283         text=text.replace(re,'&quot;');
284         re=new RegExp("\\n","g");
285         text=text.replace(re,"\\n");
286         return text;
289 function escapeQuotesHTML(text) {
290         var re=new RegExp('"',"g");
291         text=text.replace(re,"&quot;");
292         return text;
295 // apply tagOpen/tagClose to selection in textarea,
296 // use sampleText instead of selection if there is none
297 // copied and adapted from phpBB
298 function insertTags(tagOpen, tagClose, sampleText) {
300         var txtarea = document.editform.wpTextbox1;
301         // IE
302         if(document.selection  && !is_gecko) {
303                 var theSelection = document.selection.createRange().text;
304                 if(!theSelection) { theSelection=sampleText;}
305                 txtarea.focus();
306                 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
307                         theSelection = theSelection.substring(0, theSelection.length - 1);
308                         document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
309                 } else {
310                         document.selection.createRange().text = tagOpen + theSelection + tagClose;
311                 }
313         // Mozilla
314         } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
315                 var startPos = txtarea.selectionStart;
316                 var endPos = txtarea.selectionEnd;
317                 var scrollTop=txtarea.scrollTop;
318                 var myText = (txtarea.value).substring(startPos, endPos);
319                 if(!myText) { myText=sampleText;}
320                 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
321                         subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
322                 } else {
323                         subst = tagOpen + myText + tagClose;
324                 }
325                 txtarea.value = txtarea.value.substring(0, startPos) + subst +
326                   txtarea.value.substring(endPos, txtarea.value.length);
327                 txtarea.focus();
329                 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
330                 txtarea.selectionStart=cPos;
331                 txtarea.selectionEnd=cPos;
332                 txtarea.scrollTop=scrollTop;
334         // All others
335         } else {
336                 var copy_alertText=alertText;
337                 var re1=new RegExp("\\$1","g");
338                 var re2=new RegExp("\\$2","g");
339                 copy_alertText=copy_alertText.replace(re1,sampleText);
340                 copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
341                 var text;
342                 if (sampleText) {
343                         text=prompt(copy_alertText);
344                 } else {
345                         text="";
346                 }
347                 if(!text) { text=sampleText;}
348                 text=tagOpen+text+tagClose;
349                 document.infoform.infobox.value=text;
350                 // in Safari this causes scrolling
351                 if(!is_safari) {
352                         txtarea.focus();
353                 }
354                 noOverwrite=true;
355         }
356         // reposition cursor if possible
357         if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();