1 // Wikipedia JavaScript support functions
3 var clientPC
= navigator
.userAgent
.toLowerCase(); // Get client info
4 var is_gecko
= ((clientPC
.indexOf('gecko')!=-1) && (clientPC
.indexOf('spoofer')==-1)
5 && (clientPC
.indexOf('khtml') == -1) && (clientPC
.indexOf('netscape/7.0')==-1));
6 var is_safari
= ((clientPC
.indexOf('AppleWebKit')!=-1) && (clientPC
.indexOf('spoofer')==-1));
7 var is_khtml
= (navigator
.vendor
== 'KDE' || ( document
.childNodes
&& !document
.all
&& !navigator
.taintEnabled
));
8 if (clientPC
.indexOf('opera') != -1) {
10 var is_opera_preseven
= (window
.opera
&& !document
.childNodes
);
11 var is_opera_seven
= (window
.opera
&& document
.childNodes
);
14 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
18 if (!window
.onloadFuncts
)
19 var onloadFuncts
= [];
21 function addOnloadHook(hookFunct
) {
22 // Allows add-on scripts to add onload functions
23 onloadFuncts
[onloadFuncts
.length
] = hookFunct
;
26 function runOnloadHook() {
27 // don't run anything below this for non-dom browsers
28 if (doneOnloadHook
|| !(document
.getElementById
&& document
.getElementsByTagName
))
36 setupCheckboxShiftClick();
38 // Run any added-on functions
39 for (var i
= 0; i
< onloadFuncts
.length
; i
++)
42 doneOnloadHook
= true;
45 function hookEvent(hookName
, hookFunct
) {
46 if (window
.addEventListener
)
47 addEventListener(hookName
, hookFunct
, false);
48 else if (window
.attachEvent
)
49 attachEvent("on" + hookName
, hookFunct
);
52 hookEvent("load", runOnloadHook
);
54 // document.write special stylesheet links
55 if (typeof stylepath
!= 'undefined' && typeof skin
!= 'undefined') {
56 if (is_opera_preseven
) {
57 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'/'+skin
+'/Opera6Fixes.css">');
58 } else if (is_opera_seven
) {
59 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'/'+skin
+'/Opera7Fixes.css">');
60 } else if (is_khtml
) {
61 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'/'+skin
+'/KHTMLFixes.css">');
64 // Un-trap us from framesets
65 if (window
.top
!= window
)
66 window
.top
.location
= window
.location
;
68 // for enhanced RecentChanges
69 function toggleVisibility(_levelId
, _otherId
, _linkId
) {
70 var thisLevel
= document
.getElementById(_levelId
);
71 var otherLevel
= document
.getElementById(_otherId
);
72 var linkLevel
= document
.getElementById(_linkId
);
73 if (thisLevel
.style
.display
== 'none') {
74 thisLevel
.style
.display
= 'block';
75 otherLevel
.style
.display
= 'none';
76 linkLevel
.style
.display
= 'inline';
78 thisLevel
.style
.display
= 'none';
79 otherLevel
.style
.display
= 'inline';
80 linkLevel
.style
.display
= 'none';
85 // attach event handlers to the input elements on history page
86 function histrowinit() {
87 hf
= document
.getElementById('pagehistory');
90 lis
= hf
.getElementsByTagName('li');
91 for (i
= 0; i
< lis
.length
; i
++) {
92 inputs
= lis
[i
].getElementsByTagName('input');
93 if (inputs
[0] && inputs
[1]) {
94 inputs
[0].onclick
= diffcheck
;
95 inputs
[1].onclick
= diffcheck
;
101 // check selection and tweak visibility/class onclick
102 function diffcheck() {
103 var dli
= false; // the li where the diff radio is checked
104 var oli
= false; // the li where the oldid radio is checked
105 hf
= document
.getElementById('pagehistory');
108 lis
= hf
.getElementsByTagName('li');
109 for (i
=0;i
<lis
.length
;i
++) {
110 inputs
= lis
[i
].getElementsByTagName('input');
111 if (inputs
[1] && inputs
[0]) {
112 if (inputs
[1].checked
|| inputs
[0].checked
) { // this row has a checked radio button
113 if (inputs
[1].checked
&& inputs
[0].checked
&& inputs
[0].value
== inputs
[1].value
)
115 if (oli
) { // it's the second checked radio
116 if (inputs
[1].checked
) {
117 oli
.className
= "selected";
120 } else if (inputs
[0].checked
) {
123 if (inputs
[0].checked
)
126 inputs
[0].style
.visibility
= 'hidden';
128 inputs
[1].style
.visibility
= 'hidden';
129 lis
[i
].className
= "selected";
131 } else { // no radio is checked in this row
133 inputs
[0].style
.visibility
= 'hidden';
135 inputs
[0].style
.visibility
= 'visible';
137 inputs
[1].style
.visibility
= 'hidden';
139 inputs
[1].style
.visibility
= 'visible';
140 lis
[i
].className
= "";
146 // generate toc from prefs form, fold sections
147 // XXX: needs testing on IE/Mac and safari
148 // more comments to follow
149 function tabbedprefs() {
150 var prefform
= document
.getElementById('preferences');
151 if (!prefform
|| !document
.createElement
)
153 if (prefform
.nodeName
.toLowerCase() == 'a')
154 return; // Occasional IE problem
155 prefform
.className
= prefform
.className
+ 'jsprefs';
156 var sections
= new Array();
157 children
= prefform
.childNodes
;
159 for (i
= 0; i
< children
.length
; i
++) {
160 if (children
[i
].nodeName
.toLowerCase() == 'fieldset') {
161 children
[i
].id
= 'prefsection-' + seci
;
162 children
[i
].className
= 'prefsection';
163 if (is_opera
|| is_khtml
)
164 children
[i
].className
= 'prefsection operaprefsection';
165 legends
= children
[i
].getElementsByTagName('legend');
166 sections
[seci
] = new Object();
167 legends
[0].className
= 'mainLegend';
168 if (legends
[0] && legends
[0].firstChild
.nodeValue
)
169 sections
[seci
].text
= legends
[0].firstChild
.nodeValue
;
171 sections
[seci
].text
= '# ' + seci
;
172 sections
[seci
].secid
= children
[i
].id
;
174 if (sections
.length
!= 1)
175 children
[i
].style
.display
= 'none';
177 var selectedid
= children
[i
].id
;
180 var toc
= document
.createElement('ul');
182 toc
.selectedid
= selectedid
;
183 for (i
= 0; i
< sections
.length
; i
++) {
184 var li
= document
.createElement('li');
186 li
.className
= 'selected';
187 var a
= document
.createElement('a');
188 a
.href
= '#' + sections
[i
].secid
;
189 a
.onmousedown
= a
.onclick
= uncoversection
;
190 a
.appendChild(document
.createTextNode(sections
[i
].text
));
191 a
.secid
= sections
[i
].secid
;
195 prefform
.parentNode
.insertBefore(toc
, prefform
.parentNode
.childNodes
[0]);
196 document
.getElementById('prefsubmit').id
= 'prefcontrol';
199 function uncoversection() {
200 oldsecid
= this.parentNode
.parentNode
.selectedid
;
201 newsec
= document
.getElementById(this.secid
);
202 if (oldsecid
!= this.secid
) {
203 ul
= document
.getElementById('preftoc');
204 document
.getElementById(oldsecid
).style
.display
= 'none';
205 newsec
.style
.display
= 'block';
206 ul
.selectedid
= this.secid
;
207 lis
= ul
.getElementsByTagName('li');
208 for (i
= 0; i
< lis
.length
; i
++) {
209 lis
[i
].className
= '';
211 this.parentNode
.className
= 'selected';
217 // tz in format [+-]HHMM
218 function checkTimezone(tz
, msg
) {
219 var localclock
= new Date();
220 // returns negative offset from GMT in minutes
221 var tzRaw
= localclock
.getTimezoneOffset();
222 var tzHour
= Math
.floor( Math
.abs(tzRaw
) / 60);
223 var tzMin
= Math
.abs(tzRaw
) % 60;
224 var tzString
= ((tzRaw
>= 0) ? "-" : "+") + ((tzHour
< 10) ? "0" : "") + tzHour
+ ((tzMin
< 10) ? "0" : "") + tzMin
;
225 if (tz
!= tzString
) {
226 var junk
= msg
.split('$1');
227 document
.write(junk
[0] + "UTC" + tzString
+ junk
[1]);
231 function unhidetzbutton() {
232 tzb
= document
.getElementById('guesstimezonebutton')
234 tzb
.style
.display
= 'inline';
237 // in [-]HH:MM format...
238 // won't yet work with non-even tzs
239 function fetchTimezone() {
240 // FIXME: work around Safari bug
241 var localclock
= new Date();
242 // returns negative offset from GMT in minutes
243 var tzRaw
= localclock
.getTimezoneOffset();
244 var tzHour
= Math
.floor( Math
.abs(tzRaw
) / 60);
245 var tzMin
= Math
.abs(tzRaw
) % 60;
246 var tzString
= ((tzRaw
>= 0) ? "-" : "") + ((tzHour
< 10) ? "0" : "") + tzHour
+
247 ":" + ((tzMin
< 10) ? "0" : "") + tzMin
;
251 function guessTimezone(box
) {
252 document
.getElementsByName("wpHourDiff")[0].value
= fetchTimezone();
255 function showTocToggle() {
256 if (document
.createTextNode
) {
257 // Uses DOM calls to avoid document.write + XHTML issues
259 var linkHolder
= document
.getElementById('toctitle')
263 var outerSpan
= document
.createElement('span');
264 outerSpan
.className
= 'toctoggle';
266 var toggleLink
= document
.createElement('a');
267 toggleLink
.id
= 'togglelink';
268 toggleLink
.className
= 'internal';
269 toggleLink
.href
= 'javascript:toggleToc()';
270 toggleLink
.appendChild(document
.createTextNode(tocHideText
));
272 outerSpan
.appendChild(document
.createTextNode('['));
273 outerSpan
.appendChild(toggleLink
);
274 outerSpan
.appendChild(document
.createTextNode(']'));
276 linkHolder
.appendChild(document
.createTextNode(' '));
277 linkHolder
.appendChild(outerSpan
);
279 var cookiePos
= document
.cookie
.indexOf("hidetoc=");
280 if (cookiePos
> -1 && document
.cookie
.charAt(cookiePos
+ 8) == 1)
285 function changeText(el
, newText
) {
286 // Safari work around
288 el
.innerText
= newText
;
289 else if (el
.firstChild
&& el
.firstChild
.nodeValue
)
290 el
.firstChild
.nodeValue
= newText
;
293 function toggleToc() {
294 var toc
= document
.getElementById('toc').getElementsByTagName('ul')[0];
295 var toggleLink
= document
.getElementById('togglelink')
297 if (toc
&& toggleLink
&& toc
.style
.display
== 'none') {
298 changeText(toggleLink
, tocHideText
);
299 toc
.style
.display
= 'block';
300 document
.cookie
= "hidetoc=0";
302 changeText(toggleLink
, tocShowText
);
303 toc
.style
.display
= 'none';
304 document
.cookie
= "hidetoc=1";
308 // this function generates the actual toolbar buttons with localized text
309 // we use it to avoid creating the toolbar where javascript is not enabled
310 function addButton(imageFile
, speedTip
, tagOpen
, tagClose
, sampleText
) {
311 // Don't generate buttons for browsers which don't fully
313 if (!document
.selection
&& !is_gecko
) {
316 imageFile
= escapeQuotesHTML(imageFile
);
317 speedTip
= escapeQuotesHTML(speedTip
);
318 tagOpen
= escapeQuotes(tagOpen
);
319 tagClose
= escapeQuotes(tagClose
);
320 sampleText
= escapeQuotes(sampleText
);
323 document
.write("<a href=\"javascript:insertTags");
324 document
.write("('"+tagOpen
+"','"+tagClose
+"','"+sampleText
+"');\">");
325 document
.write("<img width=\"23\" height=\"22\" src=\""+imageFile
+"\" border=\"0\" alt=\""+speedTip
+"\" title=\""+speedTip
+"\""+mouseOver
+">");
326 document
.write("</a>");
330 function escapeQuotes(text
) {
331 var re
= new RegExp("'","g");
332 text
= text
.replace(re
,"\\'");
333 re
= new RegExp("\\n","g");
334 text
= text
.replace(re
,"\\n");
335 return escapeQuotesHTML(text
);
338 function escapeQuotesHTML(text
) {
339 var re
= new RegExp('&',"g");
340 text
= text
.replace(re
,"&");
341 var re
= new RegExp('"',"g");
342 text
= text
.replace(re
,""");
343 var re
= new RegExp('<',"g");
344 text
= text
.replace(re
,"<");
345 var re
= new RegExp('>',"g");
346 text
= text
.replace(re
,">");
350 // apply tagOpen/tagClose to selection in textarea,
351 // use sampleText instead of selection if there is none
352 // copied and adapted from phpBB
353 function insertTags(tagOpen
, tagClose
, sampleText
) {
354 if (document
.editform
)
355 var txtarea
= document
.editform
.wpTextbox1
;
357 // some alternate form? take the first one we can find
358 var areas
= document
.getElementsByTagName('textarea');
359 var txtarea
= areas
[0];
363 if (document
.selection
&& !is_gecko
) {
364 var theSelection
= document
.selection
.createRange().text
;
366 theSelection
=sampleText
;
368 if (theSelection
.charAt(theSelection
.length
- 1) == " ") { // exclude ending space char, if any
369 theSelection
= theSelection
.substring(0, theSelection
.length
- 1);
370 document
.selection
.createRange().text
= tagOpen
+ theSelection
+ tagClose
+ " ";
372 document
.selection
.createRange().text
= tagOpen
+ theSelection
+ tagClose
;
376 } else if(txtarea
.selectionStart
|| txtarea
.selectionStart
== '0') {
377 var replaced
= false;
378 var startPos
= txtarea
.selectionStart
;
379 var endPos
= txtarea
.selectionEnd
;
382 var scrollTop
= txtarea
.scrollTop
;
383 var myText
= (txtarea
.value
).substring(startPos
, endPos
);
386 if (myText
.charAt(myText
.length
- 1) == " ") { // exclude ending space char, if any
387 subst
= tagOpen
+ myText
.substring(0, (myText
.length
- 1)) + tagClose
+ " ";
389 subst
= tagOpen
+ myText
+ tagClose
;
391 txtarea
.value
= txtarea
.value
.substring(0, startPos
) + subst
+
392 txtarea
.value
.substring(endPos
, txtarea
.value
.length
);
396 var cPos
= startPos
+(tagOpen
.length
+myText
.length
+tagClose
.length
);
397 txtarea
.selectionStart
= cPos
;
398 txtarea
.selectionEnd
= cPos
;
400 txtarea
.selectionStart
= startPos
+tagOpen
.length
;
401 txtarea
.selectionEnd
= startPos
+tagOpen
.length
+myText
.length
;
403 txtarea
.scrollTop
= scrollTop
;
405 // All other browsers get no toolbar.
406 // There was previously support for a crippled "help"
407 // bar, but that caused more problems than it solved.
409 // reposition cursor if possible
410 if (txtarea
.createTextRange
)
411 txtarea
.caretPos
= document
.selection
.createRange().duplicate();
415 if (typeof ta
== "undefined" || !ta
)
418 if (is_safari
|| navigator
.userAgent
.toLowerCase().indexOf('mac') + 1
419 || navigator
.userAgent
.toLowerCase().indexOf('konqueror') + 1 )
425 var n
= document
.getElementById(id
);
429 // Are we putting accesskey in it
430 if (ta
[id
][0].length
> 0) {
431 // Is this object a object? If not assume it's the next child.
433 if (n
.nodeName
.toLowerCase() == "a") {
440 a
.accessKey
= ta
[id
][0];
441 ak
= ' ['+pref
+ta
[id
][0]+']';
444 // We don't care what type the object is when assigning tooltip
450 a
.title
= ta
[id
][1]+ak
;
456 function setupRightClickEdit() {
457 if (document
.getElementsByTagName
) {
458 var divs
= document
.getElementsByTagName('div');
459 for (var i
= 0; i
< divs
.length
; i
++) {
461 if(el
.className
== 'editsection') {
462 addRightClickEditHandler(el
);
468 function addRightClickEditHandler(el
) {
469 for (var i
= 0; i
< el
.childNodes
.length
; i
++) {
470 var link
= el
.childNodes
[i
];
471 if (link
.nodeType
== 1 && link
.nodeName
.toLowerCase() == 'a') {
472 var editHref
= link
.getAttribute('href');
474 // find the following a
475 var next
= el
.nextSibling
;
476 while (next
.nodeType
!= 1)
477 next
= next
.nextSibling
;
479 // find the following header
480 next
= next
.nextSibling
;
481 while (next
.nodeType
!= 1)
482 next
= next
.nextSibling
;
484 if (next
&& next
.nodeType
== 1 &&
485 next
.nodeName
.match(/^[Hh][1-6]$/)) {
486 next
.oncontextmenu = function() {
487 document
.location
= editHref
;
495 function setupCheckboxShiftClick() {
496 if (document
.getElementsByTagName
) {
497 var uls
= document
.getElementsByTagName('ul');
498 var len
= uls
.length
;
499 for (var i
= 0; i
< len
; ++i
) {
500 addCheckboxClickHandlers(uls
[i
]);
505 function addCheckboxClickHandlers(ul
) {
506 if ( !ul
.childNodes
) {
509 var len
= ul
.childNodes
.length
;
514 ul
.lastCheckbox
= null;
515 for (var i
= 0; i
<len
; ++i
) {
516 var child
= ul
.childNodes
[i
];
517 if ( child
&& child
.childNodes
&& child
.childNodes
[0] ) {
518 var cb
= child
.childNodes
[0];
519 if ( !cb
.nodeName
|| cb
.nodeName
.toLowerCase() != 'input' ||
520 !cb
.type
|| cb
.type
.toLowerCase() != 'checkbox' ) {
523 cb
.index
= ul
.checkboxes
.push(cb
) - 1;
525 cb
.onmouseup
= checkboxMouseupHandler
;
530 function checkboxMouseupHandler(e
) {
531 if (typeof e
== 'undefined') {
534 if ( !e
.shiftKey
|| this.container
.lastCheckbox
=== null ) {
535 this.container
.lastCheckbox
= this.index
;
538 var endState
= !this.checked
;
539 if ( is_opera
) { // opera has already toggled the checkbox by this point
540 endState
= !endState
;
543 if ( this.index
< this.container
.lastCheckbox
) {
544 start
= this.index
+ 1;
545 finish
= this.container
.lastCheckbox
;
547 start
= this.container
.lastCheckbox
;
548 finish
= this.index
- 1;
550 for (var i
= start
; i
<= finish
; ++i
) {
551 this.container
.checkboxes
[i
].checked
= endState
;
553 this.container
.lastCheckbox
= this.index
;
557 function fillDestFilename() {
558 if (!document
.getElementById
)
560 var path
= document
.getElementById('wpUploadFile').value
;
561 // Find trailing part
562 var slash
= path
.lastIndexOf('/');
563 var backslash
= path
.lastIndexOf('\\');
565 if (slash
== -1 && backslash
== -1) {
567 } else if (slash
> backslash
) {
568 fname
= path
.substring(slash
+1, 10000);
570 fname
= path
.substring(backslash
+1, 10000);
573 // Capitalise first letter and replace spaces by underscores
574 fname
= fname
.charAt(0).toUpperCase().concat(fname
.substring(1,10000)).replace(/ /g
, '_');
577 var destFile
= document
.getElementById('wpDestFile');
579 destFile
.value
= fname
;
583 function considerChangingExpiryFocus() {
584 if (!document
.getElementById
)
586 var drop
= document
.getElementById('wpBlockExpiry');
589 var field
= document
.getElementById('wpBlockOther');
592 var opt
= drop
.value
;
594 field
.style
.display
= '';
596 field
.style
.display
= 'none';
599 function scrollEditBox() {
600 var editBoxEl
= document
.getElementById("wpTextbox1");
601 var scrollTopEl
= document
.getElementById("wpScrolltop");
602 var editFormEl
= document
.getElementById("editform");
604 if (editBoxEl
&& scrollTopEl
) {
605 if (scrollTopEl
.value
) editBoxEl
.scrollTop
= scrollTopEl
.value
;
606 editFormEl
.onsubmit = function() {
607 document
.getElementById("wpScrolltop").value
= document
.getElementById("wpTextbox1").scrollTop
;
612 hookEvent("load", scrollEditBox
);
614 function allmessagesfilter() {
615 text
= document
.getElementById('allmessagesinput').value
;
616 k
= document
.getElementById('allmessagestable');
619 var items
= k
.getElementsByTagName('span');
621 if ( text
.length
> allmessages_prev
.length
) {
622 for (var i
= items
.length
-1, j
= 0; i
>= 0; i
--) {
623 j
= allmessagesforeach(items
, i
, j
);
626 for (var i
= 0, j
= 0; i
< items
.length
; i
++) {
627 j
= allmessagesforeach(items
, i
, j
);
630 allmessages_prev
= text
;
633 function allmessagesforeach(items
, i
, j
) {
634 var hItem
= items
[i
].getAttribute('id');
635 if (hItem
.substring(0,17) == 'sp-allmessages-i-') {
636 if (items
[i
].firstChild
&& items
[i
].firstChild
.nodeName
== '#text' && items
[i
].firstChild
.nodeValue
.indexOf(text
) != -1) {
637 var itemA
= document
.getElementById( hItem
.replace('i', 'r1') );
638 var itemB
= document
.getElementById( hItem
.replace('i', 'r2') );
639 if ( itemA
.style
.display
!= '' ) {
640 var s
= "allmessageshider(\"" + hItem
.replace('i', 'r1') + "\", \"" + hItem
.replace('i', 'r2') + "\", '')";
641 var k
= window
.setTimeout(s
,j
++*5);
644 var itemA
= document
.getElementById( hItem
.replace('i', 'r1') );
645 var itemB
= document
.getElementById( hItem
.replace('i', 'r2') );
646 if ( itemA
.style
.display
!= 'none' ) {
647 var s
= "allmessageshider(\"" + hItem
.replace('i', 'r1') + "\", \"" + hItem
.replace('i', 'r2') + "\", 'none')";
648 var k
= window
.setTimeout(s
,j
++*5);
656 function allmessageshider(idA
, idB
, cstyle
) {
657 var itemA
= document
.getElementById( idA
);
658 var itemB
= document
.getElementById( idB
);
659 if (itemA
) { itemA
.style
.display
= cstyle
; }
660 if (itemB
) { itemB
.style
.display
= cstyle
; }
663 function allmessagesmodified() {
664 allmessages_modified
= !allmessages_modified
;
665 k
= document
.getElementById('allmessagestable');
667 var items
= k
.getElementsByTagName('tr');
668 for (var i
= 0, j
= 0; i
< items
.length
; i
++) {
669 if (!allmessages_modified
) {
670 if ( items
[i
].style
.display
!= '' ) {
671 var s
= "allmessageshider(\"" + items
[i
].getAttribute('id') + "\", null, '')";
672 var k
= window
.setTimeout(s
,j
++*5);
674 } else if (items
[i
].getAttribute('class') == 'def' && allmessages_modified
) {
675 if ( items
[i
].style
.display
!= 'none' ) {
676 var s
= "allmessageshider(\"" + items
[i
].getAttribute('id') + "\", null, 'none')";
677 var k
= window
.setTimeout(s
,j
++*5);
683 function allmessagesshow() {
684 k
= document
.getElementById('allmessagesfilter');
685 if (k
) { k
.style
.display
= ''; }
687 allmessages_prev
= '';
688 allmessages_modified
= false;
691 hookEvent("load", allmessagesshow
);