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
)
23 // Allows add-on scripts to add onload functions
24 onloadFuncts
[onloadFuncts
.length
] = hookFunct
;
27 function runOnloadHook()
29 // don't run anything below this for non-dom browsers
30 if ( doneOnloadHook
|| !( document
.getElementById
&& document
.getElementsByTagName
) )
39 // Run any added-on functions
40 for ( var i
= 0; i
< onloadFuncts
.length
; i
++ )
43 doneOnloadHook
= true;
46 function hookEvent( hookName
, hookFunct
)
48 if ( window
.addEventListener
)
49 addEventListener( hookName
, hookFunct
, false );
50 else if ( window
.attachEvent
)
51 attachEvent( "on" + hookName
, hookFunct
);
54 hookEvent( "load", runOnloadHook
);
56 // document.write special stylesheet links
57 if(typeof stylepath
!= 'undefined' && typeof skin
!= 'undefined') {
58 if (is_opera_preseven
) {
59 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'/'+skin
+'/Opera6Fixes.css">');
60 } else if (is_opera_seven
) {
61 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'/'+skin
+'/Opera7Fixes.css">');
62 } else if (is_khtml
) {
63 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'/'+skin
+'/KHTMLFixes.css">');
66 // Un-trap us from framesets
67 if( window
.top
!= window
) window
.top
.location
= window
.location
;
69 // for enhanced RecentChanges
70 function toggleVisibility( _levelId
, _otherId
, _linkId
) {
71 var thisLevel
= document
.getElementById( _levelId
);
72 var otherLevel
= document
.getElementById( _otherId
);
73 var linkLevel
= document
.getElementById( _linkId
);
74 if ( thisLevel
.style
.display
== 'none' ) {
75 thisLevel
.style
.display
= 'block';
76 otherLevel
.style
.display
= 'none';
77 linkLevel
.style
.display
= 'inline';
79 thisLevel
.style
.display
= 'none';
80 otherLevel
.style
.display
= 'inline';
81 linkLevel
.style
.display
= 'none';
86 // attach event handlers to the input elements on history page
87 function histrowinit () {
88 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
;
100 // check selection and tweak visibility/class onclick
101 function diffcheck() {
102 var dli
= false; // the li where the diff radio is checked
103 var oli
= false; // the li where the oldid radio is checked
104 hf
= document
.getElementById('pagehistory');
106 lis
= hf
.getElementsByTagName('li');
107 for (i
=0;i
<lis
.length
;i
++) {
108 inputs
=lis
[i
].getElementsByTagName('input');
109 if(inputs
[1] && inputs
[0]) {
110 if(inputs
[1].checked
|| inputs
[0].checked
) { // this row has a checked radio button
111 if(inputs
[1].checked
&& inputs
[0].checked
&& inputs
[0].value
== inputs
[1].value
) return false;
112 if(oli
) { // it's the second checked radio
113 if(inputs
[1].checked
) {
114 oli
.className
= "selected";
117 } else if (inputs
[0].checked
) {
120 if(inputs
[0].checked
) dli
= lis
[i
];
121 if(!oli
) inputs
[0].style
.visibility
= 'hidden';
122 if(dli
) inputs
[1].style
.visibility
= 'hidden';
123 lis
[i
].className
= "selected";
125 } else { // no radio is checked in this row
126 if(!oli
) inputs
[0].style
.visibility
= 'hidden';
127 else inputs
[0].style
.visibility
= 'visible';
128 if(dli
) inputs
[1].style
.visibility
= 'hidden';
129 else inputs
[1].style
.visibility
= 'visible';
130 lis
[i
].className
= "";
136 // generate toc from prefs form, fold sections
137 // XXX: needs testing on IE/Mac and safari
138 // more comments to follow
139 function tabbedprefs() {
140 var prefform
= document
.getElementById('preferences');
141 if(!prefform
|| !document
.createElement
) return;
142 if(prefform
.nodeName
.toLowerCase() == 'a') return; // Occasional IE problem
143 prefform
.className
= prefform
.className
+ 'jsprefs';
144 var sections
= new Array();
145 children
= prefform
.childNodes
;
147 for(i
=0;i
<children
.length
;i
++) {
148 if(children
[i
].nodeName
.toLowerCase() == 'fieldset') {
149 children
[i
].id
= 'prefsection-' + seci
;
150 children
[i
].className
= 'prefsection';
151 if(is_opera
|| is_khtml
) children
[i
].className
= 'prefsection operaprefsection';
152 legends
= children
[i
].getElementsByTagName('legend');
153 sections
[seci
] = new Object();
154 legends
[0].className
= 'mainLegend';
155 if(legends
[0] && legends
[0].firstChild
.nodeValue
)
156 sections
[seci
].text
= legends
[0].firstChild
.nodeValue
;
158 sections
[seci
].text
= '# ' + seci
;
159 sections
[seci
].secid
= children
[i
].id
;
161 if(sections
.length
!= 1) children
[i
].style
.display
= 'none';
162 else var selectedid
= children
[i
].id
;
165 var toc
= document
.createElement('ul');
167 toc
.selectedid
= selectedid
;
168 for(i
=0;i
<sections
.length
;i
++) {
169 var li
= document
.createElement('li');
170 if(i
== 0) li
.className
= 'selected';
171 var a
= document
.createElement('a');
172 a
.href
= '#' + sections
[i
].secid
;
173 a
.onmousedown
= a
.onclick
= uncoversection
;
174 a
.appendChild(document
.createTextNode(sections
[i
].text
));
175 a
.secid
= sections
[i
].secid
;
179 prefform
.parentNode
.insertBefore(toc
, prefform
.parentNode
.childNodes
[0]);
180 document
.getElementById('prefsubmit').id
= 'prefcontrol';
182 function uncoversection() {
183 oldsecid
= this.parentNode
.parentNode
.selectedid
;
184 newsec
= document
.getElementById(this.secid
);
185 if(oldsecid
!= this.secid
) {
186 ul
= document
.getElementById('preftoc');
187 document
.getElementById(oldsecid
).style
.display
= 'none';
188 newsec
.style
.display
= 'block';
189 ul
.selectedid
= this.secid
;
190 lis
= ul
.getElementsByTagName('li');
191 for(i
=0;i
< lis
.length
;i
++) {
192 lis
[i
].className
= '';
194 this.parentNode
.className
= 'selected';
200 // tz in format [+-]HHMM
201 function checkTimezone( tz
, msg
) {
202 var localclock
= new Date();
203 // returns negative offset from GMT in minutes
204 var tzRaw
= localclock
.getTimezoneOffset();
205 var tzHour
= Math
.floor( Math
.abs(tzRaw
) / 60);
206 var tzMin
= Math
.abs(tzRaw
) % 60;
207 var tzString
= ((tzRaw
>= 0) ? "-" : "+") + ((tzHour
< 10) ? "0" : "") + tzHour
+ ((tzMin
< 10) ? "0" : "") + tzMin
;
208 if( tz
!= tzString
) {
209 var junk
= msg
.split( '$1' );
210 document
.write( junk
[0] + "UTC" + tzString
+ junk
[1] );
213 function unhidetzbutton() {
214 tzb
= document
.getElementById('guesstimezonebutton')
215 if(tzb
) tzb
.style
.display
= 'inline';
218 // in [-]HH:MM format...
219 // won't yet work with non-even tzs
220 function fetchTimezone() {
221 // FIXME: work around Safari bug
222 var localclock
= new Date();
223 // returns negative offset from GMT in minutes
224 var tzRaw
= localclock
.getTimezoneOffset();
225 var tzHour
= Math
.floor( Math
.abs(tzRaw
) / 60);
226 var tzMin
= Math
.abs(tzRaw
) % 60;
227 var tzString
= ((tzRaw
>= 0) ? "-" : "") + ((tzHour
< 10) ? "0" : "") + tzHour
+
228 ":" + ((tzMin
< 10) ? "0" : "") + tzMin
;
232 function guessTimezone(box
) {
233 document
.getElementsByName("wpHourDiff")[0].value
= fetchTimezone();
236 function showTocToggle() {
237 if (document
.createTextNode
) {
238 // Uses DOM calls to avoid document.write + XHTML issues
240 var linkHolder
= document
.getElementById('toctitle')
241 if (!linkHolder
) return;
243 var outerSpan
= document
.createElement('span');
244 outerSpan
.className
= 'toctoggle';
246 var toggleLink
= document
.createElement('a');
247 toggleLink
.id
= 'togglelink';
248 toggleLink
.className
= 'internal';
249 toggleLink
.href
= 'javascript:toggleToc()';
250 toggleLink
.appendChild(document
.createTextNode(tocHideText
));
252 outerSpan
.appendChild(document
.createTextNode('['));
253 outerSpan
.appendChild(toggleLink
);
254 outerSpan
.appendChild(document
.createTextNode(']'));
256 linkHolder
.appendChild(document
.createTextNode(' '));
257 linkHolder
.appendChild(outerSpan
);
259 var cookiePos
= document
.cookie
.indexOf("hidetoc=");
260 if (cookiePos
> -1 && document
.cookie
.charAt(cookiePos
+ 8) == 1)
265 function changeText(el
, newText
) {
266 // Safari work around
268 el
.innerText
= newText
;
269 else if (el
.firstChild
&& el
.firstChild
.nodeValue
)
270 el
.firstChild
.nodeValue
= newText
;
273 function toggleToc() {
274 var toc
= document
.getElementById('toc').getElementsByTagName('ul')[0];
275 var toggleLink
= document
.getElementById('togglelink')
277 if(toc
&& toggleLink
&& toc
.style
.display
== 'none') {
278 changeText(toggleLink
, tocHideText
);
279 toc
.style
.display
= 'block';
280 document
.cookie
= "hidetoc=0";
282 changeText(toggleLink
, tocShowText
);
283 toc
.style
.display
= 'none';
284 document
.cookie
= "hidetoc=1";
288 // this function generates the actual toolbar buttons with localized text
289 // we use it to avoid creating the toolbar where javascript is not enabled
290 function addButton(imageFile
, speedTip
, tagOpen
, tagClose
, sampleText
) {
292 // Don't generate buttons for browsers which don't fully
294 if(!document
.selection
&& !is_gecko
) {
297 imageFile
=escapeQuotesHTML(imageFile
);
298 speedTip
=escapeQuotesHTML(speedTip
);
299 tagOpen
=escapeQuotes(tagOpen
);
300 tagClose
=escapeQuotes(tagClose
);
301 sampleText
=escapeQuotes(sampleText
);
304 document
.write("<a href=\"javascript:insertTags");
305 document
.write("('"+tagOpen
+"','"+tagClose
+"','"+sampleText
+"');\">");
306 document
.write("<img width=\"23\" height=\"22\" src=\""+imageFile
+"\" border=\"0\" alt=\""+speedTip
+"\" title=\""+speedTip
+"\""+mouseOver
+">");
307 document
.write("</a>");
311 function escapeQuotes(text
) {
312 var re
=new RegExp("'","g");
313 text
=text
.replace(re
,"\\'");
314 re
=new RegExp("\\n","g");
315 text
=text
.replace(re
,"\\n");
316 return escapeQuotesHTML(text
);
319 function escapeQuotesHTML(text
) {
320 var re
=new RegExp('&',"g");
321 text
=text
.replace(re
,"&");
322 var re
=new RegExp('"',"g");
323 text
=text
.replace(re
,""");
324 var re
=new RegExp('<',"g");
325 text
=text
.replace(re
,"<");
326 var re
=new RegExp('>',"g");
327 text
=text
.replace(re
,">");
331 // apply tagOpen/tagClose to selection in textarea,
332 // use sampleText instead of selection if there is none
333 // copied and adapted from phpBB
334 function insertTags(tagOpen
, tagClose
, sampleText
) {
336 var txtarea
= document
.editform
.wpTextbox1
;
338 if(document
.selection
&& !is_gecko
) {
339 var theSelection
= document
.selection
.createRange().text
;
340 if(!theSelection
) { theSelection
=sampleText
;}
342 if(theSelection
.charAt(theSelection
.length
- 1) == " "){// exclude ending space char, if any
343 theSelection
= theSelection
.substring(0, theSelection
.length
- 1);
344 document
.selection
.createRange().text
= tagOpen
+ theSelection
+ tagClose
+ " ";
346 document
.selection
.createRange().text
= tagOpen
+ theSelection
+ tagClose
;
350 } else if(txtarea
.selectionStart
|| txtarea
.selectionStart
== '0') {
351 var replaced
= false;
352 var startPos
= txtarea
.selectionStart
;
353 var endPos
= txtarea
.selectionEnd
;
354 if(endPos
-startPos
) replaced
=true;
355 var scrollTop
=txtarea
.scrollTop
;
356 var myText
= (txtarea
.value
).substring(startPos
, endPos
);
357 if(!myText
) { myText
=sampleText
;}
358 if(myText
.charAt(myText
.length
- 1) == " "){ // exclude ending space char, if any
359 subst
= tagOpen
+ myText
.substring(0, (myText
.length
- 1)) + tagClose
+ " ";
361 subst
= tagOpen
+ myText
+ tagClose
;
363 txtarea
.value
= txtarea
.value
.substring(0, startPos
) + subst
+
364 txtarea
.value
.substring(endPos
, txtarea
.value
.length
);
368 var cPos
=startPos
+(tagOpen
.length
+myText
.length
+tagClose
.length
);
369 txtarea
.selectionStart
=cPos
;
370 txtarea
.selectionEnd
=cPos
;
372 txtarea
.selectionStart
=startPos
+tagOpen
.length
;
373 txtarea
.selectionEnd
=startPos
+tagOpen
.length
+myText
.length
;
375 txtarea
.scrollTop
=scrollTop
;
377 // All other browsers get no toolbar.
378 // There was previously support for a crippled "help"
379 // bar, but that caused more problems than it solved.
381 // reposition cursor if possible
382 if (txtarea
.createTextRange
) txtarea
.caretPos
= document
.selection
.createRange().duplicate();
386 if(typeof ta
== "undefined" || !ta
) return;
388 if(is_safari
|| navigator
.userAgent
.toLowerCase().indexOf( 'mac' ) + 1
389 || navigator
.userAgent
.toLowerCase().indexOf( 'konqueror' ) + 1 ) pref
= 'control-';
390 if(is_opera
) pref
= 'shift-esc-';
393 n
= document
.getElementById(id
);
395 // Are we putting accesskey in it
396 if(ta
[id
][0].length
> 0) {
397 // Is this object a object? If not assume it's the next child.
399 if ( n
.nodeName
.toLowerCase() == "a" ) {
406 a
.accessKey
= ta
[id
][0];
407 ak
= ' ['+pref
+ta
[id
][0]+']';
410 // We don't care what type the object is when assigning tooltip
416 a
.title
= ta
[id
][1]+ak
;
422 function setupRightClickEdit() {
423 if( document
.getElementsByTagName
) {
424 var divs
= document
.getElementsByTagName( 'div' );
425 for( var i
= 0; i
< divs
.length
; i
++ ) {
427 if( el
.className
== 'editsection' ) {
428 addRightClickEditHandler( el
);
434 function addRightClickEditHandler( el
) {
435 for( var i
= 0; i
< el
.childNodes
.length
; i
++ ) {
436 var link
= el
.childNodes
[i
];
437 if( link
.nodeType
== 1 && link
.nodeName
.toLowerCase() == 'a' ) {
438 var editHref
= link
.getAttribute( 'href' );
440 // find the following a
441 var next
= el
.nextSibling
;
442 while( next
.nodeType
!= 1 )
443 next
= next
.nextSibling
;
445 // find the following header
446 next
= next
.nextSibling
;
447 while( next
.nodeType
!= 1 )
448 next
= next
.nextSibling
;
450 if( next
&& next
.nodeType
== 1 &&
451 next
.nodeName
.match( /^[Hh][1-6]$/ ) ) {
452 next
.oncontextmenu = function() {
453 document
.location
= editHref
;
461 function fillDestFilename() {
462 if (!document
.getElementById
) return;
463 var path
= document
.getElementById('wpUploadFile').value
;
464 // Find trailing part
465 var slash
= path
.lastIndexOf( '/' );
466 var backslash
= path
.lastIndexOf( '\\' );
468 if ( slash
== -1 && backslash
== -1 ) {
470 } else if ( slash
> backslash
) {
471 fname
= path
.substring( slash
+1, 10000 );
473 fname
= path
.substring( backslash
+1, 10000 );
476 // Capitalise first letter and replace spaces by underscores
477 fname
= fname
.charAt(0).toUpperCase().concat(fname
.substring(1,10000)).replace( / /g
, '_' );
480 var destFile
= document
.getElementById('wpDestFile');
481 if (destFile
) destFile
.value
= fname
;
485 function considerChangingExpiryFocus() {
486 if (!document
.getElementById
) return;
487 var drop
= document
.getElementById('wpBlockExpiry');
489 var field
= document
.getElementById('wpBlockOther');
491 var opt
= drop
.value
;
493 field
.style
.display
= '';
495 field
.style
.display
= 'none';
498 function scrollEditBox() {
500 var editBoxEl
= document
.getElementById("wpTextbox1");
501 var scrollTopEl
= document
.getElementById("wpScrolltop");
502 var editFormEl
= document
.getElementById("editform");
504 if ( editBoxEl
&& scrollTopEl
) {
506 if (scrollTopEl
.value
) editBoxEl
.scrollTop
= scrollTopEl
.value
;
507 editFormEl
.onsubmit = function() {
508 document
.getElementById("wpScrolltop").value
= document
.getElementById("wpTextbox1").scrollTop
;
513 hookEvent( "load", scrollEditBox
);