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