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));
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 // document.write special stylesheet links
18 function addcss ( stylepath
) {
19 if (is_opera_preseven
) {
20 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'Opera6Fixes.css">');
21 } else if (is_opera_seven
) {
22 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'Opera7Fixes.css">');
23 } else if (is_khtml
) {
24 document
.write('<link rel="stylesheet" type="text/css" href="'+stylepath
+'KHTMLFixes.css">');
29 // Un-trap us from framesets
30 if( window
.top
!= window
) window
.top
.location
= window
.location
;
32 // for enhanced RecentChanges
33 function toggleVisibility( _levelId
, _otherId
, _linkId
) {
34 var thisLevel
= document
.getElementById( _levelId
);
35 var otherLevel
= document
.getElementById( _otherId
);
36 var linkLevel
= document
.getElementById( _linkId
);
37 if ( thisLevel
.style
.display
== 'none' ) {
38 thisLevel
.style
.display
= 'block';
39 otherLevel
.style
.display
= 'none';
40 linkLevel
.style
.display
= 'inline';
42 thisLevel
.style
.display
= 'none';
43 otherLevel
.style
.display
= 'inline';
44 linkLevel
.style
.display
= 'none';
49 // tz in format [+-]HHMM
50 function checkTimezone( tz
, msg
) {
51 var localclock
= new Date();
52 // returns negative offset from GMT in minutes
53 var tzRaw
= localclock
.getTimezoneOffset();
54 var tzHour
= Math
.floor( Math
.abs(tzRaw
) / 60);
55 var tzMin
= Math
.abs(tzRaw
) % 60;
56 var tzString
= ((tzRaw
>= 0) ? "-" : "+") + ((tzHour
< 10) ? "0" : "") + tzHour
+ ((tzMin
< 10) ? "0" : "") + tzMin
;
57 if( tz
!= tzString
) {
58 var junk
= msg
.split( '$1' );
59 document
.write( junk
[0] + "UTC" + tzString
+ junk
[1] );
63 // in [-]HH:MM format...
64 // won't yet work with non-even tzs
65 function fetchTimezone() {
66 // FIXME: work around Safari bug
67 var localclock
= new Date();
68 // returns negative offset from GMT in minutes
69 var tzRaw
= localclock
.getTimezoneOffset();
70 var tzHour
= Math
.floor( Math
.abs(tzRaw
) / 60);
71 var tzMin
= Math
.abs(tzRaw
) % 60;
72 var tzString
= ((tzRaw
>= 0) ? "-" : "") + ((tzHour
< 10) ? "0" : "") + tzHour
+
73 ":" + ((tzMin
< 10) ? "0" : "") + tzMin
;
77 function guessTimezone(box
) {
78 document
.preferences
.wpHourDiff
.value
= fetchTimezone();
81 function showTocToggle(show
,hide
) {
82 if(document
.getElementById
) {
83 document
.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleToc()" class="internal">' +
84 '<span id="showlink" style="display:none;">' + show
+ '</span>' +
85 '<span id="hidelink">' + hide
+ '</span>'
90 function toggleToc() {
91 var toc
= document
.getElementById('tocinside');
92 var showlink
=document
.getElementById('showlink');
93 var hidelink
=document
.getElementById('hidelink');
94 if(toc
.style
.display
== 'none') {
95 toc
.style
.display
= tocWas
;
96 hidelink
.style
.display
='';
97 showlink
.style
.display
='none';
100 tocWas
= toc
.style
.display
;
101 toc
.style
.display
= 'none';
102 hidelink
.style
.display
='none';
103 showlink
.style
.display
='';
108 // this function generates the actual toolbar buttons with localized text
109 // we use it to avoid creating the toolbar where javascript is not enabled
110 function addButton(imageFile
, speedTip
, tagOpen
, tagClose
, sampleText
) {
112 speedTip
=escapeQuotes(speedTip
);
113 tagOpen
=escapeQuotes(tagOpen
);
114 tagClose
=escapeQuotes(tagClose
);
115 sampleText
=escapeQuotes(sampleText
);
118 // we can't change the selection, so we show example texts
119 // when moving the mouse instead, until the first button is clicked
120 if(!document
.selection
&& !is_gecko
) {
121 // filter backslashes so it can be shown in the infobox
122 var re
=new RegExp("\\\\n","g");
123 tagOpen
=tagOpen
.replace(re
,"");
124 tagClose
=tagClose
.replace(re
,"");
125 mouseOver
= "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen
+sampleText
+tagClose
+"'};\"";
128 document
.write("<a href=\"javascript:insertTags");
129 document
.write("('"+tagOpen
+"','"+tagClose
+"','"+sampleText
+"');\">");
131 document
.write("<img width=\"23\" height=\"22\" src=\""+imageFile
+"\" border=\"0\" ALT=\""+speedTip
+"\" TITLE=\""+speedTip
+"\""+mouseOver
+">");
132 document
.write("</a>");
136 function addInfobox(infoText
,text_alert
) {
137 alertText
=text_alert
;
138 var clientPC
= navigator
.userAgent
.toLowerCase(); // Get client info
140 var re
=new RegExp("\\\\n","g");
141 alertText
=alertText
.replace(re
,"\n");
143 // if no support for changing selection, add a small copy & paste field
144 // document.selection is an IE-only property. The full toolbar works in IE and
145 // Gecko-based browsers.
146 if(!document
.selection
&& !is_gecko
) {
147 infoText
=escapeQuotesHTML(infoText
);
148 document
.write("<form name='infoform' id='infoform'>"+
149 "<input size=80 id='infobox' name='infobox' value=\""+
150 infoText
+"\" READONLY></form>");
155 function escapeQuotes(text
) {
156 var re
=new RegExp("'","g");
157 text
=text
.replace(re
,"\\'");
158 re
=new RegExp('"',"g");
159 text
=text
.replace(re
,'"');
160 re
=new RegExp("\\n","g");
161 text
=text
.replace(re
,"\\n");
165 function escapeQuotesHTML(text
) {
166 var re
=new RegExp('"',"g");
167 text
=text
.replace(re
,""");
171 // apply tagOpen/tagClose to selection in textarea,
172 // use sampleText instead of selection if there is none
173 // copied and adapted from phpBB
174 function insertTags(tagOpen
, tagClose
, sampleText
) {
176 var txtarea
= document
.editform
.wpTextbox1
;
178 if(document
.selection
&& !is_gecko
) {
179 var theSelection
= document
.selection
.createRange().text
;
180 if(!theSelection
) { theSelection
=sampleText
;}
182 if(theSelection
.charAt(theSelection
.length
- 1) == " "){// exclude ending space char, if any
183 theSelection
= theSelection
.substring(0, theSelection
.length
- 1);
184 document
.selection
.createRange().text
= tagOpen
+ theSelection
+ tagClose
+ " ";
186 document
.selection
.createRange().text
= tagOpen
+ theSelection
+ tagClose
;
190 } else if(txtarea
.selectionStart
|| txtarea
.selectionStart
== '0') {
191 var startPos
= txtarea
.selectionStart
;
192 var endPos
= txtarea
.selectionEnd
;
193 var scrollTop
=txtarea
.scrollTop
;
194 var myText
= (txtarea
.value
).substring(startPos
, endPos
);
195 if(!myText
) { myText
=sampleText
;}
196 if(myText
.charAt(myText
.length
- 1) == " "){ // exclude ending space char, if any
197 subst
= tagOpen
+ myText
.substring(0, (myText
.length
- 1)) + tagClose
+ " ";
199 subst
= tagOpen
+ myText
+ tagClose
;
201 txtarea
.value
= txtarea
.value
.substring(0, startPos
) + subst
+
202 txtarea
.value
.substring(endPos
, txtarea
.value
.length
);
205 var cPos
=startPos
+(tagOpen
.length
+myText
.length
+tagClose
.length
);
206 txtarea
.selectionStart
=cPos
;
207 txtarea
.selectionEnd
=cPos
;
208 txtarea
.scrollTop
=scrollTop
;
212 var copy_alertText
=alertText
;
213 var re1
=new RegExp("\\$1","g");
214 var re2
=new RegExp("\\$2","g");
215 copy_alertText
=copy_alertText
.replace(re1
,sampleText
);
216 copy_alertText
=copy_alertText
.replace(re2
,tagOpen
+sampleText
+tagClose
);
219 text
=prompt(copy_alertText
);
223 if(!text
) { text
=sampleText
;}
224 text
=tagOpen
+text
+tagClose
;
225 document
.infoform
.infobox
.value
=text
;
226 // in Safari this causes scrolling
232 // reposition cursor if possible
233 if (txtarea
.createTextRange
) txtarea
.caretPos
= document
.selection
.createRange().duplicate();