xref: /dokuwiki/lib/scripts/script.js (revision 8b06d178223afa83719d5719942e315c41adc596)
1/**
2 * Some of these scripts were taken from wikipedia.org and were modified for DokuWiki
3 */
4
5/**
6 * Some browser detection
7 */
8var clientPC  = navigator.userAgent.toLowerCase(); // Get client info
9var is_gecko  = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
10                && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));
11var is_safari = ((clientPC.indexOf('AppleWebKit')!=-1) && (clientPC.indexOf('spoofer')==-1));
12var is_khtml  = (navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ));
13if (clientPC.indexOf('opera')!=-1) {
14    var is_opera = true;
15    var is_opera_preseven = (window.opera && !document.childNodes);
16    var is_opera_seven = (window.opera && document.childNodes);
17}
18
19//http://simon.incutio.com/archive/2004/05/26/addLoadEvent#comment2
20function addEvent(oTarget, sType, fpDest) {
21  var oOldEvent = oTarget[sType];
22  if (typeof oOldEvent != "function") {
23    oTarget[sType] = fpDest;
24  } else {
25    oTarget[sType] = function(e) {
26      oOldEvent(e);
27      fpDest(e);
28    }
29  }
30}
31
32/**
33 * Get the X offset of the top left corner of the given object
34 *
35 * @link http://www.quirksmode.org/index.html?/js/findpos.html
36 */
37function findPosX(object){
38  var curleft = 0;
39  if(typeof(object) == 'object'){
40    var obj = object;
41  }else{
42    var obj = document.getElementById(object);
43  }
44  if (obj.offsetParent){
45    while (obj.offsetParent){
46      curleft += obj.offsetLeft;
47      obj = obj.offsetParent;
48    }
49  }
50  else if (obj.x){
51    curleft += obj.x;
52  }
53  return curleft;
54} //end findPosX function
55
56/**
57 * Get the Y offset of the top left corner of the given object
58 *
59 * @link http://www.quirksmode.org/index.html?/js/findpos.html
60 */
61function findPosY(object){
62  var curtop = 0;
63  if(typeof(object) == 'object'){
64    var obj = object;
65  }else{
66    var obj = document.getElementById(object);
67  }
68  if (obj.offsetParent){
69    while (obj.offsetParent){
70      curtop += obj.offsetTop;
71      obj = obj.offsetParent;
72    }
73  }
74  else if (obj.y){
75    curtop += obj.y;
76  }
77  return curtop;
78} //end findPosY function
79
80/**
81 * Escape special chars in JavaScript
82 *
83 * @author Andreas Gohr <andi@splitbrain.org>
84 */
85function jsEscape(text){
86    var re=new RegExp("\\\\","g");
87    text=text.replace(re,"\\\\");
88    var re=new RegExp("'","g");
89    text=text.replace(re,"\\'");
90    re=new RegExp('"',"g");
91    text=text.replace(re,'&quot;');
92    re=new RegExp("\\\\\\\\n","g");
93    text=text.replace(re,"\\n");
94    return text;
95}
96
97/**
98 * This function escapes some special chars
99 * @deprecated by above function
100 */
101function escapeQuotes(text) {
102  var re=new RegExp("'","g");
103  text=text.replace(re,"\\'");
104  re=new RegExp('"',"g");
105  text=text.replace(re,'&quot;');
106  re=new RegExp("\\n","g");
107  text=text.replace(re,"\\n");
108  return text;
109}
110
111/**
112 * Prints a animated gif to show the search is performed
113 *
114 * @author Andreas Gohr <andi@splitbrain.org>
115 */
116function showLoadBar(){
117  if(document.getElementById){
118    document.write('<img src="'+DOKU_BASE+'lib/images/loading.gif" '+
119                   'width="150" height="12" id="loading" />');
120  }
121}
122
123/**
124 * Disables the animated gif to show the search is done
125 *
126 * @author Andreas Gohr <andi@splitbrain.org>
127 */
128function hideLoadBar(){
129  if(document.getElementById){
130    document.getElementById('loading').style.display="none";
131  }
132}
133
134/*
135 * Insert the selected filename and close the window
136 *
137 * @see http://www.alexking.org/index.php?content=software/javascript/content.php
138 */
139function mediaSelect(file){
140	opener.insertAtCarret('wikitext','{{'+file+'}}');
141  window.close();
142}
143
144/**
145 * For the upload Dialog. Prefills the wikiname.
146 */
147function suggestWikiname(){
148  var file = document.upload.upload.value;
149
150  file = file.substr(file.lastIndexOf('/')+1);
151  file = file.substr(file.lastIndexOf('\\')+1);
152
153  document.upload.id.value = file;
154}
155
156/**
157 * This prints the switch to toggle the Table of Contents
158 */
159function showTocToggle(showtxt,hidetxt) {
160  if(document.getElementById) {
161		show = '<img src="'+DOKU_BASE+'lib/images/arrow_down.gif" alt="'+showtxt+'">';
162		hide = '<img src="'+DOKU_BASE+'lib/images/arrow_up.gif" alt="'+hidetxt+'">';
163
164    document.writeln('<div class=\'toctoggle\'><a href="javascript:toggleToc()" class="toc">' +
165    '<span id="showlink" style="display:none;">' + show + '</span>' +
166    '<span id="hidelink">' + hide + '</span>'
167    + '</a></div>');
168  }
169}
170
171/**
172 * This toggles the visibility of the Table of Contents
173 */
174function toggleToc() {
175  var toc = document.getElementById('tocinside');
176  var showlink=document.getElementById('showlink');
177  var hidelink=document.getElementById('hidelink');
178  if(toc.style.display == 'none') {
179    toc.style.display = tocWas;
180    hidelink.style.display='';
181    showlink.style.display='none';
182  } else {
183    tocWas = toc.style.display;
184    toc.style.display = 'none';
185    hidelink.style.display='none';
186    showlink.style.display='';
187
188  }
189}
190
191/*
192 * This sets a cookie by JavaScript
193 *
194 * @see http://www.webreference.com/js/column8/functions.html
195 */
196function setCookie(name, value, expires, path, domain, secure) {
197  var curCookie = name + "=" + escape(value) +
198      ((expires) ? "; expires=" + expires.toGMTString() : "") +
199      ((path) ? "; path=" + path : "") +
200      ((domain) ? "; domain=" + domain : "") +
201      ((secure) ? "; secure" : "");
202  document.cookie = curCookie;
203}
204
205/*
206 * This reads a cookie by JavaScript
207 *
208 * @see http://www.webreference.com/js/column8/functions.html
209 */
210function getCookie(name) {
211  var dc = document.cookie;
212  var prefix = name + "=";
213  var begin = dc.indexOf("; " + prefix);
214  if (begin == -1) {
215    begin = dc.indexOf(prefix);
216    if (begin != 0) return null;
217  } else
218    begin += 2;
219  var end = document.cookie.indexOf(";", begin);
220  if (end == -1)
221    end = dc.length;
222  return unescape(dc.substring(begin + prefix.length, end));
223}
224
225/*
226 * This is needed for the cookie functions
227 *
228 * @see http://www.webreference.com/js/column8/functions.html
229 */
230function fixDate(date) {
231  var base = new Date(0);
232  var skew = base.getTime();
233  if (skew > 0)
234    date.setTime(date.getTime() - skew);
235}
236
237/*
238 * This enables/disables checkboxes for acl-administration
239 *
240 * @author Frank Schubert <frank@schokilade.de>
241 */
242function checkAclLevel(){
243  if(document.getElementById) {
244    var scope = document.getElementById('acl_scope').value;
245
246    //check for namespace
247    if( (scope.indexOf(":*") > 0) || (scope == "*") ){
248      document.getElementsByName('acl_checkbox[4]')[0].disabled=false;
249      document.getElementsByName('acl_checkbox[8]')[0].disabled=false;
250    }else{
251      document.getElementsByName('acl_checkbox[4]')[0].checked=false;
252      document.getElementsByName('acl_checkbox[8]')[0].checked=false;
253
254      document.getElementsByName('acl_checkbox[4]')[0].disabled=true;
255      document.getElementsByName('acl_checkbox[8]')[0].disabled=true;
256    }
257  }
258}
259
260/* insitu footnote addition
261 * provide a wrapper for domTT javascript library
262 * this function is placed in the onmouseover event of footnote references in the main page
263 *
264 * @author Chris Smith <chris [at] jalakai [dot] co [dot] uk>
265 */
266var currentFootnote = 0;
267function fnt(id, e, evt) {
268
269    if (currentFootnote && id != currentFootnote) {
270        domTT_close(document.getElementById('insitu-fn'+currentFootnote));
271    }
272
273    // does the footnote tooltip already exist?
274    var fnt = document.getElementById('insitu-fn'+id);
275    if (!fnt) {
276        // if not create it...
277
278        // locate the footnote anchor element
279        var a = document.getElementById( "fn"+id );
280        if (!a) return;
281
282        // anchor parent is the footnote container, get its innerHTML
283        var footnote = new String (a.parentNode.innerHTML);
284
285        // strip the leading footnote anchors and their comma separators
286        footnote = footnote.replace(/<a\s.*?href=\".*\#fnt\d+\".*?<\/a>/gi, '');
287        footnote = footnote.replace(/^\s+(,\s+)+/,'');
288
289        // prefix ids on any elements with "insitu-" to ensure they remain unique
290        footnote = footnote.replace(/\bid=\"(.*?)\"/gi,'id="insitu-$1');
291   	} else {
292        var footnote = new String(fnt.innerHTML);
293    }
294
295    // activate the tooltip
296    domTT_activate(e, evt, 'content', footnote, 'type', 'velcro', 'id', 'insitu-fn'+id, 'styleClass', 'insitu-footnote', 'maxWidth', document.body.offsetWidth*0.4);
297    currentFootnote = id;
298}
299
300
301/**
302 * Add the edit window size controls
303 */
304function initSizeCtl(ctlid,edid){
305		if(!document.getElementById) return;
306
307    var ctl      = document.getElementById(ctlid);
308    var textarea = document.getElementById(edid);
309
310    var hgt = getCookie('DokuWikisizeCtl');
311    if(hgt == null){
312      textarea.style.height = '300px';
313    }else{
314      textarea.style.height = hgt;
315    }
316
317    var l = document.createElement('img');
318    var s = document.createElement('img');
319    l.src = DOKU_BASE+'lib/images/larger.gif';
320    s.src = DOKU_BASE+'lib/images/smaller.gif';
321    l.onclick = function(){sizeCtl(edid,100);}
322    s.onclick = function(){sizeCtl(edid,-100);}
323    ctl.appendChild(l);
324    ctl.appendChild(s);
325}
326
327/**
328 * This sets the vertical size of the editbox
329 */
330function sizeCtl(edid,val){
331  var textarea = document.getElementById(edid);
332  var height = parseInt(textarea.style.height.substr(0,textarea.style.height.length-2));
333  height += val;
334  textarea.style.height = height+'px';
335
336  var now = new Date();
337  fixDate(now);
338  now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000); //expire in a year
339  setCookie('DokuWikisizeCtl',textarea.style.height,now);
340}
341