xref: /dokuwiki/lib/scripts/script.js (revision 2c43c3df0337ec2d0730926168d6b5289d93a344)
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_macos  = navigator.appVersion.indexOf('Mac') != -1;
10var is_gecko  = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1) &&
11                (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));
12var is_safari = ((clientPC.indexOf('AppleWebKit')!=-1) && (clientPC.indexOf('spoofer')==-1));
13var is_khtml  = (navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ));
14if (clientPC.indexOf('opera')!=-1) {
15    var is_opera = true;
16    var is_opera_preseven = (window.opera && !document.childNodes);
17    var is_opera_seven = (window.opera && document.childNodes);
18}
19
20/**
21 * Rewrite the accesskey tooltips to be more browser and OS specific.
22 *
23 * Accesskey tooltips are still only a best-guess of what will work
24 * on well known systems.
25 *
26 * @author Ben Coburn <btcoburn@silicodon.net>
27 */
28function updateAccessKeyTooltip() {
29  // determin tooltip text (order matters)
30  var tip = 'ALT+'; //default
31  if (is_macos) { tip = 'CTRL+'; }
32  if (is_opera) { tip = 'SHIFT+ESC '; }
33  // add other cases here...
34
35  // do tooltip update
36  if (tip=='ALT+') { return; }
37  var exp = /\[ALT\+/i;
38  var rep = '['+tip;
39
40  var elements = document.getElementsByTagName('a');
41  for (var i=0; i<elements.length; i++) {
42    if (elements[i].accessKey.length==1 && elements[i].title.length>0) {
43      elements[i].title = elements[i].title.replace(exp, rep);
44    }
45  }
46
47  elements = document.getElementsByTagName('input');
48  for (var i=0; i<elements.length; i++) {
49    if (elements[i].accessKey.length==1 && elements[i].title.length>0) {
50      elements[i].title = elements[i].title.replace(exp, rep);
51    }
52  }
53
54  elements = document.getElementsByTagName('button');
55  for (var i=0; i<elements.length; i++) {
56    if (elements[i].accessKey.length==1 && elements[i].title.length>0) {
57      elements[i].title = elements[i].title.replace(exp, rep);
58    }
59  }
60}
61
62/**
63 * Handy shortcut to document.getElementById
64 *
65 * This function was taken from the prototype library
66 *
67 * @link http://prototype.conio.net/
68 */
69function $() {
70  var elements = new Array();
71
72  for (var i = 0; i < arguments.length; i++) {
73    var element = arguments[i];
74    if (typeof element == 'string')
75      element = document.getElementById(element);
76
77    if (arguments.length == 1)
78      return element;
79
80    elements.push(element);
81  }
82
83  return elements;
84}
85
86/**
87 * Simple function to check if a global var is defined
88 *
89 * @author Kae Verens
90 * @link http://verens.com/archives/2005/07/25/isset-for-javascript/#comment-2835
91 */
92function isset(varname){
93  return(typeof(window[varname])!='undefined');
94}
95
96/**
97 * Select elements by their class name
98 *
99 * @author Dustin Diaz <dustin [at] dustindiaz [dot] com>
100 * @link   http://www.dustindiaz.com/getelementsbyclass/
101 */
102function getElementsByClass(searchClass,node,tag) {
103    var classElements = new Array();
104    if ( node == null )
105        node = document;
106    if ( tag == null )
107        tag = '*';
108    var els = node.getElementsByTagName(tag);
109    var elsLen = els.length;
110    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
111    for (i = 0, j = 0; i < elsLen; i++) {
112        if ( pattern.test(els[i].className) ) {
113            classElements[j] = els[i];
114            j++;
115        }
116    }
117    return classElements;
118}
119
120/**
121 * Get the X offset of the top left corner of the given object
122 *
123 * @link http://www.quirksmode.org/index.html?/js/findpos.html
124 */
125function findPosX(object){
126  var curleft = 0;
127  var obj = $(object);
128  if (obj.offsetParent){
129    while (obj.offsetParent){
130      curleft += obj.offsetLeft;
131      obj = obj.offsetParent;
132    }
133  }
134  else if (obj.x){
135    curleft += obj.x;
136  }
137  return curleft;
138} //end findPosX function
139
140/**
141 * Get the Y offset of the top left corner of the given object
142 *
143 * @link http://www.quirksmode.org/index.html?/js/findpos.html
144 */
145function findPosY(object){
146  var curtop = 0;
147  var obj = $(object);
148  if (obj.offsetParent){
149    while (obj.offsetParent){
150      curtop += obj.offsetTop;
151      obj = obj.offsetParent;
152    }
153  }
154  else if (obj.y){
155    curtop += obj.y;
156  }
157  return curtop;
158} //end findPosY function
159
160/**
161 * Escape special chars in JavaScript
162 *
163 * @author Andreas Gohr <andi@splitbrain.org>
164 */
165function jsEscape(text){
166    var re=new RegExp("\\\\","g");
167    text=text.replace(re,"\\\\");
168    re=new RegExp("'","g");
169    text=text.replace(re,"\\'");
170    re=new RegExp('"',"g");
171    text=text.replace(re,'&quot;');
172    re=new RegExp("\\\\\\\\n","g");
173    text=text.replace(re,"\\n");
174    return text;
175}
176
177/**
178 * This function escapes some special chars
179 * @deprecated by above function
180 */
181function escapeQuotes(text) {
182  var re=new RegExp("'","g");
183  text=text.replace(re,"\\'");
184  re=new RegExp('"',"g");
185  text=text.replace(re,'&quot;');
186  re=new RegExp("\\n","g");
187  text=text.replace(re,"\\n");
188  return text;
189}
190
191/**
192 * Adds a node as the first childenode to the given parent
193 *
194 * @see appendChild()
195 */
196function prependChild(parent,element) {
197    if(!parent.firstChild){
198        parent.appendChild(element);
199    }else{
200        parent.insertBefore(element,parent.firstChild);
201    }
202}
203
204/**
205 * Prints a animated gif to show the search is performed
206 *
207 * Because we need to modify the DOM here before the document is loaded
208 * and parsed completely we have to rely on document.write()
209 *
210 * @author Andreas Gohr <andi@splitbrain.org>
211 */
212function showLoadBar(){
213
214  document.write('<img src="'+DOKU_BASE+'lib/images/loading.gif" '+
215                 'width="150" height="12" alt="..." />');
216
217  /* this does not work reliable in IE
218  obj = $(id);
219
220  if(obj){
221    obj.innerHTML = '<img src="'+DOKU_BASE+'lib/images/loading.gif" '+
222                    'width="150" height="12" alt="..." />';
223    obj.style.display="block";
224  }
225  */
226}
227
228/**
229 * Disables the animated gif to show the search is done
230 *
231 * @author Andreas Gohr <andi@splitbrain.org>
232 */
233function hideLoadBar(id){
234  obj = $(id);
235  if(obj) obj.style.display="none";
236}
237
238/**
239 * Adds the toggle switch to the TOC
240 */
241function addTocToggle() {
242    if(!document.getElementById) return;
243    var header = $('toc__header');
244    if(!header) return;
245
246    var obj       = document.createElement('span');
247    obj.id        = 'toc__toggle';
248    obj.innerHTML = '<span>-</span>';
249    obj.className = 'toc_close';
250    obj.onclick   = toggleToc;
251
252    prependChild(header,obj);
253}
254
255/**
256 * This toggles the visibility of the Table of Contents
257 */
258function toggleToc() {
259  var toc = $('toc__inside');
260  var obj = $('toc__toggle');
261  if(toc.style.display == 'none') {
262    toc.style.display = '';
263    obj.innerHTML     = '-';
264    obj.className     = 'toc_close';
265  } else {
266    toc.style.display = 'none';
267    obj.innerHTML     = '<span>+</span>';
268    obj.className     = 'toc_open';
269  }
270}
271
272/**
273 * This enables/disables checkboxes for acl-administration
274 *
275 * @author Frank Schubert <frank@schokilade.de>
276 */
277function checkAclLevel(){
278  if(document.getElementById) {
279    var scope = $('acl_scope').value;
280
281    //check for namespace
282    if( (scope.indexOf(":*") > 0) || (scope == "*") ){
283      document.getElementsByName('acl_checkbox[4]')[0].disabled=false;
284      document.getElementsByName('acl_checkbox[8]')[0].disabled=false;
285    }else{
286      document.getElementsByName('acl_checkbox[4]')[0].checked=false;
287      document.getElementsByName('acl_checkbox[8]')[0].checked=false;
288
289      document.getElementsByName('acl_checkbox[4]')[0].disabled=true;
290      document.getElementsByName('acl_checkbox[8]')[0].disabled=true;
291    }
292  }
293}
294
295/**
296 * Display an insitu footnote popup
297 *
298 * @author Andreas Gohr <andi@splitbrain.org>
299 * @author Chris Smith <chris@jalakai.co.uk>
300 */
301function footnote(e){
302    var obj = e.target;
303    var id = obj.id.substr(5);
304
305    // get or create the footnote popup div
306    var fndiv = $('insitu__fn');
307    if(!fndiv){
308        fndiv = document.createElement('div');
309        fndiv.id        = 'insitu__fn';
310        fndiv.className = 'insitu-footnote JSpopup dokuwiki';
311
312        // autoclose on mouseout - ignoring bubbled up events
313        addEvent(fndiv,'mouseout',function(e){
314            if(e.target != fndiv){
315                e.stopPropagation();
316                return;
317            }
318            // check if the element was really left
319            if(e.pageX){        // Mozilla
320                var bx1 = findPosX(fndiv);
321                var bx2 = bx1 + fndiv.offsetWidth;
322                var by1 = findPosY(fndiv);
323                var by2 = by1 + fndiv.offsetHeight;
324                var x = e.pageX;
325                var y = e.pageY;
326                if(x > bx1 && x < bx2 && y > by1 && y < by2){
327                    // we're still inside boundaries
328                    e.stopPropagation();
329                    return;
330                }
331            }else{              // IE
332                if(e.offsetX > 0 && e.offsetX < fndiv.offsetWidth-1 &&
333                   e.offsetY > 0 && e.offsetY < fndiv.offsetHeight-1){
334                    // we're still inside boundaries
335                    e.stopPropagation();
336                    return;
337                }
338            }
339            // okay, hide it
340            fndiv.style.display='none';
341        });
342        document.body.appendChild(fndiv);
343    }
344
345    // locate the footnote anchor element
346    var a = $( "fn__"+id );
347    if (!a){ return; }
348
349    // anchor parent is the footnote container, get its innerHTML
350    var content = new String (a.parentNode.innerHTML);
351
352    // strip the leading content anchors and their comma separators
353    content = content.replace(/<a\s.*?href=\".*\#fnt__\d+\".*?<\/a>/gi, '');
354    content = content.replace(/^\s+(,\s+)+/,'');
355
356    // prefix ids on any elements with "insitu__" to ensure they remain unique
357    content = content.replace(/\bid=\"(.*?)\"/gi,'id="insitu__$1');
358
359    // now put the content into the wrapper
360    fndiv.innerHTML = content;
361
362    // position the div and make it visible
363    var x; var y;
364    if(e.pageX){        // Mozilla
365        x = e.pageX;
366        y = e.pageY;
367    }else{              // IE
368        x = e.offsetX;
369        y = e.offsetY;
370    }
371    fndiv.style.position = 'absolute';
372    fndiv.style.left = (x+2)+'px';
373    fndiv.style.top  = (y+2)+'px';
374    fndiv.style.display = '';
375}
376
377/**
378 * Add the event handlers to footnotes
379 *
380 * @author Andreas Gohr <andi@splitbrain.org>
381 */
382addInitEvent(function(){
383    var elems = getElementsByClass('fn_top',null,'a');
384    for(var i=0; i<elems.length; i++){
385        addEvent(elems[i],'mouseover',function(e){footnote(e);});
386    }
387});
388
389/**
390 * Add the edit window size controls
391 */
392function initSizeCtl(ctlid,edid){
393    if(!document.getElementById){ return; }
394
395    var ctl      = $(ctlid);
396    var textarea = $(edid);
397    if(!ctl || !textarea) return;
398
399    var hgt = DokuCookie.getValue('sizeCtl');
400    if(hgt){
401      textarea.style.height = hgt;
402    }else{
403      textarea.style.height = '300px';
404    }
405
406    var l = document.createElement('img');
407    var s = document.createElement('img');
408    var w = document.createElement('img');
409    l.src = DOKU_BASE+'lib/images/larger.gif';
410    s.src = DOKU_BASE+'lib/images/smaller.gif';
411    w.src = DOKU_BASE+'lib/images/wrap.gif';
412    addEvent(l,'click',function(){sizeCtl(edid,100);});
413    addEvent(s,'click',function(){sizeCtl(edid,-100);});
414    addEvent(w,'click',function(){toggleWrap(edid);});
415    ctl.appendChild(l);
416    ctl.appendChild(s);
417    ctl.appendChild(w);
418}
419
420/**
421 * This sets the vertical size of the editbox
422 */
423function sizeCtl(edid,val){
424  var textarea = $(edid);
425  var height = parseInt(textarea.style.height.substr(0,textarea.style.height.length-2));
426  height += val;
427  textarea.style.height = height+'px';
428
429  DokuCookie.setValue('sizeCtl',textarea.style.height);
430}
431
432/**
433 * Toggle the wrapping mode of a textarea
434 *
435 * @author Fluffy Convict <fluffyconvict@hotmail.com>
436 * @link   http://news.hping.org/comp.lang.javascript.archive/12265.html
437 * @author <shutdown@flashmail.com>
438 * @link   https://bugzilla.mozilla.org/show_bug.cgi?id=302710#c2
439 */
440function toggleWrap(edid){
441    var txtarea = $(edid);
442    var wrap = txtarea.getAttribute('wrap');
443    if(wrap && wrap.toLowerCase() == 'off'){
444        txtarea.setAttribute('wrap', 'soft');
445    }else{
446        txtarea.setAttribute('wrap', 'off');
447    }
448    // Fix display for mozilla
449    var parNod = txtarea.parentNode;
450    var nxtSib = txtarea.nextSibling;
451    parNod.removeChild(txtarea);
452    parNod.insertBefore(txtarea, nxtSib);
453}
454
455/**
456 * Handler to close all open Popups
457 */
458function closePopups(){
459  if(!document.getElementById){ return; }
460
461  var divs = document.getElementsByTagName('div');
462  for(var i=0; i < divs.length; i++){
463    if(divs[i].className.indexOf('JSpopup') != -1){
464            divs[i].style.display = 'none';
465    }
466  }
467}
468
469/**
470 * Looks for an element with the ID scroll__here at scrolls to it
471 */
472function scrollToMarker(){
473    var obj = $('scroll__here');
474    if(obj) obj.scrollIntoView();
475}
476
477/**
478 * Looks for an element with the ID focus__this at sets focus to it
479 */
480function focusMarker(){
481    var obj = $('focus__this');
482    if(obj) obj.focus();
483}
484
485/**
486 * Remove messages
487 */
488function cleanMsgArea(){
489    var elems = getElementsByClass('(success|info|error)',document,'div');
490    if(elems){
491        for(var i=0; i<elems.length; i++){
492            elems[i].style.display = 'none';
493        }
494    }
495}
496