xref: /dokuwiki/lib/scripts/script.js (revision ccf5ab4be633f1da1ed602acaf3f1dc0ae27d388)
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 * Handy shortcut to document.getElementById
21 *
22 * This function was taken from the prototype library
23 *
24 * @link http://prototype.conio.net/
25 */
26function $() {
27  var elements = new Array();
28
29  for (var i = 0; i < arguments.length; i++) {
30    var element = arguments[i];
31    if (typeof element == 'string')
32      element = document.getElementById(element);
33
34    if (arguments.length == 1)
35      return element;
36
37    elements.push(element);
38  }
39
40  return elements;
41}
42
43/**
44 * Get the X offset of the top left corner of the given object
45 *
46 * @link http://www.quirksmode.org/index.html?/js/findpos.html
47 */
48function findPosX(object){
49  var curleft = 0;
50  var obj = $(object);
51  if (obj.offsetParent){
52    while (obj.offsetParent){
53      curleft += obj.offsetLeft;
54      obj = obj.offsetParent;
55    }
56  }
57  else if (obj.x){
58    curleft += obj.x;
59  }
60  return curleft;
61} //end findPosX function
62
63/**
64 * Get the Y offset of the top left corner of the given object
65 *
66 * @link http://www.quirksmode.org/index.html?/js/findpos.html
67 */
68function findPosY(object){
69  var curtop = 0;
70  var obj = $(object);
71  if (obj.offsetParent){
72    while (obj.offsetParent){
73      curtop += obj.offsetTop;
74      obj = obj.offsetParent;
75    }
76  }
77  else if (obj.y){
78    curtop += obj.y;
79  }
80  return curtop;
81} //end findPosY function
82
83/**
84 * Escape special chars in JavaScript
85 *
86 * @author Andreas Gohr <andi@splitbrain.org>
87 */
88function jsEscape(text){
89    var re=new RegExp("\\\\","g");
90    text=text.replace(re,"\\\\");
91    re=new RegExp("'","g");
92    text=text.replace(re,"\\'");
93    re=new RegExp('"',"g");
94    text=text.replace(re,'&quot;');
95    re=new RegExp("\\\\\\\\n","g");
96    text=text.replace(re,"\\n");
97    return text;
98}
99
100/**
101 * This function escapes some special chars
102 * @deprecated by above function
103 */
104function escapeQuotes(text) {
105  var re=new RegExp("'","g");
106  text=text.replace(re,"\\'");
107  re=new RegExp('"',"g");
108  text=text.replace(re,'&quot;');
109  re=new RegExp("\\n","g");
110  text=text.replace(re,"\\n");
111  return text;
112}
113
114/**
115 * Adds a node as the first childenode to the given parent
116 *
117 * @see appendChild()
118 */
119function prependChild(parent,element) {
120    if(!parent.firstChild){
121        parent.appendChild(element);
122    }else{
123        parent.insertBefore(element,parent.firstChild);
124    }
125}
126
127/**
128 * Prints a animated gif to show the search is performed
129 *
130 * @author Andreas Gohr <andi@splitbrain.org>
131 */
132function showLoadBar(id){
133  obj = $(id);
134
135  if(obj){
136    obj.innerHTML = '<img src="'+DOKU_BASE+'lib/images/loading.gif" '+
137                    'width="150" height="12" alt="..." />';
138    obj.style.display="block";
139  }
140}
141
142/**
143 * Disables the animated gif to show the search is done
144 *
145 * @author Andreas Gohr <andi@splitbrain.org>
146 */
147function hideLoadBar(id){
148  obj = $(id);
149  if(obj) obj.style.display="none";
150}
151
152/*
153 * Insert the selected filename and close the window
154 *
155 * @see http://www.alexking.org/index.php?content=software/javascript/content.php
156 */
157function mediaSelect(file){
158  opener.insertTags('wiki__text','{{'+file+'|','}}',file);
159  window.close();
160}
161
162/**
163 * For the upload Dialog. Prefills the wikiname.
164 */
165function suggestWikiname(){
166  var file = document.upload.upload.value;
167
168  file = file.substr(file.lastIndexOf('/')+1);
169  file = file.substr(file.lastIndexOf('\\')+1);
170
171  document.upload.id.value = file;
172}
173
174/**
175 * Adds the toggle switch to the TOC
176 */
177function addTocToggle() {
178    if(!document.getElementById) return;
179    var header = $('toc__header');
180  if(!header) return;
181
182  var showimg     = document.createElement('img');
183    showimg.id      = 'toc__show';
184  showimg.src     = DOKU_BASE+'lib/images/arrow_down.gif';
185  showimg.alt     = '+';
186    showimg.onclick = toggleToc;
187  showimg.style.display = 'none';
188
189    var hideimg     = document.createElement('img');
190    hideimg.id      = 'toc__hide';
191  hideimg.src     = DOKU_BASE+'lib/images/arrow_up.gif';
192  hideimg.alt     = '-';
193    hideimg.onclick = toggleToc;
194
195  prependChild(header,showimg);
196  prependChild(header,hideimg);
197}
198
199/**
200 * This toggles the visibility of the Table of Contents
201 */
202function toggleToc() {
203  var toc = $('toc__inside');
204  var showimg = $('toc__show');
205  var hideimg = $('toc__hide');
206  if(toc.style.display == 'none') {
207    toc.style.display      = '';
208    hideimg.style.display = '';
209    showimg.style.display = 'none';
210  } else {
211    toc.style.display      = 'none';
212    hideimg.style.display = 'none';
213    showimg.style.display = '';
214  }
215}
216
217/*
218 * This sets a cookie by JavaScript
219 *
220 * @see http://www.webreference.com/js/column8/functions.html
221 */
222function setCookie(name, value, expires, path, domain, secure) {
223  var curCookie = name + "=" + escape(value) +
224      ((expires) ? "; expires=" + expires.toGMTString() : "") +
225      ((path) ? "; path=" + path : "") +
226      ((domain) ? "; domain=" + domain : "") +
227      ((secure) ? "; secure" : "");
228  document.cookie = curCookie;
229}
230
231/*
232 * This reads a cookie by JavaScript
233 *
234 * @see http://www.webreference.com/js/column8/functions.html
235 */
236function getCookie(name) {
237  var dc = document.cookie;
238  var prefix = name + "=";
239  var begin = dc.indexOf("; " + prefix);
240  if (begin == -1) {
241    begin = dc.indexOf(prefix);
242    if (begin !== 0){ return null; }
243  } else {
244    begin += 2;
245  }
246  var end = document.cookie.indexOf(";", begin);
247  if (end == -1){
248    end = dc.length;
249  }
250  return unescape(dc.substring(begin + prefix.length, end));
251}
252
253/*
254 * This is needed for the cookie functions
255 *
256 * @see http://www.webreference.com/js/column8/functions.html
257 */
258function fixDate(date) {
259  var base = new Date(0);
260  var skew = base.getTime();
261  if (skew > 0){
262    date.setTime(date.getTime() - skew);
263  }
264}
265
266/*
267 * This enables/disables checkboxes for acl-administration
268 *
269 * @author Frank Schubert <frank@schokilade.de>
270 */
271function checkAclLevel(){
272  if(document.getElementById) {
273    var scope = $('acl_scope').value;
274
275    //check for namespace
276    if( (scope.indexOf(":*") > 0) || (scope == "*") ){
277      document.getElementsByName('acl_checkbox[4]')[0].disabled=false;
278      document.getElementsByName('acl_checkbox[8]')[0].disabled=false;
279    }else{
280      document.getElementsByName('acl_checkbox[4]')[0].checked=false;
281      document.getElementsByName('acl_checkbox[8]')[0].checked=false;
282
283      document.getElementsByName('acl_checkbox[4]')[0].disabled=true;
284      document.getElementsByName('acl_checkbox[8]')[0].disabled=true;
285    }
286  }
287}
288
289/**
290 * insitu footnote addition
291 *
292 * provide a wrapper for domTT javascript library
293 * this function is placed in the onmouseover event of footnote references in the main page
294 *
295 * @author Chris Smith <chris [at] jalakai [dot] co [dot] uk>
296 */
297var currentFootnote = 0;
298function fnt(id, e, evt) {
299
300    if (currentFootnote && id != currentFootnote) {
301        domTT_close($('insitu__fn'+currentFootnote));
302    }
303
304    // does the footnote tooltip already exist?
305    var fnote = $('insitu__fn'+id);
306    var footnote;
307    if (!fnote) {
308        // if not create it...
309
310        // locate the footnote anchor element
311        var a = $( "fn__"+id );
312        if (!a){ return; }
313
314        // anchor parent is the footnote container, get its innerHTML
315        footnote = new String (a.parentNode.innerHTML);
316
317        // strip the leading footnote anchors and their comma separators
318        footnote = footnote.replace(/<a\s.*?href=\".*\#fnt__\d+\".*?<\/a>/gi, '');
319        footnote = footnote.replace(/^\s+(,\s+)+/,'');
320
321        // prefix ids on any elements with "insitu__" to ensure they remain unique
322        footnote = footnote.replace(/\bid=\"(.*?)\"/gi,'id="insitu__$1');
323    } else {
324        footnote = new String(fnt.innerHTML);
325    }
326
327    // activate the tooltip
328    domTT_activate(e, evt, 'content', footnote, 'type', 'velcro', 'id', 'insitu__fn'+id, 'styleClass', 'insitu-footnote JSpopup', 'maxWidth', document.body.offsetWidth*0.4);
329    currentFootnote = id;
330}
331
332
333/**
334 * Add the edit window size controls
335 */
336function initSizeCtl(ctlid,edid){
337        if(!document.getElementById){ return; }
338
339    var ctl      = $(ctlid);
340    var textarea = $(edid);
341
342    var hgt = getCookie('DokuWikisizeCtl');
343    if(hgt === null || hgt === ''){
344      textarea.style.height = '300px';
345    }else{
346      textarea.style.height = hgt;
347    }
348
349    var l = document.createElement('img');
350    var s = document.createElement('img');
351    l.src = DOKU_BASE+'lib/images/larger.gif';
352    s.src = DOKU_BASE+'lib/images/smaller.gif';
353        addEvent(l,'click',function(){sizeCtl(edid,100);});
354        addEvent(s,'click',function(){sizeCtl(edid,-100);});
355    ctl.appendChild(l);
356    ctl.appendChild(s);
357}
358
359/**
360 * This sets the vertical size of the editbox
361 */
362function sizeCtl(edid,val){
363  var textarea = $(edid);
364  var height = parseInt(textarea.style.height.substr(0,textarea.style.height.length-2));
365  height += val;
366  textarea.style.height = height+'px';
367
368  var now = new Date();
369  fixDate(now);
370  now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000); //expire in a year
371  setCookie('DokuWikisizeCtl',textarea.style.height,now);
372}
373
374/**
375 * Handler to close all open Popups
376 */
377function closePopups(){
378  if(!document.getElementById){ return; }
379
380  var divs = document.getElementsByTagName('div');
381  for(var i=0; i < divs.length; i++){
382    if(divs[i].className.indexOf('JSpopup') != -1){
383            divs[i].style.display = 'none';
384    }
385  }
386}
387
388/**
389 * Looks for an element with the ID scroll__here at scrolls to it
390 */
391function scrollToMarker(){
392    var obj = $('scroll__here');
393    if(obj) obj.scrollIntoView();
394}
395