Arbitrary diff selection without javascript
[mediawiki.git] / stylesheets / wikibits.js
blob1d3a6827528e420c1bfa3d2c0862c68a9b4a4017
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));
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     var is_opera_preseven = (window.opera && !document.childNodes);
14     var is_opera_seven = (window.opera && document.childNodes);
17 // add any onload functions in this hook (please don't hard-code any events in the xhtml source)
18 function onloadhook () {
19     // don't run anything below this for non-dom browsers
20     if(!(document.getElementById && document.getElementsByTagName)) return;
21     histrowinit();
23 if (window.addEventListener) window.addEventListener("load",onloadhook,false);
24 else if (window.attachEvent) window.attachEvent("onload",onloadhook);
27 // document.write special stylesheet links
28 function addcss ( stylepath ) {
29     if (is_opera_preseven) {
30         document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'Opera6Fixes.css">');
31     } else if (is_opera_seven) {
32         document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'Opera7Fixes.css">');
33     } else if (is_khtml) {
34         document.write('<link rel="stylesheet" type="text/css" href="'+stylepath+'KHTMLFixes.css">');
35     }
36     return;
39 // Un-trap us from framesets
40 if( window.top != window ) window.top.location = window.location;
42 // for enhanced RecentChanges
43 function toggleVisibility( _levelId, _otherId, _linkId) {
44         var thisLevel = document.getElementById( _levelId );
45         var otherLevel = document.getElementById( _otherId );
46         var linkLevel = document.getElementById( _linkId );
47         if ( thisLevel.style.display == 'none' ) {
48                 thisLevel.style.display = 'block';
49                 otherLevel.style.display = 'none';
50                 linkLevel.style.display = 'inline';
51         } else {
52                 thisLevel.style.display = 'none';
53                 otherLevel.style.display = 'inline';
54                 linkLevel.style.display = 'none';
55                 }
58 // page history stuff
59 // attach event handlers to the input elements on history page
60 function histrowinit () {
61     hf = document.getElementById('pagehistory');
62     if(!hf) return;
63     lis = hf.getElementsByTagName('li');
64     for (i=0;i<lis.length;i++) {
65         inputs=lis[i].getElementsByTagName('INPUT');
66         if(inputs[0] && inputs[1]) {
67                 inputs[0].onclick = diffcheck;
68                 inputs[1].onclick = diffcheck;
69         }
70     }
71     diffcheck();
73 // check selection and tweak visibility/class onclick
74 function diffcheck() { 
75     var dli = false; // the li where the diff radio is checked
76     var oli = false; // the li where the oldid radio is checked
77     hf = document.getElementById('pagehistory');
78     if(!hf) return;
79     lis = hf.getElementsByTagName('li');
80     for (i=0;i<lis.length;i++) {
81         inputs=lis[i].getElementsByTagName('INPUT');
82         if(inputs[1] && inputs[0]) {
83             if(inputs[1].checked || inputs[0].checked) { // this row has a checked radio button
84                 if(inputs[1].checked && inputs[0].checked && inputs[0].value == inputs[1].value) return false;
85                 if(oli) { // it's the second checked radio
86                     if(inputs[1].checked) {
87                     oli.className = "selected";
88                     return false 
89                     }
90                 } else if (inputs[0].checked) {
91                     return false;
92                 }
93                 if(inputs[0].checked) dli = lis[i];
94                 if(!oli) inputs[0].style.visibility = 'hidden';
95                 if(dli) inputs[1].style.visibility = 'hidden';
96                 lis[i].className = "selected";
97                 oli = lis[i];
98             }  else { // no radio is checked in this row
99                 if(!oli) inputs[0].style.visibility = 'hidden';
100                 else inputs[0].style.visibility = 'visible';
101                 if(dli) inputs[1].style.visibility = 'hidden';
102                 else inputs[1].style.visibility = 'visible';
103                 lis[i].className = "";
104             }
105         }
106     }
109 // Timezone stuff
110 // tz in format [+-]HHMM
111 function checkTimezone( tz, msg ) {
112         var localclock = new Date();
113         // returns negative offset from GMT in minutes
114         var tzRaw = localclock.getTimezoneOffset();
115         var tzHour = Math.floor( Math.abs(tzRaw) / 60);
116         var tzMin = Math.abs(tzRaw) % 60;
117         var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
118         if( tz != tzString ) {
119                 var junk = msg.split( '$1' );
120                 document.write( junk[0] + "UTC" + tzString + junk[1] );
121         }
124 // in [-]HH:MM format...
125 // won't yet work with non-even tzs
126 function fetchTimezone() {
127         // FIXME: work around Safari bug
128         var localclock = new Date();
129         // returns negative offset from GMT in minutes
130         var tzRaw = localclock.getTimezoneOffset();
131         var tzHour = Math.floor( Math.abs(tzRaw) / 60);
132         var tzMin = Math.abs(tzRaw) % 60;
133         var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "0" : "") + tzHour +
134                 ":" + ((tzMin < 10) ? "0" : "") + tzMin;
135         return tzString;
138 function guessTimezone(box) {
139         document.preferences.wpHourDiff.value = fetchTimezone();
142 function showTocToggle(show,hide) {
143         if(document.getElementById) {
144                 document.writeln('<span class=\'toctoggle\'>[<a href="javascript:toggleToc()" class="internal">' +
145                 '<span id="showlink" style="display:none;">' + show + '</span>' +
146                 '<span id="hidelink">' + hide + '</span>'
147                 + '</a>]</span>');
148         }
152 function toggleToc() {
153         var toc = document.getElementById('tocinside');
154         var showlink=document.getElementById('showlink');
155         var hidelink=document.getElementById('hidelink');
156         if(toc.style.display == 'none') {
157                 toc.style.display = tocWas;
158                 hidelink.style.display='';
159                 showlink.style.display='none';
161         } else {
162                 tocWas = toc.style.display;
163                 toc.style.display = 'none';
164                 hidelink.style.display='none';
165                 showlink.style.display='';
167         }
170 // this function generates the actual toolbar buttons with localized text
171 // we use it to avoid creating the toolbar where javascript is not enabled
172 function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText) {
174         speedTip=escapeQuotes(speedTip);
175         tagOpen=escapeQuotes(tagOpen);
176         tagClose=escapeQuotes(tagClose);
177         sampleText=escapeQuotes(sampleText);
178         var mouseOver="";
180         // we can't change the selection, so we show example texts
181         // when moving the mouse instead, until the first button is clicked
182         if(!document.selection && !is_gecko) {
183                 // filter backslashes so it can be shown in the infobox
184                 var re=new RegExp("\\\\n","g");
185                 tagOpen=tagOpen.replace(re,"");
186                 tagClose=tagClose.replace(re,"");
187                 mouseOver = "onMouseover=\"if(!noOverwrite){document.infoform.infobox.value='"+tagOpen+sampleText+tagClose+"'};\"";
188         }
190         document.write("<a href=\"javascript:insertTags");
191         document.write("('"+tagOpen+"','"+tagClose+"','"+sampleText+"');\">");
193         document.write("<img width=\"23\" height=\"22\" src=\""+imageFile+"\" border=\"0\" ALT=\""+speedTip+"\" TITLE=\""+speedTip+"\""+mouseOver+">");
194         document.write("</a>");
195         return;
198 function addInfobox(infoText,text_alert) {
199         alertText=text_alert;
200         var clientPC = navigator.userAgent.toLowerCase(); // Get client info
202         var re=new RegExp("\\\\n","g");
203         alertText=alertText.replace(re,"\n");
205         // if no support for changing selection, add a small copy & paste field
206         // document.selection is an IE-only property. The full toolbar works in IE and
207         // Gecko-based browsers.
208         if(!document.selection && !is_gecko) {
209                 infoText=escapeQuotesHTML(infoText);
210                 document.write("<form name='infoform' id='infoform'>"+
211                         "<input size=80 id='infobox' name='infobox' value=\""+
212                         infoText+"\" READONLY></form>");
213         }
217 function escapeQuotes(text) {
218         var re=new RegExp("'","g");
219         text=text.replace(re,"\\'");
220         re=new RegExp('"',"g");
221         text=text.replace(re,'&quot;');
222         re=new RegExp("\\n","g");
223         text=text.replace(re,"\\n");
224         return text;
227 function escapeQuotesHTML(text) {
228         var re=new RegExp('"',"g");
229         text=text.replace(re,"&quot;");
230         return text;
233 // apply tagOpen/tagClose to selection in textarea,
234 // use sampleText instead of selection if there is none
235 // copied and adapted from phpBB
236 function insertTags(tagOpen, tagClose, sampleText) {
238         var txtarea = document.editform.wpTextbox1;
239         // IE
240         if(document.selection  && !is_gecko) {
241                 var theSelection = document.selection.createRange().text;
242                 if(!theSelection) { theSelection=sampleText;}
243                 txtarea.focus();
244                 if(theSelection.charAt(theSelection.length - 1) == " "){// exclude ending space char, if any
245                         theSelection = theSelection.substring(0, theSelection.length - 1);
246                         document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
247                 } else {
248                         document.selection.createRange().text = tagOpen + theSelection + tagClose;
249                 }
251         // Mozilla
252         } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
253                 var startPos = txtarea.selectionStart;
254                 var endPos = txtarea.selectionEnd;
255                 var scrollTop=txtarea.scrollTop;
256                 var myText = (txtarea.value).substring(startPos, endPos);
257                 if(!myText) { myText=sampleText;}
258                 if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
259                         subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
260                 } else {
261                         subst = tagOpen + myText + tagClose;
262                 }
263                 txtarea.value = txtarea.value.substring(0, startPos) + subst +
264                   txtarea.value.substring(endPos, txtarea.value.length);
265                 txtarea.focus();
267                 var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
268                 txtarea.selectionStart=cPos;
269                 txtarea.selectionEnd=cPos;
270                 txtarea.scrollTop=scrollTop;
272         // All others
273         } else {
274                 var copy_alertText=alertText;
275                 var re1=new RegExp("\\$1","g");
276                 var re2=new RegExp("\\$2","g");
277                 copy_alertText=copy_alertText.replace(re1,sampleText);
278                 copy_alertText=copy_alertText.replace(re2,tagOpen+sampleText+tagClose);
279                 var text;
280                 if (sampleText) {
281                         text=prompt(copy_alertText);
282                 } else {
283                         text="";
284                 }
285                 if(!text) { text=sampleText;}
286                 text=tagOpen+text+tagClose;
287                 document.infoform.infobox.value=text;
288                 // in Safari this causes scrolling
289                 if(!is_safari) {
290                         txtarea.focus();
291                 }
292                 noOverwrite=true;
293         }
294         // reposition cursor if possible
295         if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();