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