show infobox in all KHTML browsers
[lhc/web/wiklou.git] / stylesheets / wikibits.js
1 // Wikipedia JavaScript support functions
2
3 // for enhanced RecentChanges
4 function toggleVisibility( _levelId, _otherId, _linkId) {
5 var thisLevel = document.getElementById( _levelId );
6 var otherLevel = document.getElementById( _otherId );
7 var linkLevel = document.getElementById( _linkId );
8 if ( thisLevel.style.display == 'none' ) {
9 thisLevel.style.display = 'block';
10 otherLevel.style.display = 'none';
11 linkLevel.style.display = 'inline';
12 } else {
13 thisLevel.style.display = 'none';
14 otherLevel.style.display = 'inline';
15 linkLevel.style.display = 'none';
16 }
17 }
18
19 // Timezone stuff
20 // tz in format [+-]HHMM
21 function checkTimezone( tz, msg ) {
22 var localclock = new Date();
23 // returns negative offset from GMT in minutes
24 var tzRaw = localclock.getTimezoneOffset();
25 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
26 var tzMin = Math.abs(tzRaw) % 60;
27 var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
28 if( tz != tzString ) {
29 var junk = msg.split( '$1' );
30 document.write( junk[0] + "UTC" + tzString + junk[1] );
31 }
32 }
33
34 // in [-][H]H format...
35 // won't yet work with non-even tzs
36 function fetchTimezone() {
37 // FIXME: work around Safari bug
38 var localclock = new Date();
39 // returns negative offset from GMT in minutes
40 var tzRaw = localclock.getTimezoneOffset();
41 var tzHour = Math.floor( Math.abs(tzRaw) / 60);
42 var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "" : "0") + tzHour;
43 return tzString;
44 }
45
46 function guessTimezone(box) {
47 document.preferences.wpHourDiff.value = fetchTimezone();
48 }
49
50 function showTocToggle(show,hide) {
51 if(document.getElementById) {
52 document.writeln('<small>[<a href="javascript:toggleToc()" class="internal">' +
53 '<span id="showlink" style="display:none;">' + show + '</span>' +
54 '<span id="hidelink">' + hide + '</span>'
55 + '</a>]</small>');
56 }
57 }
58
59 function toggleToc() {
60 var toc = document.getElementById('tocinside');
61 var showlink=document.getElementById('showlink');
62 var hidelink=document.getElementById('hidelink');
63 if(toc.style.display == 'none') {
64 toc.style.display = tocWas;
65 hidelink.style.display='';
66 showlink.style.display='none';
67
68 } else {
69 tocWas = toc.style.display;
70 toc.style.display = 'none';
71 hidelink.style.display='none';
72 showlink.style.display='';
73
74 }
75 }
76
77 // this function generates the actual toolbar buttons with localized text
78 // we use it to avoid creating the toolbar where javascript is not enabled
79 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
80
81
82 speedTip=escapeQuotes(speedTip);
83 tagOpen=escapeQuotes(tagOpen);
84 tagClose=escapeQuotes(tagClose);
85 sampleText=escapeQuotes(sampleText);
86 document.write("<a href=\"#\" onclick=\"javascript:insertTags");
87 document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");
88 document.write("<img width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" ALT=\""+speedTip+"\" TITLE=\""+speedTip+"\">");
89 document.write("</a>");
90 return;
91 }
92
93 function addInfobox(infoText) {
94
95 // if no support for changing selection, add a small copy & paste field
96 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
97 var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
98 && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
99 && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1)
100 && (clientPC.indexOf('khtml')==-1));
101 if(!document.selection && !is_nav) {
102 document.write("<form name='infoform' id='infoform'>"+
103 "<input size=80 id='infobox' name='infobox' value='"+
104 infoText+"' READONLY></form>");
105 }
106
107 }
108
109 function escapeQuotes(text) {
110
111 text=text.replace(/'/g,"\\'");
112 text=text.replace(/\n/g,"\\n");
113 return text;
114 }
115
116 // apply tagOpen/tagClose to selection in textarea,
117 // use sampleText instead of selection if there is none
118 // copied and adapted from phpBB
119 function insertTags(tagOpen, tagClose, sampleText) {
120
121 var txtarea = document.editform.wpTextbox1;
122 // IE
123 if(document.selection) {
124 var theSelection = document.selection.createRange().text;
125 if(!theSelection) { theSelection=sampleText;}
126 txtarea.focus();
127 document.selection.createRange().text = tagOpen + theSelection + tagClose;
128 // Mozilla
129 } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
130 var startPos = txtarea.selectionStart;
131 var endPos = txtarea.selectionEnd;
132 var myText = (txtarea.value).substring(startPos, endPos);
133 if(!myText) { myText=sampleText;}
134 txtarea.value = txtarea.value.substring(0, startPos) + tagOpen + myText + tagClose + txtarea.value.substring(endPos, txtarea.value.length);
135 txtarea.focus();
136 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
137 txtarea.selectionStart=cPos;
138 txtarea.selectionEnd=cPos;
139 // All others
140 } else {
141 // Append at the end: Some people find that annoying
142 //txtarea.value += tagOpen + sampleText + tagClose;
143 //txtarea.focus();
144 tagOpen=tagOpen.replace(/\n/g,"");
145 tagClose=tagClose.replace(/\n/g,"");
146 document.infoform.infobox.value=tagOpen+sampleText+tagClose;
147 txtarea.focus();
148 }
149 // reposition cursor if possible
150 if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
151 }