user_token
[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 if(prefform.nodeName == 'A') return; // Occasional IE problem
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-' + seci;
123 children[i].className = 'prefsection';
124 if(is_opera || is_khtml) children[i].className = 'prefsection 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';
153 }
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;
169 }
170
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 }
184 }
185 function unhidetzbutton() {
186 tzb = document.getElementById('guesstimezonebutton')
187 if(tzb) tzb.style.display = 'inline';
188 }
189
190 // in [-]HH:MM format...
191 // won't yet work with non-even tzs
192 function fetchTimezone() {
193 // FIXME: work around Safari bug
194 var localclock = new Date();
195 // returns negative offset from GMT in minutes
196 var tzRaw = localclock.getTimezoneOffset();
197 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
198 var tzMin = Math.abs(tzRaw) % 60;
199 var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
200 ":" + ((tzMin < 10) ? "0" : "") + tzMin;
201 return tzString;
202 }
203
204 function guessTimezone(box) {
205 document.preferences.wpHourDiff.value = fetchTimezone();
206 }
207
208 function showTocToggle(show,hide) {
209 if(document.getElementById) {
210 document.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleToc()" class="internal">' +
211 '<span id="showlink" style="display:none;">' + show + '</span>' +
212 '<span id="hidelink">' + hide + '</span>'
213 + '</a>]</span>');
214 }
215 }
216
217
218 function toggleToc() {
219 var toc = document.getElementById('tocinside');
220 var showlink=document.getElementById('showlink');
221 var hidelink=document.getElementById('hidelink');
222 if(toc.style.display == 'none') {
223 toc.style.display = tocWas;
224 hidelink.style.display='';
225 showlink.style.display='none';
226
227 } else {
228 tocWas = toc.style.display;
229 toc.style.display = 'none';
230 hidelink.style.display='none';
231 showlink.style.display='';
232
233 }
234 }
235
236 // this function generates the actual toolbar buttons with localized text
237 // we use it to avoid creating the toolbar where javascript is not enabled
238 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
239
240 speedTip=escapeQuotes(speedTip);
241 tagOpen=escapeQuotes(tagOpen);
242 tagClose=escapeQuotes(tagClose);
243 sampleText=escapeQuotes(sampleText);
244 var mouseOver="";
245
246 // we can't change the selection, so we show example texts
247 // when moving the mouse instead, until the first button is clicked
248 if(!document.selection && !is_gecko) {
249 // filter backslashes so it can be shown in the infobox
250 var re=new RegExp("\\\\n","g");
251 tagOpen=tagOpen.replace(re,"");
252 tagClose=tagClose.replace(re,"");
253 mouseOver = "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen+sampleText+tagClose+"'};\"";
254 }
255
256 document.write("<a href=\"javascript:insertTags");
257 document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");
258
259 document.write("<img width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" ALT=\""+speedTip+"\" TITLE=\""+speedTip+"\""+mouseOver+">");
260 document.write("</a>");
261 return;
262 }
263
264 function addInfobox(infoText,text_alert) {
265 alertText=text_alert;
266 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
267
268 var re=new RegExp("\\\\n","g");
269 alertText=alertText.replace(re,"\n");
270
271 // if no support for changing selection, add a small copy & paste field
272 // document.selection is an IE-only property. The full toolbar works in IE and
273 // Gecko-based browsers.
274 if(!document.selection && !is_gecko) {
275 infoText=escapeQuotesHTML(infoText);
276 document.write("<form name='infoform' id='infoform'>"+
277 "<input size=80 id='infobox' name='infobox' value=\""+
278 infoText+"\" READONLY></form>");
279 }
280
281 }
282
283 function escapeQuotes(text) {
284 var re=new RegExp("'","g");
285 text=text.replace(re,"\\'");
286 re=new RegExp('"',"g");
287 text=text.replace(re,'&quot;');
288 re=new RegExp("\\n","g");
289 text=text.replace(re,"\\n");
290 return text;
291 }
292
293 function escapeQuotesHTML(text) {
294 var re=new RegExp('"',"g");
295 text=text.replace(re,"&quot;");
296 return text;
297 }
298
299 // apply tagOpen/tagClose to selection in textarea,
300 // use sampleText instead of selection if there is none
301 // copied and adapted from phpBB
302 function insertTags(tagOpen, tagClose, sampleText) {
303
304 var txtarea = document.editform.wpTextbox1;
305 // IE
306 if(document.selection && !is_gecko) {
307 var theSelection = document.selection.createRange().text;
308 if(!theSelection) { theSelection=sampleText;}
309 txtarea.focus();
310 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
311 theSelection = theSelection.substring(0, theSelection.length - 1);
312 document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
313 } else {
314 document.selection.createRange().text = tagOpen + theSelection + tagClose;
315 }
316
317 // Mozilla
318 } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
319 var startPos = txtarea.selectionStart;
320 var endPos = txtarea.selectionEnd;
321 var scrollTop=txtarea.scrollTop;
322 var myText = (txtarea.value).substring(startPos, endPos);
323 if(!myText) { myText=sampleText;}
324 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
325 subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
326 } else {
327 subst = tagOpen + myText + tagClose;
328 }
329 txtarea.value = txtarea.value.substring(0, startPos) + subst +
330 txtarea.value.substring(endPos, txtarea.value.length);
331 txtarea.focus();
332
333 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
334 txtarea.selectionStart=cPos;
335 txtarea.selectionEnd=cPos;
336 txtarea.scrollTop=scrollTop;
337
338 // All others
339 } else {
340 var copy_alertText=alertText;
341 var re1=new RegExp("\\$1","g");
342 var re2=new RegExp("\\$2","g");
343 copy_alertText=copy_alertText.replace(re1,sampleText);
344 copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
345 var text;
346 if (sampleText) {
347 text=prompt(copy_alertText);
348 } else {
349 text="";
350 }
351 if(!text) { text=sampleText;}
352 text=tagOpen+text+tagClose;
353 document.infoform.infobox.value=text;
354 // in Safari this causes scrolling
355 if(!is_safari) {
356 txtarea.focus();
357 }
358 noOverwrite=true;
359 }
360 // reposition cursor if possible
361 if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
362 }
363
364 function akeytt() {
365 if(typeof ta == "undefined" || !ta) return;
366 pref = 'alt-';
367 if(is_safari || navigator.userAgent.toLowerCase().indexOf( 'mac' ) + 1 ) pref = 'control-';
368 if(is_opera) pref = 'shift-esc-';
369 for(id in ta) {
370 n = document.getElementById(id);
371 if(n){
372 a = n.childNodes[0];
373 if(a){
374 if(ta[id][0].length > 0) {
375 a.accessKey = ta[id][0];
376 ak = ' ['+pref+ta[id][0]+']';
377 } else {
378 ak = '';
379 }
380 a.title = ta[id][1]+ak;
381 } else {
382 if(ta[id][0].length > 0) {
383 n.accessKey = ta[id][0];
384 ak = ' ['+pref+ta[id][0]+']';
385 } else {
386 ak = '';
387 }
388 n.title = ta[id][1]+ak;
389 }
390 }
391 }
392 }