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