xref: /dokuwiki/lib/scripts/index.js (revision 9a3b7d9f166ec2227b91614228c308f50d3e259f)
1/**
2 * Javascript for index view
3 *
4 * @author Andreas Gohr <andi@splitbrain.org>
5 */
6
7index = {
8
9     /**
10     * Delay in ms before showing the throbber.
11     * Used to skip the throbber for fast AJAX calls.
12     */
13    throbber_delay: 500,
14
15    /**
16     * Attach event handlers to all "folders" below the given element
17     *
18     * @author Andreas Gohr <andi@splitbrain.org>
19     */
20    treeattach: function(obj){
21        if(!obj) return;
22
23        var items = getElementsByClass('idx_dir',obj,'a');
24        for(var i=0; i<items.length; i++){
25            var elem = items[i];
26
27            // attach action to make the link clickable by AJAX
28            addEvent(elem,'click',function(event){ return index.toggle(event,this); });
29        }
30    },
31
32    /**
33     * Open or close a subtree using AJAX
34     * The contents of subtrees are "cached" untill the page is reloaded.
35     * A "loading" indicator is shown only when the AJAX call is slow.
36     *
37     * @author Andreas Gohr <andi@splitbrain.org>
38     * @author Ben Coburn <btcoburn@silicodon.net>
39     */
40    toggle: function(event,clicky){
41        var listitem = clicky.parentNode.parentNode;
42
43        // if already open, close by removing the sublist
44        var sublists = listitem.getElementsByTagName('ul');
45        if(sublists.length && listitem.className=='open'){
46            sublists[0].style.display='none';
47            listitem.className='closed';
48            return false;
49        }
50
51        // just show if already loaded
52        if(sublists.length && listitem.className=='closed'){
53            sublists[0].style.display='';
54            listitem.className='open';
55            return false;
56        }
57
58        // prepare an AJAX call to fetch the subtree
59        var ajax = new sack(DOKU_BASE + 'lib/exe/ajax.php');
60        ajax.AjaxFailedAlert = '';
61        ajax.encodeURIString = false;
62        if(ajax.failed) return true;
63
64        //prepare the new ul
65        var ul = document.createElement('ul');
66        ul.className = 'idx';
67        timeout = window.setTimeout(function(){
68            // show the throbber as needed
69            ul.innerHTML = '<li><img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="loading..." title="loading..." /></li>';
70            listitem.appendChild(ul);
71            listitem.className='open';
72        }, this.throbber_delay);
73        ajax.elementObj = ul;
74        ajax.afterCompletion = function(){
75            window.clearTimeout(timeout);
76            index.treeattach(ul);
77            if (listitem.className!='open') {
78                listitem.appendChild(ul);
79                listitem.className='open';
80            }
81        };
82        ajax.runAJAX(clicky.search.substr(1)+'&call=index');
83        return false;
84
85    }
86};
87
88
89addInitEvent(function(){
90    index.treeattach($('index__tree'));
91});
92