xref: /dokuwiki/lib/scripts/script.js (revision 6154103c237cc4bbb5f69d17c315714c5b1b6a2d)
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(){
133  if(document.getElementById){
134    document.write('<img src="'+DOKU_BASE+'lib/images/loading.gif" '+
135                   'width="150" height="12" id="loading" />');
136  }
137}
138
139/**
140 * Disables the animated gif to show the search is done
141 *
142 * @author Andreas Gohr <andi@splitbrain.org>
143 */
144function hideLoadBar(){
145  if(document.getElementById){
146    $('loading').style.display="none";
147  }
148}
149
150/*
151 * Insert the selected filename and close the window
152 *
153 * @see http://www.alexking.org/index.php?content=software/javascript/content.php
154 */
155function mediaSelect(file){
156	opener.insertAtCarret('wikitext','{{'+file+'}}');
157  window.close();
158}
159
160/**
161 * For the upload Dialog. Prefills the wikiname.
162 */
163function suggestWikiname(){
164  var file = document.upload.upload.value;
165
166  file = file.substr(file.lastIndexOf('/')+1);
167  file = file.substr(file.lastIndexOf('\\')+1);
168
169  document.upload.id.value = file;
170}
171
172/**
173 * Adds the toggle switch to the TOC
174 */
175function addTocToggle() {
176	if(!document.getElementById) return;
177	var header = $('toc__header');
178  if(!header) return;
179
180  var showimg     = document.createElement('img');
181	showimg.id      = 'toc__show';
182  showimg.src     = DOKU_BASE+'lib/images/arrow_down.gif';
183  showimg.alt     = '+';
184	showimg.onclick = toggleToc;
185  showimg.style.display = 'none';
186
187	var hideimg     = document.createElement('img');
188	hideimg.id      = 'toc__hide';
189  hideimg.src     = DOKU_BASE+'lib/images/arrow_up.gif';
190  hideimg.alt     = '-';
191	hideimg.onclick = toggleToc;
192
193  prependChild(header,showimg);
194  prependChild(header,hideimg);
195}
196
197/**
198 * This toggles the visibility of the Table of Contents
199 */
200function toggleToc() {
201  var toc = $('toc__inside');
202  var showimg = $('toc__show');
203  var hideimg = $('toc__hide');
204  if(toc.style.display == 'none') {
205    toc.style.display      = '';
206    hideimg.style.display = '';
207    showimg.style.display = 'none';
208  } else {
209    toc.style.display      = 'none';
210    hideimg.style.display = 'none';
211    showimg.style.display = '';
212  }
213}
214
215/*
216 * This sets a cookie by JavaScript
217 *
218 * @see http://www.webreference.com/js/column8/functions.html
219 */
220function setCookie(name, value, expires, path, domain, secure) {
221  var curCookie = name + "=" + escape(value) +
222      ((expires) ? "; expires=" + expires.toGMTString() : "") +
223      ((path) ? "; path=" + path : "") +
224      ((domain) ? "; domain=" + domain : "") +
225      ((secure) ? "; secure" : "");
226  document.cookie = curCookie;
227}
228
229/*
230 * This reads a cookie by JavaScript
231 *
232 * @see http://www.webreference.com/js/column8/functions.html
233 */
234function getCookie(name) {
235  var dc = document.cookie;
236  var prefix = name + "=";
237  var begin = dc.indexOf("; " + prefix);
238  if (begin == -1) {
239    begin = dc.indexOf(prefix);
240    if (begin !== 0){ return null; }
241  } else {
242    begin += 2;
243  }
244  var end = document.cookie.indexOf(";", begin);
245  if (end == -1){
246    end = dc.length;
247  }
248  return unescape(dc.substring(begin + prefix.length, end));
249}
250
251/*
252 * This is needed for the cookie functions
253 *
254 * @see http://www.webreference.com/js/column8/functions.html
255 */
256function fixDate(date) {
257  var base = new Date(0);
258  var skew = base.getTime();
259  if (skew > 0){
260    date.setTime(date.getTime() - skew);
261  }
262}
263
264/*
265 * This enables/disables checkboxes for acl-administration
266 *
267 * @author Frank Schubert <frank@schokilade.de>
268 */
269function checkAclLevel(){
270  if(document.getElementById) {
271    var scope = $('acl_scope').value;
272
273    //check for namespace
274    if( (scope.indexOf(":*") > 0) || (scope == "*") ){
275      document.getElementsByName('acl_checkbox[4]')[0].disabled=false;
276      document.getElementsByName('acl_checkbox[8]')[0].disabled=false;
277    }else{
278      document.getElementsByName('acl_checkbox[4]')[0].checked=false;
279      document.getElementsByName('acl_checkbox[8]')[0].checked=false;
280
281      document.getElementsByName('acl_checkbox[4]')[0].disabled=true;
282      document.getElementsByName('acl_checkbox[8]')[0].disabled=true;
283    }
284  }
285}
286
287/**
288 * insitu footnote addition
289 *
290 * provide a wrapper for domTT javascript library
291 * this function is placed in the onmouseover event of footnote references in the main page
292 *
293 * @author Chris Smith <chris [at] jalakai [dot] co [dot] uk>
294 */
295var currentFootnote = 0;
296function fnt(id, e, evt) {
297
298    if (currentFootnote && id != currentFootnote) {
299        domTT_close($('insitu-fn'+currentFootnote));
300    }
301
302    // does the footnote tooltip already exist?
303    var fnote = $('insitu-fn'+id);
304    var footnote;
305    if (!fnote) {
306        // if not create it...
307
308        // locate the footnote anchor element
309        var a = $( "fn"+id );
310        if (!a){ return; }
311
312        // anchor parent is the footnote container, get its innerHTML
313        footnote = new String (a.parentNode.innerHTML);
314
315        // strip the leading footnote anchors and their comma separators
316        footnote = footnote.replace(/<a\s.*?href=\".*\#fnt\d+\".*?<\/a>/gi, '');
317        footnote = footnote.replace(/^\s+(,\s+)+/,'');
318
319        // prefix ids on any elements with "insitu-" to ensure they remain unique
320        footnote = footnote.replace(/\bid=\"(.*?)\"/gi,'id="insitu-$1');
321   	} else {
322        footnote = new String(fnt.innerHTML);
323    }
324
325    // activate the tooltip
326    domTT_activate(e, evt, 'content', footnote, 'type', 'velcro', 'id', 'insitu-fn'+id, 'styleClass', 'insitu-footnote JSpopup', 'maxWidth', document.body.offsetWidth*0.4);
327    currentFootnote = id;
328}
329
330
331/**
332 * Add the edit window size controls
333 */
334function initSizeCtl(ctlid,edid){
335		if(!document.getElementById){ return; }
336
337    var ctl      = $(ctlid);
338    var textarea = $(edid);
339
340    var hgt = getCookie('DokuWikisizeCtl');
341    if(hgt === null || hgt === ''){
342      textarea.style.height = '300px';
343    }else{
344      textarea.style.height = hgt;
345    }
346
347    var l = document.createElement('img');
348    var s = document.createElement('img');
349    l.src = DOKU_BASE+'lib/images/larger.gif';
350    s.src = DOKU_BASE+'lib/images/smaller.gif';
351		addEvent(l,'click',function(){sizeCtl(edid,100);});
352		addEvent(s,'click',function(){sizeCtl(edid,-100);});
353    ctl.appendChild(l);
354    ctl.appendChild(s);
355}
356
357/**
358 * This sets the vertical size of the editbox
359 */
360function sizeCtl(edid,val){
361  var textarea = $(edid);
362  var height = parseInt(textarea.style.height.substr(0,textarea.style.height.length-2));
363  height += val;
364  textarea.style.height = height+'px';
365
366  var now = new Date();
367  fixDate(now);
368  now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000); //expire in a year
369  setCookie('DokuWikisizeCtl',textarea.style.height,now);
370}
371
372/**
373 * Handler to close all open Popups
374 */
375function closePopups(){
376  if(!document.getElementById){ return; }
377
378  var divs = document.getElementsByTagName('div');
379  for(var i=0; i < divs.length; i++){
380    if(divs[i].className.indexOf('JSpopup') != -1){
381			divs[i].style.display = 'none';
382    }
383  }
384}
385
386/**
387 * Looks for an element with the ID scroll__here at scrolls to it
388 */
389function scrollToMarker(){
390	var obj = $('scroll__here');
391	if(obj) obj.scrollIntoView();
392}
393