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
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) {
12 var is_opera_preseven = (window.opera && !document.childNodes);
13 var is_opera_seven = (window.opera && document.childNodes);
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;
25 if (window.addEventListener) window.addEventListener("load",onloadhook,false);
26 else if (window.attachEvent) window.attachEvent("onload",onloadhook);
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">');
39 // Un-trap us from framesets
40 if( window.top != window ) window.top.location = window.location;
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';
52 thisLevel.style.display = 'none';
53 otherLevel.style.display = 'inline';
54 linkLevel.style.display = 'none';
59 // attach event handlers to the input elements on history page
60 function histrowinit () {
61 hf = document.getElementById('pagehistory');
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;
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');
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";
90 } else if (inputs[0].checked) {
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";
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 = "";
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;
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;
130 sections[seci].text = '# ' + seci;
131 sections[seci].secid = children[i].id;
133 if(sections.length != 1) children[i].style.display = 'none';
134 else var selectedid = children[i].id;
137 var toc = document.createElement('UL');
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;
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 = '';
166 this.parentNode.className = 'selected';
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] );
185 function unhidetzbutton() {
186 tzb = document.getElementById('guesstimezonebutton')
187 if(tzb) tzb.style.display = 'inline';
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;
204 function guessTimezone(box) {
205 document.preferences.wpHourDiff.value = fetchTimezone();
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>'
218 function toggleToc() {
219 var tocmain = document.getElementById('toc');
220 var toc = document.getElementById('tocinside');
221 var showlink=document.getElementById('showlink');
222 var hidelink=document.getElementById('hidelink');
223 if(toc.style.display == 'none') {
224 toc.style.display = tocWas;
225 hidelink.style.display='';
226 showlink.style.display='none';
227 tocmain.className = '';
230 tocWas = toc.style.display;
231 toc.style.display = 'none';
232 hidelink.style.display='none';
233 showlink.style.display='';
234 tocmain.className = 'tochidden';
238 // this function generates the actual toolbar buttons with localized text
239 // we use it to avoid creating the toolbar where javascript is not enabled
240 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
242 speedTip=escapeQuotes(speedTip);
243 tagOpen=escapeQuotes(tagOpen);
244 tagClose=escapeQuotes(tagClose);
245 sampleText=escapeQuotes(sampleText);
248 // we can't change the selection, so we show example texts
249 // when moving the mouse instead, until the first button is clicked
250 if(!document.selection && !is_gecko) {
251 // filter backslashes so it can be shown in the infobox
252 var re=new RegExp("\\\\n","g");
253 tagOpen=tagOpen.replace(re,"");
254 tagClose=tagClose.replace(re,"");
255 mouseOver = "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen+sampleText+tagClose+"'};\"";
258 document.write("<a href=\"javascript:insertTags");
259 document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");
261 document.write("<img width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" ALT=\""+speedTip+"\" TITLE=\""+speedTip+"\""+mouseOver+">");
262 document.write("</a>");
266 function addInfobox(infoText,text_alert) {
267 alertText=text_alert;
268 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
270 var re=new RegExp("\\\\n","g");
271 alertText=alertText.replace(re,"\n");
273 // if no support for changing selection, add a small copy & paste field
274 // document.selection is an IE-only property. The full toolbar works in IE and
275 // Gecko-based browsers.
276 if(!document.selection && !is_gecko) {
277 infoText=escapeQuotesHTML(infoText);
278 document.write("<form name='infoform' id='infoform'>"+
279 "<input size=80 id='infobox' name='infobox' value=\""+
280 infoText+"\" READONLY></form>");
285 function escapeQuotes(text) {
286 var re=new RegExp("'","g");
287 text=text.replace(re,"\\'");
288 re=new RegExp('"',"g");
289 text=text.replace(re,'"');
290 re=new RegExp("\\n","g");
291 text=text.replace(re,"\\n");
295 function escapeQuotesHTML(text) {
296 var re=new RegExp('"',"g");
297 text=text.replace(re,""");
301 // apply tagOpen/tagClose to selection in textarea,
302 // use sampleText instead of selection if there is none
303 // copied and adapted from phpBB
304 function insertTags(tagOpen, tagClose, sampleText) {
306 var txtarea = document.editform.wpTextbox1;
308 if(document.selection && !is_gecko) {
309 var theSelection = document.selection.createRange().text;
310 if(!theSelection) { theSelection=sampleText;}
312 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
313 theSelection = theSelection.substring(0, theSelection.length - 1);
314 document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
316 document.selection.createRange().text = tagOpen + theSelection + tagClose;
320 } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
321 var startPos = txtarea.selectionStart;
322 var endPos = txtarea.selectionEnd;
323 var scrollTop=txtarea.scrollTop;
324 var myText = (txtarea.value).substring(startPos, endPos);
325 if(!myText) { myText=sampleText;}
326 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
327 subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
329 subst = tagOpen + myText + tagClose;
331 txtarea.value = txtarea.value.substring(0, startPos) + subst +
332 txtarea.value.substring(endPos, txtarea.value.length);
335 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
336 txtarea.selectionStart=cPos;
337 txtarea.selectionEnd=cPos;
338 txtarea.scrollTop=scrollTop;
342 var copy_alertText=alertText;
343 var re1=new RegExp("\\$1","g");
344 var re2=new RegExp("\\$2","g");
345 copy_alertText=copy_alertText.replace(re1,sampleText);
346 copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
349 text=prompt(copy_alertText);
353 if(!text) { text=sampleText;}
354 text=tagOpen+text+tagClose;
355 document.infoform.infobox.value=text;
356 // in Safari this causes scrolling
362 // reposition cursor if possible
363 if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
367 if(typeof ta == "undefined" || !ta) return;
369 if(is_safari || navigator.userAgent.toLowerCase().indexOf( 'mac' ) + 1 ) pref = 'control-';
370 if(is_opera) pref = 'shift-esc-';
372 n = document.getElementById(id);
376 if(ta[id][0].length > 0) {
377 a.accessKey = ta[id][0];
378 ak = ' ['+pref+ta[id][0]+']';
382 a.title = ta[id][1]+ak;
384 if(ta[id][0].length > 0) {
385 n.accessKey = ta[id][0];
386 ak = ' ['+pref+ta[id][0]+']';
390 n.title = ta[id][1]+ak;