1 // Wikipedia JavaScript support functions
3 // if this is true, the toolbar will no longer overwrite the infobox when you move the mouse over individual items
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) && (clientPC.indexOf('netscape/7.0')==-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) {
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;
24 if (window.addEventListener) window.addEventListener("load",onloadhook,false);
25 else if (window.attachEvent) window.attachEvent("onload",onloadhook);
28 // document.write special stylesheet links
29 function addcss ( stylepath ) {
33 // Un-trap us from framesets
34 if( window.top != window ) window.top.location = window.location;
36 // for enhanced RecentChanges
37 function toggleVisibility( _levelId, _otherId, _linkId) {
38 var thisLevel = document.getElementById( _levelId );
39 var otherLevel = document.getElementById( _otherId );
40 var linkLevel = document.getElementById( _linkId );
41 if ( thisLevel.style.display == 'none' ) {
42 thisLevel.style.display = 'block';
43 otherLevel.style.display = 'none';
44 linkLevel.style.display = 'inline';
46 thisLevel.style.display = 'none';
47 otherLevel.style.display = 'inline';
48 linkLevel.style.display = 'none';
53 // attach event handlers to the input elements on history page
54 function histrowinit () {
55 hf = document.getElementById('pagehistory');
57 lis = hf.getElementsByTagName('LI');
58 for (i=0;i<lis.length;i++) {
59 inputs=lis[i].getElementsByTagName('INPUT');
60 if(inputs[0] && inputs[1]) {
61 inputs[0].onclick = diffcheck;
62 inputs[1].onclick = diffcheck;
67 // check selection and tweak visibility/class onclick
68 function diffcheck() {
69 var dli = false; // the li where the diff radio is checked
70 var oli = false; // the li where the oldid radio is checked
71 hf = document.getElementById('pagehistory');
73 lis = hf.getElementsByTagName('LI');
74 for (i=0;i<lis.length;i++) {
75 inputs=lis[i].getElementsByTagName('INPUT');
76 if(inputs[1] && inputs[0]) {
77 if(inputs[1].checked || inputs[0].checked) { // this row has a checked radio button
78 if(inputs[1].checked && inputs[0].checked && inputs[0].value == inputs[1].value) return false;
79 if(oli) { // it's the second checked radio
80 if(inputs[1].checked) {
81 oli.className = "selected";
84 } else if (inputs[0].checked) {
87 if(inputs[0].checked) dli = lis[i];
88 if(!oli) inputs[0].style.visibility = 'hidden';
89 if(dli) inputs[1].style.visibility = 'hidden';
90 lis[i].className = "selected";
92 } else { // no radio is checked in this row
93 if(!oli) inputs[0].style.visibility = 'hidden';
94 else inputs[0].style.visibility = 'visible';
95 if(dli) inputs[1].style.visibility = 'hidden';
96 else inputs[1].style.visibility = 'visible';
97 lis[i].className = "";
103 // generate toc from prefs form, fold sections
104 // XXX: needs testing on IE/Mac and safari
105 // more comments to follow
106 function tabbedprefs() {
107 prefform = document.getElementById('preferences');
108 if(!prefform || !document.createElement) return;
109 prefform.className = prefform.className + 'jsprefs';
110 var sections = new Array();
111 children = prefform.childNodes;
113 for(i=0;i<children.length;i++) {
114 if(children[i].nodeName.indexOf('FIELDSET') != -1) {
115 children[i].id = 'prefsection-' + seci;
116 children[i].className = 'prefsection';
117 if(is_opera || is_khtml) children[i].className = 'prefsection operaprefsection';
118 legends = children[i].getElementsByTagName('LEGEND');
119 sections[seci] = new Object();
120 if(legends[0] && legends[0].firstChild.nodeValue)
121 sections[seci].text = legends[0].firstChild.nodeValue;
123 sections[seci].text = '# ' + seci;
124 sections[seci].secid = children[i].id;
126 if(sections.length != 1) children[i].style.display = 'none';
127 else var selectedid = children[i].id;
130 var toc = document.createElement('UL');
132 toc.selectedid = selectedid;
133 for(i=0;i<sections.length;i++) {
134 var li = document.createElement('LI');
135 if(i == 0) li.className = 'selected';
136 var a = document.createElement('A');
137 a.href = '#' + sections[i].secid;
138 a.onclick = uncoversection;
139 a.innerHTML = sections[i].text;
140 a.secid = sections[i].secid;
144 prefform.insertBefore(toc, children[0]);
145 document.getElementById('prefsubmit').id = 'prefcontrol';
147 function uncoversection() {
148 oldsecid = this.parentNode.parentNode.selectedid;
149 newsec = document.getElementById(this.secid);
150 if(oldsecid != this.secid) {
151 ul = document.getElementById('preftoc');
152 document.getElementById(oldsecid).style.display = 'none';
153 newsec.style.display = 'block';
154 ul.selectedid = this.secid;
155 lis = ul.getElementsByTagName('LI');
156 for(i=0;i< lis.length;i++) {
157 lis[i].className = '';
159 this.parentNode.className = 'selected';
165 // tz in format [+-]HHMM
166 function checkTimezone( tz, msg ) {
167 var localclock = new Date();
168 // returns negative offset from GMT in minutes
169 var tzRaw = localclock.getTimezoneOffset();
170 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
171 var tzMin = Math.abs(tzRaw) % 60;
172 var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
173 if( tz != tzString ) {
174 var junk = msg.split( '$1' );
175 document.write( junk[0] + "UTC" + tzString + junk[1] );
178 function unhidetzbutton() {
179 tzb = document.getElementById('guesstimezonebutton')
180 if(tzb) tzb.style.display = 'inline';
183 // in [-]HH:MM format...
184 // won't yet work with non-even tzs
185 function fetchTimezone() {
186 // FIXME: work around Safari bug
187 var localclock = new Date();
188 // returns negative offset from GMT in minutes
189 var tzRaw = localclock.getTimezoneOffset();
190 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
191 var tzMin = Math.abs(tzRaw) % 60;
192 var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
193 ":" + ((tzMin < 10) ? "0" : "") + tzMin;
197 function guessTimezone(box) {
198 document.preferences.wpHourDiff.value = fetchTimezone();
201 function showTocToggle(show,hide) {
202 if (document.getElementById) {
203 document.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleToc()" class="internal">' +
204 '<span id="showlink" style="display:none;">' + show + '</span>' +
205 '<span id="hidelink">' + hide + '</span>'
211 function toggleToc() {
212 var toc = document.getElementById('tocinside');
213 var showlink=document.getElementById('showlink');
214 var hidelink=document.getElementById('hidelink');
215 if(toc.style.display == 'none') {
216 toc.style.display = tocWas;
217 hidelink.style.display='';
218 showlink.style.display='none';
221 tocWas = toc.style.display;
222 toc.style.display = 'none';
223 hidelink.style.display='none';
224 showlink.style.display='';
229 // this function generates the actual toolbar buttons with localized text
230 // we use it to avoid creating the toolbar where javascript is not enabled
231 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
233 speedTip=escapeQuotes(speedTip);
234 tagOpen=escapeQuotes(tagOpen);
235 tagClose=escapeQuotes(tagClose);
236 sampleText=escapeQuotes(sampleText);
239 // we can't change the selection, so we show example texts
240 // when moving the mouse instead, until the first button is clicked
241 if(!document.selection && !is_gecko) {
242 // filter backslashes so it can be shown in the infobox
243 var re=new RegExp("\\\\n","g");
244 tagOpen=tagOpen.replace(re,"");
245 tagClose=tagClose.replace(re,"");
246 mouseOver = "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen+sampleText+tagClose+"'};\"";
249 document.write("<a href=\"javascript:insertTags");
250 document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");
252 document.write("<img width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" ALT=\""+speedTip+"\" TITLE=\""+speedTip+"\""+mouseOver+">");
253 document.write("</a>");
257 function addInfobox(infoText,text_alert) {
258 alertText=text_alert;
259 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
261 var re=new RegExp("\\\\n","g");
262 alertText=alertText.replace(re,"\n");
264 // if no support for changing selection, add a small copy & paste field
265 // document.selection is an IE-only property. The full toolbar works in IE and
266 // Gecko-based browsers.
267 if(!document.selection && !is_gecko) {
268 infoText=escapeQuotesHTML(infoText);
269 document.write("<form name='infoform' id='infoform'>"+
270 "<input size=80 id='infobox' name='infobox' value=\""+
271 infoText+"\" READONLY></form>");
276 function escapeQuotes(text) {
277 var re=new RegExp("'","g");
278 text=text.replace(re,"\\'");
279 re=new RegExp('"',"g");
280 text=text.replace(re,'"');
281 re=new RegExp("\\n","g");
282 text=text.replace(re,"\\n");
286 function escapeQuotesHTML(text) {
287 var re=new RegExp('"',"g");
288 text=text.replace(re,""");
292 // apply tagOpen/tagClose to selection in textarea,
293 // use sampleText instead of selection if there is none
294 // copied and adapted from phpBB
295 function insertTags(tagOpen, tagClose, sampleText) {
297 var txtarea = document.editform.wpTextbox1;
299 if(document.selection && !is_gecko) {
300 var theSelection = document.selection.createRange().text;
301 if(!theSelection) { theSelection=sampleText;}
303 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
304 theSelection = theSelection.substring(0, theSelection.length - 1);
305 document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
307 document.selection.createRange().text = tagOpen + theSelection + tagClose;
311 } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
312 var startPos = txtarea.selectionStart;
313 var endPos = txtarea.selectionEnd;
314 var scrollTop=txtarea.scrollTop;
315 var myText = (txtarea.value).substring(startPos, endPos);
316 if(!myText) { myText=sampleText;}
317 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
318 subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
320 subst = tagOpen + myText + tagClose;
322 txtarea.value = txtarea.value.substring(0, startPos) + subst +
323 txtarea.value.substring(endPos, txtarea.value.length);
326 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
327 txtarea.selectionStart=cPos;
328 txtarea.selectionEnd=cPos;
329 txtarea.scrollTop=scrollTop;
333 var copy_alertText=alertText;
334 var re1=new RegExp("\\$1","g");
335 var re2=new RegExp("\\$2","g");
336 copy_alertText=copy_alertText.replace(re1,sampleText);
337 copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
340 text=prompt(copy_alertText);
344 if(!text) { text=sampleText;}
345 text=tagOpen+text+tagClose;
346 document.infoform.infobox.value=text;
347 // in Safari this causes scrolling
353 // reposition cursor if possible
354 if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();