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