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));
11 // Un-trap us from framesets
12 if( window
.top
!= window
) window
.top
.location
= window
.location
;
14 // for enhanced RecentChanges
15 function toggleVisibility( _levelId
, _otherId
, _linkId
) {
16 var thisLevel
= document
.getElementById( _levelId
);
17 var otherLevel
= document
.getElementById( _otherId
);
18 var linkLevel
= document
.getElementById( _linkId
);
19 if ( thisLevel
.style
.display
== 'none' ) {
20 thisLevel
.style
.display
= 'block';
21 otherLevel
.style
.display
= 'none';
22 linkLevel
.style
.display
= 'inline';
24 thisLevel
.style
.display
= 'none';
25 otherLevel
.style
.display
= 'inline';
26 linkLevel
.style
.display
= 'none';
31 // tz in format [+-]HHMM
32 function checkTimezone( tz
, msg
) {
33 var localclock
= new Date();
34 // returns negative offset from GMT in minutes
35 var tzRaw
= localclock
.getTimezoneOffset();
36 var tzHour
= Math
.floor( Math
.abs(tzRaw
) / 60);
37 var tzMin
= Math
.abs(tzRaw
) % 60;
38 var tzString
= ((tzRaw
>= 0) ? "-" : "+") + ((tzHour
< 10) ? "0" : "") + tzHour
+ ((tzMin
< 10) ? "0" : "") + tzMin
;
39 if( tz
!= tzString
) {
40 var junk
= msg
.split( '$1' );
41 document
.write( junk
[0] + "UTC" + tzString
+ junk
[1] );
45 // in [-]HH:MM format...
46 // won't yet work with non-even tzs
47 function fetchTimezone() {
48 // FIXME: work around Safari bug
49 var localclock
= new Date();
50 // returns negative offset from GMT in minutes
51 var tzRaw
= localclock
.getTimezoneOffset();
52 var tzHour
= Math
.floor( Math
.abs(tzRaw
) / 60);
53 var tzMin
= Math
.abs(tzRaw
) % 60;
54 var tzString
= ((tzRaw
>= 0) ? "-" : "") + ((tzHour
< 10) ? "0" : "") + tzHour
+
55 ":" + ((tzMin
< 10) ? "0" : "") + tzMin
;
59 function guessTimezone(box
) {
60 document
.preferences
.wpHourDiff
.value
= fetchTimezone();
63 function showTocToggle(show
,hide
) {
64 if(document
.getElementById
) {
65 document
.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleToc()" class="internal">' +
66 '<span id="showlink" style="display:none;">' + show
+ '</span>' +
67 '<span id="hidelink">' + hide
+ '</span>'
72 function toggleToc() {
73 var toc
= document
.getElementById('tocinside');
74 var showlink
=document
.getElementById('showlink');
75 var hidelink
=document
.getElementById('hidelink');
76 if(toc
.style
.display
== 'none') {
77 toc
.style
.display
= tocWas
;
78 hidelink
.style
.display
='';
79 showlink
.style
.display
='none';
82 tocWas
= toc
.style
.display
;
83 toc
.style
.display
= 'none';
84 hidelink
.style
.display
='none';
85 showlink
.style
.display
='';
90 // this function generates the actual toolbar buttons with localized text
91 // we use it to avoid creating the toolbar where javascript is not enabled
92 function addButton(imageFile
, speedTip
, tagOpen
, tagClose
, sampleText
) {
94 speedTip
=escapeQuotes(speedTip
);
95 tagOpen
=escapeQuotes(tagOpen
);
96 tagClose
=escapeQuotes(tagClose
);
97 sampleText
=escapeQuotes(sampleText
);
100 // we can't change the selection, so we show example texts
101 // when moving the mouse instead, until the first button is clicked
102 if(!document
.selection
&& !is_gecko
) {
103 // filter backslashes so it can be shown in the infobox
104 var re
=new RegExp("\\\\n","g");
105 tagOpen
=tagOpen
.replace(re
,"");
106 tagClose
=tagClose
.replace(re
,"");
107 mouseOver
= "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen
+sampleText
+tagClose
+"'};\"";
110 document
.write("<a href=\"javascript:insertTags");
111 document
.write("('"+tagOpen
+"','"+tagClose
+"','"+sampleText
+"');\">");
113 document
.write("<img width=\"23\" height=\"22\" src=\""+imageFile
+"\" border=\"0\" ALT=\""+speedTip
+"\" TITLE=\""+speedTip
+"\""+mouseOver
+">");
114 document
.write("</a>");
118 function addInfobox(infoText
,text_alert
) {
119 alertText
=text_alert
;
120 var clientPC
= navigator
.userAgent
.toLowerCase(); // Get client info
122 var re
=new RegExp("\\\\n","g");
123 alertText
=alertText
.replace(re
,"\n");
125 // if no support for changing selection, add a small copy & paste field
126 // document.selection is an IE-only property. The full toolbar works in IE and
127 // Gecko-based browsers.
128 if(!document
.selection
&& !is_gecko
) {
129 infoText
=escapeQuotesHTML(infoText
);
130 document
.write("<form name='infoform' id='infoform'>"+
131 "<input size=80 id='infobox' name='infobox' value=\""+
132 infoText
+"\" READONLY></form>");
137 function escapeQuotes(text
) {
138 var re
=new RegExp("'","g");
139 text
=text
.replace(re
,"\\'");
140 re
=new RegExp('"',"g");
141 text
=text
.replace(re
,'"');
142 re
=new RegExp("\\n","g");
143 text
=text
.replace(re
,"\\n");
147 function escapeQuotesHTML(text
) {
148 var re
=new RegExp('"',"g");
149 text
=text
.replace(re
,""");
153 // apply tagOpen/tagClose to selection in textarea,
154 // use sampleText instead of selection if there is none
155 // copied and adapted from phpBB
156 function insertTags(tagOpen
, tagClose
, sampleText
) {
158 var txtarea
= document
.editform
.wpTextbox1
;
160 if(document
.selection
&& !is_gecko
) {
161 var theSelection
= document
.selection
.createRange().text
;
162 if(!theSelection
) { theSelection
=sampleText
;}
164 if(theSelection
.charAt(theSelection
.length
- 1) == " "){// exclude ending space char, if any
165 theSelection
= theSelection
.substring(0, theSelection
.length
- 1);
166 document
.selection
.createRange().text
= tagOpen
+ theSelection
+ tagClose
+ " ";
168 document
.selection
.createRange().text
= tagOpen
+ theSelection
+ tagClose
;
172 } else if(txtarea
.selectionStart
|| txtarea
.selectionStart
== '0') {
173 var startPos
= txtarea
.selectionStart
;
174 var endPos
= txtarea
.selectionEnd
;
175 var scrollTop
=txtarea
.scrollTop
;
176 var myText
= (txtarea
.value
).substring(startPos
, endPos
);
177 if(!myText
) { myText
=sampleText
;}
178 if(myText
.charAt(myText
.length
- 1) == " "){ // exclude ending space char, if any
179 subst
= tagOpen
+ myText
.substring(0, (myText
.length
- 1)) + tagClose
+ " ";
181 subst
= tagOpen
+ myText
+ tagClose
;
183 txtarea
.value
= txtarea
.value
.substring(0, startPos
) + subst
+
184 txtarea
.value
.substring(endPos
, txtarea
.value
.length
);
187 var cPos
=startPos
+(tagOpen
.length
+myText
.length
+tagClose
.length
);
188 txtarea
.selectionStart
=cPos
;
189 txtarea
.selectionEnd
=cPos
;
190 txtarea
.scrollTop
=scrollTop
;
194 var copy_alertText
=alertText
;
195 var re1
=new RegExp("\\$1","g");
196 var re2
=new RegExp("\\$2","g");
197 copy_alertText
=copy_alertText
.replace(re1
,sampleText
);
198 copy_alertText
=copy_alertText
.replace(re2
,tagOpen
+sampleText
+tagClose
);
201 text
=prompt(copy_alertText
);
205 if(!text
) { text
=sampleText
;}
206 text
=tagOpen
+text
+tagClose
;
207 document
.infoform
.infobox
.value
=text
;
208 // in Safari this causes scrolling
214 // reposition cursor if possible
215 if (txtarea
.createTextRange
) txtarea
.caretPos
= document
.selection
.createRange().duplicate();