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