forgot the star html
[mediawiki.git] / stylesheets / wikibits.js
blobcd489e531e30e74ef0ca0c715c070216d828c920
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
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));
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';
23         } else {
24                 thisLevel.style.display = 'none';
25                 otherLevel.style.display = 'inline';
26                 linkLevel.style.display = 'none';
27                 }
30 // Timezone stuff
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] );
42         }
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;
56         return tzString;
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>'
68                 + '</a>]</span>');
69         }
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';
81         } else {
82                 tocWas = toc.style.display;
83                 toc.style.display = 'none';
84                 hidelink.style.display='none';
85                 showlink.style.display='';
87         }
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);
98         var mouseOver="";
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+"'};\"";
108         }
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>");
115         return;
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>");
133         }
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,'&quot;');
142         re=new RegExp("\\n","g");
143         text=text.replace(re,"\\n");
144         return text;
147 function escapeQuotesHTML(text) {
148         var re=new RegExp('"',"g");
149         text=text.replace(re,"&quot;");
150         return text;
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;
159         // IE
160         if(document.selection  && !is_gecko) {
161                 var theSelection = document.selection.createRange().text;
162                 if(!theSelection) { theSelection=sampleText;}
163                 txtarea.focus();
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 + " ";
167                 } else {
168                         document.selection.createRange().text = tagOpen + theSelection + tagClose;
169                 }
171         // Mozilla
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 + " ";
180                 } else {
181                         subst = tagOpen + myText + tagClose;
182                 }
183                 txtarea.value = txtarea.value.substring(0, startPos) + subst +
184                   txtarea.value.substring(endPos, txtarea.value.length);
185                 txtarea.focus();
187                 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
188                 txtarea.selectionStart=cPos;
189                 txtarea.selectionEnd=cPos;
190                 txtarea.scrollTop=scrollTop;
192         // All others
193         } else {
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);
199                 var text;
200                 if (sampleText) {
201                         text=prompt(copy_alertText);
202                 } else {
203                         text="";
204                 }
205                 if(!text) { text=sampleText;}
206                 text=tagOpen+text+tagClose;
207                 document.infoform.infobox.value=text;
208                 // in Safari this causes scrolling
209                 if(!is_safari) {
210                         txtarea.focus();
211                 }
212                 noOverwrite=true;
213         }
214         // reposition cursor if possible
215         if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();