xref: /dokuwiki/lib/scripts/script.js (revision b5941dfab8516bd445afebc91d6a4942cab4d5f0)
1// if jQuery was loaded, let's make it noConflict here.
2if ('function' === typeof jQuery && 'function' === typeof jQuery.noConflict) {
3    jQuery.noConflict();
4}
5
6/**
7 * Mark a JavaScript function as deprecated
8 *
9 * This will print a warning to the JavaScript console (if available) in
10 * Firebug and Chrome and a stack trace (if available) to easily locate the
11 * problematic function call.
12 *
13 * @param msg optional message to print
14 */
15function DEPRECATED(msg){
16    if(!window.console) return;
17    if(!msg) msg = '';
18
19    var func;
20    if(arguments.callee) func = arguments.callee.caller.name;
21    if(func) func = ' '+func+'()';
22    var line = 'DEPRECATED function call'+func+'. '+msg;
23
24    if(console.warn){
25        console.warn(line);
26    }else{
27        console.log(line);
28    }
29
30    if(console.trace) console.trace();
31}
32
33/**
34 * Construct a wrapper function for deprecated function names
35 *
36 * This function returns a wrapper function which just calls DEPRECATED
37 * and the new function.
38 *
39 * @param func    The new function
40 * @param context Optional; The context (`this`) of the call
41 */
42function DEPRECATED_WRAP(func, context) {
43    return function () {
44        DEPRECATED();
45        return func.apply(context || this, arguments);
46    }
47}
48
49/**
50 * Some of these scripts were taken from wikipedia.org and were modified for DokuWiki
51 */
52
53/**
54 * Some browser detection
55 */
56var clientPC  = navigator.userAgent.toLowerCase(); // Get client info
57var is_macos  = navigator.appVersion.indexOf('Mac') != -1;
58var is_gecko  = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1) &&
59                (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));
60var is_safari = ((clientPC.indexOf('applewebkit')!=-1) && (clientPC.indexOf('spoofer')==-1));
61var is_khtml  = (navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ));
62if (clientPC.indexOf('opera')!=-1) {
63    var is_opera = true;
64    var is_opera_preseven = (window.opera && !document.childNodes);
65    var is_opera_seven = (window.opera && document.childNodes);
66}
67
68/**
69 * Handy shortcut to document.getElementById
70 *
71 * This function was taken from the prototype library
72 *
73 * @link http://prototype.conio.net/
74 */
75function $() {
76  DEPRECATED('Please use the JQuery() function instead.');
77
78  var elements = new Array();
79
80  for (var i = 0; i < arguments.length; i++) {
81    var element = arguments[i];
82    if (typeof element == 'string')
83      element = document.getElementById(element);
84
85    if (arguments.length == 1)
86      return element;
87
88    elements.push(element);
89  }
90
91  return elements;
92}
93
94/**
95 * Simple function to check if a global var is defined
96 *
97 * @author Kae Verens
98 * @link http://verens.com/archives/2005/07/25/isset-for-javascript/#comment-2835
99 */
100function isset(varname){
101  return(typeof(window[varname])!='undefined');
102}
103
104/**
105 * Get the computed style of a node.
106 *
107 * @link https://acidmartin.wordpress.com/2008/08/26/style-get-any-css-property-value-of-an-object/
108 * @link http://svn.dojotoolkit.org/src/dojo/trunk/_base/html.js
109 */
110function gcs(node){
111    if(node.currentStyle){
112        return node.currentStyle;
113    }else{
114        return node.ownerDocument.defaultView.getComputedStyle(node, null);
115    }
116}
117
118/**
119 * Escape special chars in JavaScript
120 *
121 * @author Andreas Gohr <andi@splitbrain.org>
122 */
123function jsEscape(text){
124    var re=new RegExp("\\\\","g");
125    text=text.replace(re,"\\\\");
126    re=new RegExp("'","g");
127    text=text.replace(re,"\\'");
128    re=new RegExp('"',"g");
129    text=text.replace(re,'&quot;');
130    re=new RegExp("\\\\\\\\n","g");
131    text=text.replace(re,"\\n");
132    return text;
133}
134
135/**
136 * This function escapes some special chars
137 * @deprecated by above function
138 */
139function escapeQuotes(text) {
140  var re=new RegExp("'","g");
141  text=text.replace(re,"\\'");
142  re=new RegExp('"',"g");
143  text=text.replace(re,'&quot;');
144  re=new RegExp("\\n","g");
145  text=text.replace(re,"\\n");
146  return text;
147}
148
149/**
150 * Prints a animated gif to show the search is performed
151 *
152 * Because we need to modify the DOM here before the document is loaded
153 * and parsed completely we have to rely on document.write()
154 *
155 * @author Andreas Gohr <andi@splitbrain.org>
156 */
157function showLoadBar(){
158
159  document.write('<img src="'+DOKU_BASE+'lib/images/loading.gif" '+
160                 'width="150" height="12" alt="..." />');
161
162  /* this does not work reliable in IE
163  obj = $(id);
164
165  if(obj){
166    obj.innerHTML = '<img src="'+DOKU_BASE+'lib/images/loading.gif" '+
167                    'width="150" height="12" alt="..." />';
168    obj.style.display="block";
169  }
170  */
171}
172
173/**
174 * Disables the animated gif to show the search is done
175 *
176 * @author Andreas Gohr <andi@splitbrain.org>
177 */
178function hideLoadBar(id){
179  obj = $(id);
180  if(obj) obj.style.display="none";
181}
182
183
184/**
185 * Create JavaScript mouseover popup
186 */
187function insitu_popup(target, popup_id) {
188
189    // get or create the popup div
190    var fndiv = $(popup_id);
191    if(!fndiv){
192        fndiv = document.createElement('div');
193        fndiv.id        = popup_id;
194        fndiv.className = 'insitu-footnote JSpopup dokuwiki';
195
196        // autoclose on mouseout - ignoring bubbled up events
197        addEvent(fndiv,'mouseout',function(e){
198            var p = e.relatedTarget || e.toElement;
199            while (p && p !== this) {
200                p = p.parentNode;
201            }
202            if (p === this) {
203                return;
204            }
205            // okay, hide it
206            this.style.display='none';
207        });
208        getElementsByClass('dokuwiki', document.body, 'div')[0].appendChild(fndiv);
209    }
210
211    var non_static_parent = fndiv.parentNode;
212    while (non_static_parent != document && gcs(non_static_parent)['position'] == 'static') {
213        non_static_parent = non_static_parent.parentNode;
214    }
215
216    var fixed_target_parent = target;
217    while (fixed_target_parent != document && gcs(fixed_target_parent)['position'] != 'fixed') {
218        fixed_target_parent = fixed_target_parent.parentNode;
219    }
220
221    // position the div and make it visible
222    if (fixed_target_parent != document) {
223        // the target has position fixed, that means the footnote needs to be fixed, too
224        fndiv.style.position = 'fixed';
225    } else {
226        fndiv.style.position = 'absolute';
227    }
228
229    if (fixed_target_parent != document || non_static_parent == document) {
230        fndiv.style.left = findPosX(target)+'px';
231        fndiv.style.top  = (findPosY(target)+target.offsetHeight * 1.5) + 'px';
232    } else {
233        fndiv.style.left = (findPosX(target) - findPosX(non_static_parent)) +'px';
234        fndiv.style.top  = (findPosY(target)+target.offsetHeight * 1.5 - findPosY(non_static_parent)) + 'px';
235    }
236
237    fndiv.style.display = '';
238    return fndiv;
239}
240
241/**
242 * Display an insitu footnote popup
243 *
244 * @author Andreas Gohr <andi@splitbrain.org>
245 * @author Chris Smith <chris@jalakai.co.uk>
246 */
247function footnote(e){
248    var fndiv = insitu_popup(e.target, 'insitu__fn');
249
250    // locate the footnote anchor element
251    var a = $("fn__" + e.target.id.substr(5));
252    if (!a){ return; }
253
254    // anchor parent is the footnote container, get its innerHTML
255    var content = new String (a.parentNode.parentNode.innerHTML);
256
257    // strip the leading content anchors and their comma separators
258    content = content.replace(/<sup>.*<\/sup>/gi, '');
259    content = content.replace(/^\s+(,\s+)+/,'');
260
261    // prefix ids on any elements with "insitu__" to ensure they remain unique
262    content = content.replace(/\bid=(['"])([^"']+)\1/gi,'id="insitu__$2');
263
264    // now put the content into the wrapper
265    fndiv.innerHTML = content;
266}
267
268/**
269 * Add the event handlers to footnotes
270 *
271 * @author Andreas Gohr <andi@splitbrain.org>
272 */
273addInitEvent(function(){
274    var elems = getElementsByClass('fn_top',null,'a');
275    for(var i=0; i<elems.length; i++){
276        addEvent(elems[i],'mouseover',function(e){footnote(e);});
277    }
278});
279
280
281/**
282 * Handler to close all open Popups
283 */
284function closePopups(){
285    jQuery('div.JSpopup').hide();
286}
287
288function revisionsForm(){
289    var revForm = $('page__revisions');
290    if (!revForm) return;
291    var elems = revForm.elements;
292    var countTicks = 0;
293    for (var i=0; i<elems.length; i++) {
294        var input1 = elems[i];
295        if (input1.type=='checkbox') {
296            addEvent(input1,'click',function(e){
297                if (this.checked) countTicks++;
298                else countTicks--;
299                for (var j=0; j<elems.length; j++) {
300                    var input2 = elems[j];
301                    if (countTicks >= 2) input2.disabled = (input2.type=='checkbox' && !input2.checked);
302                    else input2.disabled = (input2.type!='checkbox');
303                }
304            });
305            input1.checked = false; // chrome reselects on back button which messes up the logic
306        } else if(input1.type=='submit'){
307            input1.disabled = true;
308        }
309    }
310}
311
312
313/**
314 * disable multiple revisions checkboxes if two are checked
315 *
316 * @author Anika Henke <anika@selfthinker.org>
317 */
318addInitEvent(revisionsForm);
319
320
321/**
322 * Highlight the section when hovering over the appropriate section edit button
323 *
324 * @author Andreas Gohr <andi@splitbrain.org>
325 */
326addInitEvent(function(){
327    var btns = getElementsByClass('btn_secedit',document,'form');
328    for(var i=0; i<btns.length; i++){
329        addEvent(btns[i],'mouseover',function(e){
330            var tgt = this.parentNode;
331            var nr = tgt.className.match(/(\s+|^)editbutton_(\d+)(\s+|$)/)[2];
332            do {
333                tgt = tgt.previousSibling;
334            } while (tgt !== null && typeof tgt.tagName === 'undefined');
335            if (tgt === null) return;
336            while(typeof tgt.className === 'undefined' ||
337                  tgt.className.match('(\\s+|^)sectionedit' + nr + '(\\s+|$)') === null) {
338                if (typeof tgt.className !== 'undefined') {
339                    tgt.className += ' section_highlight';
340                }
341                tgt = (tgt.previousSibling !== null) ? tgt.previousSibling : tgt.parentNode;
342            }
343            if (typeof tgt.className !== 'undefined') tgt.className += ' section_highlight';
344        });
345
346        addEvent(btns[i],'mouseout',function(e){
347            var secs = getElementsByClass('section_highlight');
348            for(var j=0; j<secs.length; j++){
349                secs[j].className = secs[j].className.replace(/section_highlight/g,'');
350            }
351        });
352    }
353});
354
355