missing semicolon
[lhc/web/wiklou.git] / stylesheets / wikibits.js
1 // Wikipedia JavaScript support functions
2
3 // if this is true, the toolbar will no longer overwrite the infobox when you move the mouse over individual items
4 var noOverwrite=false;
5 var alertText;
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
10 // Un-trap us from framesets
11 if( window.top != window ) window.top.location = window.location;
12
13 // for enhanced RecentChanges
14 function toggleVisibility( _levelId, _otherId, _linkId) {
15 var thisLevel = document.getElementById( _levelId );
16 var otherLevel = document.getElementById( _otherId );
17 var linkLevel = document.getElementById( _linkId );
18 if ( thisLevel.style.display == 'none' ) {
19 thisLevel.style.display = 'block';
20 otherLevel.style.display = 'none';
21 linkLevel.style.display = 'inline';
22 } else {
23 thisLevel.style.display = 'none';
24 otherLevel.style.display = 'inline';
25 linkLevel.style.display = 'none';
26 }
27 }
28
29 // Timezone stuff
30 // tz in format [+-]HHMM
31 function checkTimezone( tz, msg ) {
32 var localclock = new Date();
33 // returns negative offset from GMT in minutes
34 var tzRaw = localclock.getTimezoneOffset();
35 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
36 var tzMin = Math.abs(tzRaw) % 60;
37 var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
38 if( tz != tzString ) {
39 var junk = msg.split( '$1' );
40 document.write( junk[0] + "UTC" + tzString + junk[1] );
41 }
42 }
43
44 // in [-]HH:MM format...
45 // won't yet work with non-even tzs
46 function fetchTimezone() {
47 // FIXME: work around Safari bug
48 var localclock = new Date();
49 // returns negative offset from GMT in minutes
50 var tzRaw = localclock.getTimezoneOffset();
51 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
52 var tzMin = Math.abs(tzRaw) % 60;
53 var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
54 ":" + ((tzMin < 10) ? "0" : "") + tzMin;
55 return tzString;
56 }
57
58 function guessTimezone(box) {
59 document.preferences.wpHourDiff.value = fetchTimezone();
60 }
61
62 function showTocToggle(show,hide) {
63 if(document.getElementById) {
64 document.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleToc()" class="internal">' +
65 '<span id="showlink" style="display:none;">' + show + '</span>' +
66 '<span id="hidelink">' + hide + '</span>'
67 + '</a>]</span>');
68 }
69 }
70
71 function toggleToc() {
72 var toc = document.getElementById('tocinside');
73 var showlink=document.getElementById('showlink');
74 var hidelink=document.getElementById('hidelink');
75 if(toc.style.display == 'none') {
76 toc.style.display = tocWas;
77 hidelink.style.display='';
78 showlink.style.display='none';
79
80 } else {
81 tocWas = toc.style.display;
82 toc.style.display = 'none';
83 hidelink.style.display='none';
84 showlink.style.display='';
85
86 }
87 }
88
89 // this function generates the actual toolbar buttons with localized text
90 // we use it to avoid creating the toolbar where javascript is not enabled
91 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
92
93 speedTip=escapeQuotes(speedTip);
94 tagOpen=escapeQuotes(tagOpen);
95 tagClose=escapeQuotes(tagClose);
96 sampleText=escapeQuotes(sampleText);
97 var mouseOver="";
98
99 // we can't change the selection, so we show example texts
100 // when moving the mouse instead, until the first button is clicked
101 if(!document.selection && !is_gecko) {
102 // filter backslashes so it can be shown in the infobox
103 var re=new RegExp("\\\\n","g");
104 tagOpen=tagOpen.replace(re,"");
105 tagClose=tagClose.replace(re,"");
106 mouseOver = "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen+sampleText+tagClose+"'};\"";
107 }
108
109 document.write("<a href=\"#\" onclick=\"javascript:insertTags");
110 document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");
111
112 document.write("<img width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" ALT=\""+speedTip+"\" TITLE=\""+speedTip+"\""+mouseOver+">");
113 document.write("</a>");
114 return;
115 }
116
117 function addInfobox(infoText,text_alert) {
118 alertText=text_alert;
119 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
120
121 var re=new RegExp("\\\\n","g");
122 alertText=alertText.replace(re,"\n");
123
124 // if no support for changing selection, add a small copy & paste field
125 // document.selection is an IE-only property. The full toolbar works in IE and
126 // Gecko-based browsers.
127 if(!document.selection && !is_gecko) {
128 infoText=escapeQuotesHTML(infoText);
129 document.write("<form name='infoform' id='infoform'>"+
130 "<input size=80 id='infobox' name='infobox' value=\""+
131 infoText+"\" READONLY></form>");
132 }
133
134 }
135
136 function escapeQuotes(text) {
137 var re=new RegExp("'","g");
138 text=text.replace(re,"\\'");
139 re=new RegExp('"',"g");
140 text=text.replace(re,'&quot;');
141 re=new RegExp("\\n","g");
142 text=text.replace(re,"\\n");
143 return text;
144 }
145
146 function escapeQuotesHTML(text) {
147 var re=new RegExp('"',"g");
148 text=text.replace(re,"&quot;");
149 return text;
150 }
151
152 // apply tagOpen/tagClose to selection in textarea,
153 // use sampleText instead of selection if there is none
154 // copied and adapted from phpBB
155 function insertTags(tagOpen, tagClose, sampleText) {
156
157 var txtarea = document.editform.wpTextbox1;
158 // IE
159 if(document.selection && !is_gecko) {
160 var theSelection = document.selection.createRange().text;
161 if(!theSelection) { theSelection=sampleText;}
162 txtarea.focus();
163 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
164 theSelection = theSelection.substring(0, theSelection.length - 1);
165 document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
166 } else {
167 document.selection.createRange().text = tagOpen + theSelection + tagClose;
168 }
169
170 // Mozilla
171 } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
172 var startPos = txtarea.selectionStart;
173 var endPos = txtarea.selectionEnd;
174 var scrollTop=txtarea.scrollTop;
175 var myText = (txtarea.value).substring(startPos, endPos);
176 if(!myText) { myText=sampleText;}
177 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
178 subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
179 } else {
180 subst = tagOpen + myText + tagClose;
181 }
182 txtarea.value = txtarea.value.substring(0, startPos) + subst +
183 txtarea.value.substring(endPos, txtarea.value.length);
184 txtarea.focus();
185
186 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
187 txtarea.selectionStart=cPos;
188 txtarea.selectionEnd=cPos;
189 txtarea.scrollTop=scrollTop;
190
191 // All others
192 } else {
193 var copy_alertText=alertText;
194 var re1=new RegExp("\\$1","g");
195 var re2=new RegExp("\\$2","g");
196 copy_alertText=copy_alertText.replace(re1,sampleText);
197 copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
198 var text;
199 if (sampleText) {
200 text=prompt(copy_alertText);
201 } else {
202 text="";
203 }
204 if(!text) { text=sampleText;}
205 text=tagOpen+text+tagClose;
206 document.infoform.infobox.value=text;
207 txtarea.focus();
208 noOverwrite=true;
209 }
210 // reposition cursor if possible
211 if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
212 }