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)
15 function onloadhook () {
16 // don't run anything below this for non-dom browsers
17 if(!(document
.getElementById
&& document
.getElementsByTagName
)) return;
23 if (window
.addEventListener
) window
.addEventListener("load",onloadhook
,false);
24 else if (window
.attachEvent
) window
.attachEvent("onload",onloadhook
);
27 // document.write special stylesheet links
28 if(typeof stylepath
!= 'undefined' && typeof skin
!= 'undefined') {
29 if (is_opera_preseven
) {
30 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'/'+skin
+'/Opera6Fixes.css">');
31 } else if (is_opera_seven
) {
32 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'/'+skin
+'/Opera7Fixes.css">');
33 } else if (is_khtml
) {
34 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'/'+skin
+'/KHTMLFixes.css">');
37 // Un-trap us from framesets
38 if( window
.top
!= window
) window
.top
.location
= window
.location
;
40 // for enhanced RecentChanges
41 function toggleVisibility( _levelId
, _otherId
, _linkId
) {
42 var thisLevel
= document
.getElementById( _levelId
);
43 var otherLevel
= document
.getElementById( _otherId
);
44 var linkLevel
= document
.getElementById( _linkId
);
45 if ( thisLevel
.style
.display
== 'none' ) {
46 thisLevel
.style
.display
= 'block';
47 otherLevel
.style
.display
= 'none';
48 linkLevel
.style
.display
= 'inline';
50 thisLevel
.style
.display
= 'none';
51 otherLevel
.style
.display
= 'inline';
52 linkLevel
.style
.display
= 'none';
57 // attach event handlers to the input elements on history page
58 function histrowinit () {
59 hf
= document
.getElementById('pagehistory');
61 lis
= hf
.getElementsByTagName('li');
62 for (i
=0;i
<lis
.length
;i
++) {
63 inputs
=lis
[i
].getElementsByTagName('input');
64 if(inputs
[0] && inputs
[1]) {
65 inputs
[0].onclick
= diffcheck
;
66 inputs
[1].onclick
= diffcheck
;
71 // check selection and tweak visibility/class onclick
72 function diffcheck() {
73 var dli
= false; // the li where the diff radio is checked
74 var oli
= false; // the li where the oldid radio is checked
75 hf
= document
.getElementById('pagehistory');
77 lis
= hf
.getElementsByTagName('li');
78 for (i
=0;i
<lis
.length
;i
++) {
79 inputs
=lis
[i
].getElementsByTagName('input');
80 if(inputs
[1] && inputs
[0]) {
81 if(inputs
[1].checked
|| inputs
[0].checked
) { // this row has a checked radio button
82 if(inputs
[1].checked
&& inputs
[0].checked
&& inputs
[0].value
== inputs
[1].value
) return false;
83 if(oli
) { // it's the second checked radio
84 if(inputs
[1].checked
) {
85 oli
.className
= "selected";
88 } else if (inputs
[0].checked
) {
91 if(inputs
[0].checked
) dli
= lis
[i
];
92 if(!oli
) inputs
[0].style
.visibility
= 'hidden';
93 if(dli
) inputs
[1].style
.visibility
= 'hidden';
94 lis
[i
].className
= "selected";
96 } else { // no radio is checked in this row
97 if(!oli
) inputs
[0].style
.visibility
= 'hidden';
98 else inputs
[0].style
.visibility
= 'visible';
99 if(dli
) inputs
[1].style
.visibility
= 'hidden';
100 else inputs
[1].style
.visibility
= 'visible';
101 lis
[i
].className
= "";
107 // generate toc from prefs form, fold sections
108 // XXX: needs testing on IE/Mac and safari
109 // more comments to follow
110 function tabbedprefs() {
111 prefform
= document
.getElementById('preferences');
112 if(!prefform
|| !document
.createElement
) return;
113 if(prefform
.nodeName
.toLowerCase() == 'a') return; // Occasional IE problem
114 prefform
.className
= prefform
.className
+ 'jsprefs';
115 var sections
= new Array();
116 children
= prefform
.childNodes
;
118 for(i
=0;i
<children
.length
;i
++) {
119 if(children
[i
].nodeName
.toLowerCase().indexOf('fieldset') != -1) {
120 children
[i
].id
= 'prefsection-' + seci
;
121 children
[i
].className
= 'prefsection';
122 if(is_opera
|| is_khtml
) children
[i
].className
= 'prefsection operaprefsection';
123 legends
= children
[i
].getElementsByTagName('legend');
124 sections
[seci
] = new Object();
125 if(legends
[0] && legends
[0].firstChild
.nodeValue
)
126 sections
[seci
].text
= legends
[0].firstChild
.nodeValue
;
128 sections
[seci
].text
= '# ' + seci
;
129 sections
[seci
].secid
= children
[i
].id
;
131 if(sections
.length
!= 1) children
[i
].style
.display
= 'none';
132 else var selectedid
= children
[i
].id
;
135 var toc
= document
.createElement('ul');
137 toc
.selectedid
= selectedid
;
138 for(i
=0;i
<sections
.length
;i
++) {
139 var li
= document
.createElement('li');
140 if(i
== 0) li
.className
= 'selected';
141 var a
= document
.createElement('a');
142 a
.href
= '#' + sections
[i
].secid
;
143 a
.onclick
= uncoversection
;
144 a
.appendChild(document
.createTextNode(sections
[i
].text
));
145 a
.secid
= sections
[i
].secid
;
149 prefform
.insertBefore(toc
, children
[0]);
150 document
.getElementById('prefsubmit').id
= 'prefcontrol';
152 function uncoversection() {
153 oldsecid
= this.parentNode
.parentNode
.selectedid
;
154 newsec
= document
.getElementById(this.secid
);
155 if(oldsecid
!= this.secid
) {
156 ul
= document
.getElementById('preftoc');
157 document
.getElementById(oldsecid
).style
.display
= 'none';
158 newsec
.style
.display
= 'block';
159 ul
.selectedid
= this.secid
;
160 lis
= ul
.getElementsByTagName('li');
161 for(i
=0;i
< lis
.length
;i
++) {
162 lis
[i
].className
= '';
164 this.parentNode
.className
= 'selected';
170 // tz in format [+-]HHMM
171 function checkTimezone( tz
, msg
) {
172 var localclock
= new Date();
173 // returns negative offset from GMT in minutes
174 var tzRaw
= localclock
.getTimezoneOffset();
175 var tzHour
= Math
.floor( Math
.abs(tzRaw
) / 60);
176 var tzMin
= Math
.abs(tzRaw
) % 60;
177 var tzString
= ((tzRaw
>= 0) ? "-" : "+") + ((tzHour
< 10) ? "0" : "") + tzHour
+ ((tzMin
< 10) ? "0" : "") + tzMin
;
178 if( tz
!= tzString
) {
179 var junk
= msg
.split( '$1' );
180 document
.write( junk
[0] + "UTC" + tzString
+ junk
[1] );
183 function unhidetzbutton() {
184 tzb
= document
.getElementById('guesstimezonebutton')
185 if(tzb
) tzb
.style
.display
= 'inline';
188 // in [-]HH:MM format...
189 // won't yet work with non-even tzs
190 function fetchTimezone() {
191 // FIXME: work around Safari bug
192 var localclock
= new Date();
193 // returns negative offset from GMT in minutes
194 var tzRaw
= localclock
.getTimezoneOffset();
195 var tzHour
= Math
.floor( Math
.abs(tzRaw
) / 60);
196 var tzMin
= Math
.abs(tzRaw
) % 60;
197 var tzString
= ((tzRaw
>= 0) ? "-" : "") + ((tzHour
< 10) ? "0" : "") + tzHour
+
198 ":" + ((tzMin
< 10) ? "0" : "") + tzMin
;
202 function guessTimezone(box
) {
203 document
.preferences
.wpHourDiff
.value
= fetchTimezone();
206 function showTocToggle() {
207 if (document
.createTextNode
) {
208 // Uses DOM calls to avoid document.write + XHTML issues
210 var linkHolder
= document
.getElementById('toctitle')
211 if (!linkHolder
) return;
213 var outerSpan
= document
.createElement('span');
214 outerSpan
.className
= 'toctoggle';
216 var toggleLink
= document
.createElement('a');
217 toggleLink
.id
= 'togglelink';
218 toggleLink
.className
= 'internal';
219 toggleLink
.href
= 'javascript:toggleToc()';
220 toggleLink
.appendChild(document
.createTextNode(tocHideText
));
222 outerSpan
.appendChild(document
.createTextNode('['));
223 outerSpan
.appendChild(toggleLink
);
224 outerSpan
.appendChild(document
.createTextNode(']'));
226 linkHolder
.appendChild(document
.createTextNode(' '));
227 linkHolder
.appendChild(outerSpan
);
229 var cookiePos
= document
.cookie
.indexOf("hidetoc=");
230 if (cookiePos
> -1 && document
.cookie
.charAt(cookiePos
+ 8) == 1)
235 function changeText(el
, newText
) {
236 // Safari work around
238 el
.innerText
= newText
;
239 else if (el
.firstChild
&& el
.firstChild
.nodeValue
)
240 el
.firstChild
.nodeValue
= newText
;
243 function toggleToc() {
244 var toc
= document
.getElementById('toc').getElementsByTagName('ul')[0];
245 var toggleLink
= document
.getElementById('togglelink')
247 if(toc
&& toggleLink
&& toc
.style
.display
== 'none') {
248 changeText(toggleLink
, tocHideText
);
249 toc
.style
.display
= 'block';
250 document
.cookie
= "hidetoc=0";
252 changeText(toggleLink
, tocShowText
);
253 toc
.style
.display
= 'none';
254 document
.cookie
= "hidetoc=1";
258 // this function generates the actual toolbar buttons with localized text
259 // we use it to avoid creating the toolbar where javascript is not enabled
260 function addButton(imageFile
, speedTip
, tagOpen
, tagClose
, sampleText
) {
262 // Don't generate buttons for browsers which don't fully
264 if(!document
.selection
&& !is_gecko
) {
267 imageFile
=escapeQuotesHTML(imageFile
);
268 speedTip
=escapeQuotesHTML(speedTip
);
269 tagOpen
=escapeQuotes(tagOpen
);
270 tagClose
=escapeQuotes(tagClose
);
271 sampleText
=escapeQuotes(sampleText
);
274 document
.write("<a href=\"javascript:insertTags");
275 document
.write("('"+tagOpen
+"','"+tagClose
+"','"+sampleText
+"');\">");
276 document
.write("<img width=\"23\" height=\"22\" src=\""+imageFile
+"\" border=\"0\" alt=\""+speedTip
+"\" title=\""+speedTip
+"\""+mouseOver
+">");
277 document
.write("</a>");
281 function escapeQuotes(text
) {
282 var re
=new RegExp("'","g");
283 text
=text
.replace(re
,"\\'");
284 re
=new RegExp("\\n","g");
285 text
=text
.replace(re
,"\\n");
286 return escapeQuotesHTML(text
);
289 function escapeQuotesHTML(text
) {
290 var re
=new RegExp('&',"g");
291 text
=text
.replace(re
,"&");
292 var re
=new RegExp('"',"g");
293 text
=text
.replace(re
,""");
294 var re
=new RegExp('<',"g");
295 text
=text
.replace(re
,"<");
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
;
340 // All other browsers get no toolbar.
341 // There was previously support for a crippled "help"
342 // bar, but that caused more problems than it solved.
344 // reposition cursor if possible
345 if (txtarea
.createTextRange
) txtarea
.caretPos
= document
.selection
.createRange().duplicate();
349 if(typeof ta
== "undefined" || !ta
) return;
351 if(is_safari
|| navigator
.userAgent
.toLowerCase().indexOf( 'mac' ) + 1
352 || navigator
.userAgent
.toLowerCase().indexOf( 'konqueror' ) + 1 ) pref
= 'control-';
353 if(is_opera
) pref
= 'shift-esc-';
356 n
= document
.getElementById(id
);
358 // Are we putting accesskey in it
359 if(ta
[id
][0].length
> 0) {
360 // Is this object a object? If not assume it's the next child.
362 if ( n
.nodeName
.toLowerCase() == "a" ) {
369 a
.accessKey
= ta
[id
][0];
370 ak
= ' ['+pref
+ta
[id
][0]+']';
373 // We don't care what type the object is when assigning tooltip
379 a
.title
= ta
[id
][1]+ak
;
385 function setupRightClickEdit() {
386 if( document
.getElementsByTagName
) {
387 var divs
= document
.getElementsByTagName( 'div' );
388 for( var i
= 0; i
< divs
.length
; i
++ ) {
390 if( el
.className
== 'editsection' ) {
391 addRightClickEditHandler( el
);
397 function addRightClickEditHandler( el
) {
398 for( var i
= 0; i
< el
.childNodes
.length
; i
++ ) {
399 var link
= el
.childNodes
[i
];
400 if( link
.nodeType
== 1 && link
.nodeName
.toLowerCase() == 'a' ) {
401 var editHref
= link
.getAttribute( 'href' );
403 // find the following a
404 var next
= el
.nextSibling
;
405 while( next
.nodeType
!= 1 )
406 next
= next
.nextSibling
;
408 // find the following header
409 next
= next
.nextSibling
;
410 while( next
.nodeType
!= 1 )
411 next
= next
.nextSibling
;
413 if( next
&& next
.nodeType
== 1 &&
414 next
.nodeName
.match( /^[Hh][1-6]$/ ) ) {
415 next
.oncontextmenu = function() {
416 document
.location
= editHref
;
424 function fillDestFilename() {
425 if (!document
.getElementById
) return;
426 var path
= document
.getElementById('wpUploadFile').value
;
427 // Find trailing part
428 var slash
= path
.lastIndexOf( '/' );
429 var backslash
= path
.lastIndexOf( '\\' );
431 if ( slash
== -1 && backslash
== -1 ) {
433 } else if ( slash
> backslash
) {
434 fname
= path
.substring( slash
+1, 10000 );
436 fname
= path
.substring( backslash
+1, 10000 );
439 // Capitalise first letter and replace spaces by underscores
440 fname
= fname
.charAt(0).toUpperCase().concat(fname
.substring(1,10000)).replace( / /g
, '_' );
443 var destFile
= document
.getElementById('wpDestFile');
444 if (destFile
) destFile
.value
= fname
;
448 function considerChangingExpiryFocus() {
449 if (!document
.getElementById
) return;
450 var drop
= document
.getElementById('wpBlockExpiry');
452 var field
= document
.getElementById('wpBlockOther');
454 var opt
= drop
.value
;
456 field
.style
.display
= '';
458 field
.style
.display
= 'none';