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_preseven
= (window
.opera
&& !document
.childNodes
);
14 var is_opera_seven
= (window
.opera
&& document
.childNodes
);
17 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
18 function onloadhook () {
19 // don't run anything below this for non-dom browsers
20 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 function addcss ( stylepath
) {
31 if (is_opera_preseven
) {
32 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'Opera6Fixes.css">');
33 } else if (is_opera_seven
) {
34 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'Opera7Fixes.css">');
35 } else if (is_khtml
) {
36 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'KHTMLFixes.css">');
41 // Un-trap us from framesets
42 if( window
.top
!= window
) window
.top
.location
= window
.location
;
44 // for enhanced RecentChanges
45 function toggleVisibility( _levelId
, _otherId
, _linkId
) {
46 var thisLevel
= document
.getElementById( _levelId
);
47 var otherLevel
= document
.getElementById( _otherId
);
48 var linkLevel
= document
.getElementById( _linkId
);
49 if ( thisLevel
.style
.display
== 'none' ) {
50 thisLevel
.style
.display
= 'block';
51 otherLevel
.style
.display
= 'none';
52 linkLevel
.style
.display
= 'inline';
54 thisLevel
.style
.display
= 'none';
55 otherLevel
.style
.display
= 'inline';
56 linkLevel
.style
.display
= 'none';
61 // attach event handlers to the input elements on history page
62 function histrowinit () {
63 hf
= document
.getElementById('pagehistory');
65 lis
= hf
.getElementsByTagName('LI');
66 for (i
=0;i
<lis
.length
;i
++) {
67 inputs
=lis
[i
].getElementsByTagName('INPUT');
68 if(inputs
[0] && inputs
[1]) {
69 inputs
[0].onclick
= diffcheck
;
70 inputs
[1].onclick
= diffcheck
;
75 // check selection and tweak visibility/class onclick
76 function diffcheck() {
77 var dli
= false; // the li where the diff radio is checked
78 var oli
= false; // the li where the oldid radio is checked
79 hf
= document
.getElementById('pagehistory');
81 lis
= hf
.getElementsByTagName('LI');
82 for (i
=0;i
<lis
.length
;i
++) {
83 inputs
=lis
[i
].getElementsByTagName('INPUT');
84 if(inputs
[1] && inputs
[0]) {
85 if(inputs
[1].checked
|| inputs
[0].checked
) { // this row has a checked radio button
86 if(inputs
[1].checked
&& inputs
[0].checked
&& inputs
[0].value
== inputs
[1].value
) return false;
87 if(oli
) { // it's the second checked radio
88 if(inputs
[1].checked
) {
89 oli
.className
= "selected";
92 } else if (inputs
[0].checked
) {
95 if(inputs
[0].checked
) dli
= lis
[i
];
96 if(!oli
) inputs
[0].style
.visibility
= 'hidden';
97 if(dli
) inputs
[1].style
.visibility
= 'hidden';
98 lis
[i
].className
= "selected";
100 } else { // no radio is checked in this row
101 if(!oli
) inputs
[0].style
.visibility
= 'hidden';
102 else inputs
[0].style
.visibility
= 'visible';
103 if(dli
) inputs
[1].style
.visibility
= 'hidden';
104 else inputs
[1].style
.visibility
= 'visible';
105 lis
[i
].className
= "";
111 // generate toc from prefs form, fold sections
112 // XXX: needs testing on IE/Mac and safari
113 // more comments to follow
114 function tabbedprefs() {
115 prefform
= document
.getElementById('preferences');
116 if(!prefform
|| !document
.createElement
) return;
117 prefform
.className
= prefform
.className
+ 'jsprefs';
118 var sections
= new Array();
119 children
= prefform
.childNodes
;
121 for(i
=0;i
<children
.length
;i
++) {
122 if(children
[i
].nodeName
.indexOf('FIELDSET') != -1) {
123 children
[i
].id
= 'prefsection-' + seci
;
124 children
[i
].className
= 'prefsection';
125 if(is_opera
|| is_khtml
) children
[i
].className
= 'prefsection operaprefsection';
126 legends
= children
[i
].getElementsByTagName('LEGEND');
127 sections
[seci
] = new Object();
128 if(legends
[0] && legends
[0].firstChild
.nodeValue
)
129 sections
[seci
].text
= legends
[0].firstChild
.nodeValue
;
131 sections
[seci
].text
= '# ' + seci
;
132 sections
[seci
].secid
= children
[i
].id
;
134 if(sections
.length
!= 1) children
[i
].style
.display
= 'none';
135 else var selectedid
= children
[i
].id
;
138 var toc
= document
.createElement('UL');
140 toc
.selectedid
= selectedid
;
141 for(i
=0;i
<sections
.length
;i
++) {
142 var li
= document
.createElement('LI');
143 if(i
== 0) li
.className
= 'selected';
144 var a
= document
.createElement('A');
145 a
.href
= '#' + sections
[i
].secid
;
146 a
.onclick
= uncoversection
;
147 a
.innerHTML
= sections
[i
].text
;
148 a
.secid
= sections
[i
].secid
;
152 prefform
.insertBefore(toc
, children
[0]);
153 document
.getElementById('prefsubmit').id
= 'prefcontrol';
155 function uncoversection() {
156 oldsecid
= this.parentNode
.parentNode
.selectedid
;
157 newsec
= document
.getElementById(this.secid
);
158 if(oldsecid
!= this.secid
) {
159 ul
= document
.getElementById('preftoc');
160 document
.getElementById(oldsecid
).style
.display
= 'none';
161 newsec
.style
.display
= 'block';
162 ul
.selectedid
= this.secid
;
163 lis
= ul
.getElementsByTagName('LI');
164 for(i
=0;i
< lis
.length
;i
++) {
165 lis
[i
].className
= '';
167 this.parentNode
.className
= 'selected';
173 // tz in format [+-]HHMM
174 function checkTimezone( tz
, msg
) {
175 var localclock
= new Date();
176 // returns negative offset from GMT in minutes
177 var tzRaw
= localclock
.getTimezoneOffset();
178 var tzHour
= Math
.floor( Math
.abs(tzRaw
) / 60);
179 var tzMin
= Math
.abs(tzRaw
) % 60;
180 var tzString
= ((tzRaw
>= 0) ? "-" : "+") + ((tzHour
< 10) ? "0" : "") + tzHour
+ ((tzMin
< 10) ? "0" : "") + tzMin
;
181 if( tz
!= tzString
) {
182 var junk
= msg
.split( '$1' );
183 document
.write( junk
[0] + "UTC" + tzString
+ junk
[1] );
186 function unhidetzbutton() {
187 tzb
= document
.getElementById('guesstimezonebutton')
188 if(tzb
) tzb
.style
.display
= 'inline';
191 // in [-]HH:MM format...
192 // won't yet work with non-even tzs
193 function fetchTimezone() {
194 // FIXME: work around Safari bug
195 var localclock
= new Date();
196 // returns negative offset from GMT in minutes
197 var tzRaw
= localclock
.getTimezoneOffset();
198 var tzHour
= Math
.floor( Math
.abs(tzRaw
) / 60);
199 var tzMin
= Math
.abs(tzRaw
) % 60;
200 var tzString
= ((tzRaw
>= 0) ? "-" : "") + ((tzHour
< 10) ? "0" : "") + tzHour
+
201 ":" + ((tzMin
< 10) ? "0" : "") + tzMin
;
205 function guessTimezone(box
) {
206 document
.preferences
.wpHourDiff
.value
= fetchTimezone();
209 function showTocToggle(show
,hide
) {
210 if(document
.getElementById
) {
211 document
.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleToc()" class="internal">' +
212 '<span id="showlink" style="display:none;">' + show
+ '</span>' +
213 '<span id="hidelink">' + hide
+ '</span>'
219 function toggleToc() {
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';
229 tocWas
= toc
.style
.display
;
230 toc
.style
.display
= 'none';
231 hidelink
.style
.display
='none';
232 showlink
.style
.display
='';
237 // this function generates the actual toolbar buttons with localized text
238 // we use it to avoid creating the toolbar where javascript is not enabled
239 function addButton(imageFile
, speedTip
, tagOpen
, tagClose
, sampleText
) {
241 speedTip
=escapeQuotes(speedTip
);
242 tagOpen
=escapeQuotes(tagOpen
);
243 tagClose
=escapeQuotes(tagClose
);
244 sampleText
=escapeQuotes(sampleText
);
247 // we can't change the selection, so we show example texts
248 // when moving the mouse instead, until the first button is clicked
249 if(!document
.selection
&& !is_gecko
) {
250 // filter backslashes so it can be shown in the infobox
251 var re
=new RegExp("\\\\n","g");
252 tagOpen
=tagOpen
.replace(re
,"");
253 tagClose
=tagClose
.replace(re
,"");
254 mouseOver
= "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen
+sampleText
+tagClose
+"'};\"";
257 document
.write("<a href=\"javascript:insertTags");
258 document
.write("('"+tagOpen
+"','"+tagClose
+"','"+sampleText
+"');\">");
260 document
.write("<img width=\"23\" height=\"22\" src=\""+imageFile
+"\" border=\"0\" ALT=\""+speedTip
+"\" TITLE=\""+speedTip
+"\""+mouseOver
+">");
261 document
.write("</a>");
265 function addInfobox(infoText
,text_alert
) {
266 alertText
=text_alert
;
267 var clientPC
= navigator
.userAgent
.toLowerCase(); // Get client info
269 var re
=new RegExp("\\\\n","g");
270 alertText
=alertText
.replace(re
,"\n");
272 // if no support for changing selection, add a small copy & paste field
273 // document.selection is an IE-only property. The full toolbar works in IE and
274 // Gecko-based browsers.
275 if(!document
.selection
&& !is_gecko
) {
276 infoText
=escapeQuotesHTML(infoText
);
277 document
.write("<form name='infoform' id='infoform'>"+
278 "<input size=80 id='infobox' name='infobox' value=\""+
279 infoText
+"\" READONLY></form>");
284 function escapeQuotes(text
) {
285 var re
=new RegExp("'","g");
286 text
=text
.replace(re
,"\\'");
287 re
=new RegExp('"',"g");
288 text
=text
.replace(re
,'"');
289 re
=new RegExp("\\n","g");
290 text
=text
.replace(re
,"\\n");
294 function escapeQuotesHTML(text
) {
295 var re
=new RegExp('"',"g");
296 text
=text
.replace(re
,""");
300 // apply tagOpen/tagClose to selection in textarea,
301 // use sampleText instead of selection if there is none
302 // copied and adapted from phpBB
303 function insertTags(tagOpen
, tagClose
, sampleText
) {
305 var txtarea
= document
.editform
.wpTextbox1
;
307 if(document
.selection
&& !is_gecko
) {
308 var theSelection
= document
.selection
.createRange().text
;
309 if(!theSelection
) { theSelection
=sampleText
;}
311 if(theSelection
.charAt(theSelection
.length
- 1) == " "){// exclude ending space char, if any
312 theSelection
= theSelection
.substring(0, theSelection
.length
- 1);
313 document
.selection
.createRange().text
= tagOpen
+ theSelection
+ tagClose
+ " ";
315 document
.selection
.createRange().text
= tagOpen
+ theSelection
+ tagClose
;
319 } else if(txtarea
.selectionStart
|| txtarea
.selectionStart
== '0') {
320 var startPos
= txtarea
.selectionStart
;
321 var endPos
= txtarea
.selectionEnd
;
322 var scrollTop
=txtarea
.scrollTop
;
323 var myText
= (txtarea
.value
).substring(startPos
, endPos
);
324 if(!myText
) { myText
=sampleText
;}
325 if(myText
.charAt(myText
.length
- 1) == " "){ // exclude ending space char, if any
326 subst
= tagOpen
+ myText
.substring(0, (myText
.length
- 1)) + tagClose
+ " ";
328 subst
= tagOpen
+ myText
+ tagClose
;
330 txtarea
.value
= txtarea
.value
.substring(0, startPos
) + subst
+
331 txtarea
.value
.substring(endPos
, txtarea
.value
.length
);
334 var cPos
=startPos
+(tagOpen
.length
+myText
.length
+tagClose
.length
);
335 txtarea
.selectionStart
=cPos
;
336 txtarea
.selectionEnd
=cPos
;
337 txtarea
.scrollTop
=scrollTop
;
341 var copy_alertText
=alertText
;
342 var re1
=new RegExp("\\$1","g");
343 var re2
=new RegExp("\\$2","g");
344 copy_alertText
=copy_alertText
.replace(re1
,sampleText
);
345 copy_alertText
=copy_alertText
.replace(re2
,tagOpen
+sampleText
+tagClose
);
348 text
=prompt(copy_alertText
);
352 if(!text
) { text
=sampleText
;}
353 text
=tagOpen
+text
+tagClose
;
354 document
.infoform
.infobox
.value
=text
;
355 // in Safari this causes scrolling
361 // reposition cursor if possible
362 if (txtarea
.createTextRange
) txtarea
.caretPos
= document
.selection
.createRange().duplicate();