c65af895f516d82b6df38d08fe5422ec1d42f178
[lhc/web/wiklou.git] / stylesheets / wikibits.js
1 // Wikipedia JavaScript support functions
2
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);
15 }
16
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();
23 }
24 if (window.addEventListener) window.addEventListener("load",onloadhook,false);
25 else if (window.attachEvent) window.attachEvent("onload",onloadhook);
26
27
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;
38 }
39
40 // Un-trap us from framesets
41 if( window.top != window ) window.top.location = window.location;
42
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 }
57 }
58
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();
73 }
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 }
108 }
109
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].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 document.getElementById('guesstimezonebutton').style.display = 'inline';
154 }
155 function uncoversection() {
156 oldsecid = this.parentNode.parentNode.selectedid;
157 newsec = document.getElementById(this.secid);
158 if(oldsecid != this.secid) {
159 ul = document.getElementById('preftoc');
160 document.getElementById(oldsecid).style.display = 'none';
161 newsec.style.display = 'block';
162 ul.selectedid = this.secid;
163 lis = ul.getElementsByTagName('LI');
164 for(i=0;i< lis.length;i++) {
165 lis[i].className = '';
166 }
167 this.parentNode.className = 'selected';
168 }
169 return false;
170 }
171
172 // Timezone stuff
173 // tz in format [+-]HHMM
174 function checkTimezone( tz, msg ) {
175 var localclock = new Date();
176 // returns negative offset from GMT in minutes
177 var tzRaw = localclock.getTimezoneOffset();
178 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
179 var tzMin = Math.abs(tzRaw) % 60;
180 var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
181 if( tz != tzString ) {
182 var junk = msg.split( '$1' );
183 document.write( junk[0] + "UTC" + tzString + junk[1] );
184 }
185 }
186
187 // in [-]HH:MM format...
188 // won't yet work with non-even tzs
189 function fetchTimezone() {
190 // FIXME: work around Safari bug
191 var localclock = new Date();
192 // returns negative offset from GMT in minutes
193 var tzRaw = localclock.getTimezoneOffset();
194 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
195 var tzMin = Math.abs(tzRaw) % 60;
196 var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
197 ":" + ((tzMin < 10) ? "0" : "") + tzMin;
198 return tzString;
199 }
200
201 function guessTimezone(box) {
202 document.preferences.wpHourDiff.value = fetchTimezone();
203 }
204
205 function showTocToggle(show,hide) {
206 if(document.getElementById) {
207 document.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleToc()" class="internal">' +
208 '<span id="showlink" style="display:none;">' + show + '</span>' +
209 '<span id="hidelink">' + hide + '</span>'
210 + '</a>]</span>');
211 }
212 }
213
214
215 function toggleToc() {
216 var toc = document.getElementById('tocinside');
217 var showlink=document.getElementById('showlink');
218 var hidelink=document.getElementById('hidelink');
219 if(toc.style.display == 'none') {
220 toc.style.display = tocWas;
221 hidelink.style.display='';
222 showlink.style.display='none';
223
224 } else {
225 tocWas = toc.style.display;
226 toc.style.display = 'none';
227 hidelink.style.display='none';
228 showlink.style.display='';
229
230 }
231 }
232
233 // this function generates the actual toolbar buttons with localized text
234 // we use it to avoid creating the toolbar where javascript is not enabled
235 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
236
237 speedTip=escapeQuotes(speedTip);
238 tagOpen=escapeQuotes(tagOpen);
239 tagClose=escapeQuotes(tagClose);
240 sampleText=escapeQuotes(sampleText);
241 var mouseOver="";
242
243 // we can't change the selection, so we show example texts
244 // when moving the mouse instead, until the first button is clicked
245 if(!document.selection && !is_gecko) {
246 // filter backslashes so it can be shown in the infobox
247 var re=new RegExp("\\\\n","g");
248 tagOpen=tagOpen.replace(re,"");
249 tagClose=tagClose.replace(re,"");
250 mouseOver = "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen+sampleText+tagClose+"'};\"";
251 }
252
253 document.write("<a href=\"javascript:insertTags");
254 document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");
255
256 document.write("<img width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" ALT=\""+speedTip+"\" TITLE=\""+speedTip+"\""+mouseOver+">");
257 document.write("</a>");
258 return;
259 }
260
261 function addInfobox(infoText,text_alert) {
262 alertText=text_alert;
263 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
264
265 var re=new RegExp("\\\\n","g");
266 alertText=alertText.replace(re,"\n");
267
268 // if no support for changing selection, add a small copy & paste field
269 // document.selection is an IE-only property. The full toolbar works in IE and
270 // Gecko-based browsers.
271 if(!document.selection && !is_gecko) {
272 infoText=escapeQuotesHTML(infoText);
273 document.write("<form name='infoform' id='infoform'>"+
274 "<input size=80 id='infobox' name='infobox' value=\""+
275 infoText+"\" READONLY></form>");
276 }
277
278 }
279
280 function escapeQuotes(text) {
281 var re=new RegExp("'","g");
282 text=text.replace(re,"\\'");
283 re=new RegExp('"',"g");
284 text=text.replace(re,'&quot;');
285 re=new RegExp("\\n","g");
286 text=text.replace(re,"\\n");
287 return text;
288 }
289
290 function escapeQuotesHTML(text) {
291 var re=new RegExp('"',"g");
292 text=text.replace(re,"&quot;");
293 return text;
294 }
295
296 // apply tagOpen/tagClose to selection in textarea,
297 // use sampleText instead of selection if there is none
298 // copied and adapted from phpBB
299 function insertTags(tagOpen, tagClose, sampleText) {
300
301 var txtarea = document.editform.wpTextbox1;
302 // IE
303 if(document.selection && !is_gecko) {
304 var theSelection = document.selection.createRange().text;
305 if(!theSelection) { theSelection=sampleText;}
306 txtarea.focus();
307 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
308 theSelection = theSelection.substring(0, theSelection.length - 1);
309 document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
310 } else {
311 document.selection.createRange().text = tagOpen + theSelection + tagClose;
312 }
313
314 // Mozilla
315 } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
316 var startPos = txtarea.selectionStart;
317 var endPos = txtarea.selectionEnd;
318 var scrollTop=txtarea.scrollTop;
319 var myText = (txtarea.value).substring(startPos, endPos);
320 if(!myText) { myText=sampleText;}
321 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
322 subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
323 } else {
324 subst = tagOpen + myText + tagClose;
325 }
326 txtarea.value = txtarea.value.substring(0, startPos) + subst +
327 txtarea.value.substring(endPos, txtarea.value.length);
328 txtarea.focus();
329
330 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
331 txtarea.selectionStart=cPos;
332 txtarea.selectionEnd=cPos;
333 txtarea.scrollTop=scrollTop;
334
335 // All others
336 } else {
337 var copy_alertText=alertText;
338 var re1=new RegExp("\\$1","g");
339 var re2=new RegExp("\\$2","g");
340 copy_alertText=copy_alertText.replace(re1,sampleText);
341 copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
342 var text;
343 if (sampleText) {
344 text=prompt(copy_alertText);
345 } else {
346 text="";
347 }
348 if(!text) { text=sampleText;}
349 text=tagOpen+text+tagClose;
350 document.infoform.infobox.value=text;
351 // in Safari this causes scrolling
352 if(!is_safari) {
353 txtarea.focus();
354 }
355 noOverwrite=true;
356 }
357 // reposition cursor if possible
358 if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
359 }