]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - themes/MonoBook/wikibits.js
Remove unused var is_opera_seven
[SourceForge/phpwiki.git] / themes / MonoBook / 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) && (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) {
12     var is_opera = true;
13 }
14
15 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
16 function onloadhook () {
17     // don't run anything below this for non-dom browsers
18     if(!(document.getElementById && document.getElementsByTagName)) return;
19     histrowinit();
20     unhidetzbutton();
21     tabbedprefs();
22 }
23 if (window.addEventListener) window.addEventListener("load",onloadhook,false);
24 else if (window.attachEvent) window.attachEvent("onload",onloadhook);
25
26
27 // document.write special stylesheet links
28 function addcss ( stylepath ) {
29     return;
30 }
31
32 // Un-trap us from framesets
33 if( window.top != window ) window.top.location = window.location;
34
35 // for enhanced RecentChanges
36 function toggleVisibility( _levelId, _otherId, _linkId) {
37         var thisLevel = document.getElementById( _levelId );
38         var otherLevel = document.getElementById( _otherId );
39         var linkLevel = document.getElementById( _linkId );
40         if ( thisLevel.style.display == 'none' ) {
41                 thisLevel.style.display = 'block';
42                 otherLevel.style.display = 'none';
43                 linkLevel.style.display = 'inline';
44         } else {
45                 thisLevel.style.display = 'none';
46                 otherLevel.style.display = 'inline';
47                 linkLevel.style.display = 'none';
48                 }
49 }
50
51 // page history stuff
52 // attach event handlers to the input elements on history page
53 function histrowinit () {
54     hf = document.getElementById('pagehistory');
55     if(!hf) return;
56     lis = hf.getElementsByTagName('LI');
57     for (i=0;i<lis.length;i++) {
58         inputs=lis[i].getElementsByTagName('INPUT');
59         if(inputs[0] && inputs[1]) {
60                 inputs[0].onclick = diffcheck;
61                 inputs[1].onclick = diffcheck;
62         }
63     }
64     diffcheck();
65 }
66 // check selection and tweak visibility/class onclick
67 function diffcheck() { 
68     var dli = false; // the li where the diff radio is checked
69     var oli = false; // the li where the oldid radio is checked
70     hf = document.getElementById('pagehistory');
71     if(!hf) return;
72     lis = hf.getElementsByTagName('LI');
73     for (i=0;i<lis.length;i++) {
74         inputs=lis[i].getElementsByTagName('INPUT');
75         if(inputs[1] && inputs[0]) {
76             if(inputs[1].checked || inputs[0].checked) { // this row has a checked radio button
77                 if(inputs[1].checked && inputs[0].checked && inputs[0].value == inputs[1].value) return false;
78                 if(oli) { // it's the second checked radio
79                     if(inputs[1].checked) {
80                     oli.className = "selected";
81                     return false 
82                     }
83                 } else if (inputs[0].checked) {
84                     return false;
85                 }
86                 if(inputs[0].checked) dli = lis[i];
87                 if(!oli) inputs[0].style.visibility = 'hidden';
88                 if(dli) inputs[1].style.visibility = 'hidden';
89                 lis[i].className = "selected";
90                 oli = lis[i];
91             }  else { // no radio is checked in this row
92                 if(!oli) inputs[0].style.visibility = 'hidden';
93                 else inputs[0].style.visibility = 'visible';
94                 if(dli) inputs[1].style.visibility = 'hidden';
95                 else inputs[1].style.visibility = 'visible';
96                 lis[i].className = "";
97             }
98         }
99     }
100 }
101
102 // generate toc from prefs form, fold sections
103 // XXX: needs testing on IE/Mac and safari
104 // more comments to follow
105 function tabbedprefs() {
106     prefform = document.getElementById('preferences');
107     if(!prefform || !document.createElement) return;
108     prefform.className = prefform.className + 'jsprefs';
109     var sections = new Array();
110     children = prefform.childNodes;
111     var seci = 0;
112     for(i=0;i<children.length;i++) {
113         if(children[i].nodeName.indexOf('FIELDSET') != -1) {
114             children[i].id = 'prefsection-' + seci;
115             children[i].className = 'prefsection';
116             if(is_opera || is_khtml) children[i].className = 'prefsection operaprefsection';
117             legends = children[i].getElementsByTagName('LEGEND');
118             sections[seci] = new Object();
119             if(legends[0] && legends[0].firstChild.nodeValue)
120                 sections[seci].text = legends[0].firstChild.nodeValue;
121             else
122                 sections[seci].text = '# ' + seci;
123             sections[seci].secid = children[i].id;
124             seci++;
125             if(sections.length != 1) children[i].style.display = 'none';
126             else var selectedid = children[i].id;
127         }
128     }
129     var toc = document.createElement('UL');
130     toc.id = 'preftoc';
131     toc.selectedid = selectedid;
132     for(i=0;i<sections.length;i++) {
133         var li = document.createElement('LI');
134         if(i == 0) li.className = 'selected';
135         var a =  document.createElement('A');
136         a.href = '#' + sections[i].secid;
137         a.onclick = uncoversection;
138         a.innerHTML = sections[i].text;
139         a.secid = sections[i].secid;
140         li.appendChild(a);
141         toc.appendChild(li);
142     }
143     prefform.insertBefore(toc, children[0]);
144     document.getElementById('prefsubmit').id = 'prefcontrol';
145 }
146 function uncoversection() {
147     oldsecid = this.parentNode.parentNode.selectedid;
148     newsec = document.getElementById(this.secid);
149     if(oldsecid != this.secid) {
150         ul = document.getElementById('preftoc');
151         document.getElementById(oldsecid).style.display = 'none';
152         newsec.style.display = 'block';
153         ul.selectedid = this.secid;
154         lis = ul.getElementsByTagName('LI');
155         for(i=0;i< lis.length;i++) {
156             lis[i].className = '';
157         }
158         this.parentNode.className = 'selected';
159     }
160     return false;
161 }
162
163 // Timezone stuff
164 // tz in format [+-]HHMM
165 function checkTimezone( tz, msg ) {
166         var localclock = new Date();
167         // returns negative offset from GMT in minutes
168         var tzRaw = localclock.getTimezoneOffset();
169         var tzHour = Math.floor( Math.abs(tzRaw) / 60);
170         var tzMin = Math.abs(tzRaw) % 60;
171         var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
172         if( tz != tzString ) {
173                 var junk = msg.split( '$1' );
174                 document.write( junk[0] + "UTC" + tzString + junk[1] );
175         }
176 }
177 function unhidetzbutton() {
178     tzb = document.getElementById('guesstimezonebutton')
179     if(tzb) tzb.style.display = 'inline';
180 }
181
182 // in [-]HH:MM format...
183 // won't yet work with non-even tzs
184 function fetchTimezone() {
185         // FIXME: work around Safari bug
186         var localclock = new Date();
187         // returns negative offset from GMT in minutes
188         var tzRaw = localclock.getTimezoneOffset();
189         var tzHour = Math.floor( Math.abs(tzRaw) / 60);
190         var tzMin = Math.abs(tzRaw) % 60;
191         var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
192                 ":" + ((tzMin < 10) ? "0" : "") + tzMin;
193         return tzString;
194 }
195
196 function guessTimezone(box) {
197         document.preferences.wpHourDiff.value = fetchTimezone();
198 }
199
200 function showTocToggle(show,hide) {
201         if (document.getElementById) {
202                 document.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleToc()" class="internal">' +
203                 '<span id="showlink" style="display:none;">' + show + '</span>' +
204                 '<span id="hidelink">' + hide + '</span>'
205                 + '</a>]</span>');
206         }
207 }
208
209
210 function toggleToc() {
211         var toc = document.getElementById('tocinside');
212         var showlink=document.getElementById('showlink');
213         var hidelink=document.getElementById('hidelink');
214         if(toc.style.display == 'none') {
215                 toc.style.display = tocWas;
216                 hidelink.style.display='';
217                 showlink.style.display='none';
218
219         } else {
220                 tocWas = toc.style.display;
221                 toc.style.display = 'none';
222                 hidelink.style.display='none';
223                 showlink.style.display='';
224
225         }
226 }
227
228 function escapeQuotes(text) {
229         var re=new RegExp("'","g");
230         text=text.replace(re,"\\'");
231         re=new RegExp('"',"g");
232         text=text.replace(re,'&quot;');
233         re=new RegExp("\\n","g");
234         text=text.replace(re,"\\n");
235         return text;
236 }
237
238 // apply tagOpen/tagClose to selection in textarea,
239 // use sampleText instead of selection if there is none
240 // copied and adapted from phpBB
241 function insertTags(tagOpen, tagClose, sampleText) {
242
243         var txtarea = document.editform.wpTextbox1;
244         // IE
245         if(document.selection  && !is_gecko) {
246                 var theSelection = document.selection.createRange().text;
247                 if(!theSelection) { theSelection=sampleText;}
248                 txtarea.focus();
249                 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
250                         theSelection = theSelection.substring(0, theSelection.length - 1);
251                         document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
252                 } else {
253                         document.selection.createRange().text = tagOpen + theSelection + tagClose;
254                 }
255
256         // Mozilla
257         } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
258                 var startPos = txtarea.selectionStart;
259                 var endPos = txtarea.selectionEnd;
260                 var scrollTop=txtarea.scrollTop;
261                 var myText = (txtarea.value).substring(startPos, endPos);
262                 if(!myText) { myText=sampleText;}
263                 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
264                         subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
265                 } else {
266                         subst = tagOpen + myText + tagClose;
267                 }
268                 txtarea.value = txtarea.value.substring(0, startPos) + subst +
269                   txtarea.value.substring(endPos, txtarea.value.length);
270                 txtarea.focus();
271
272                 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
273                 txtarea.selectionStart=cPos;
274                 txtarea.selectionEnd=cPos;
275                 txtarea.scrollTop=scrollTop;
276
277         // All others
278         } else {
279                 var copy_alertText=alertText;
280                 var re1=new RegExp("\\$1","g");
281                 var re2=new RegExp("\\$2","g");
282                 copy_alertText=copy_alertText.replace(re1,sampleText);
283                 copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
284                 var text;
285                 if (sampleText) {
286                         text=prompt(copy_alertText);
287                 } else {
288                         text="";
289                 }
290                 if(!text) { text=sampleText;}
291                 text=tagOpen+text+tagClose;
292                 document.infoform.infobox.value=text;
293                 // in Safari this causes scrolling
294                 if(!is_safari) {
295                         txtarea.focus();
296                 }
297                 noOverwrite=true;
298         }
299         // reposition cursor if possible
300         if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
301 }