xref: /dokuwiki/lib/scripts/index.js (revision 2674fdfba016552f6f843df0e812db75b904fcda)
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(e){ return index.toggle(e,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(e,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            e.preventDefault();
49            return false;
50        }
51
52        // just show if already loaded
53        if(sublists.length && listitem.className=='closed'){
54            sublists[0].style.display='';
55            listitem.className='open';
56            e.preventDefault();
57            return false;
58        }
59
60        // prepare an AJAX call to fetch the subtree
61        var ajax = new sack(DOKU_BASE + 'lib/exe/ajax.php');
62        ajax.AjaxFailedAlert = '';
63        ajax.encodeURIString = false;
64        if(ajax.failed) return true;
65
66        //prepare the new ul
67        var ul = document.createElement('ul');
68        ul.className = 'idx';
69        timeout = window.setTimeout(function(){
70            // show the throbber as needed
71            ul.innerHTML = '<li><img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="loading..." title="loading..." /></li>';
72            listitem.appendChild(ul);
73            listitem.className='open';
74        }, this.throbber_delay);
75        ajax.elementObj = ul;
76        ajax.afterCompletion = function(){
77            window.clearTimeout(timeout);
78            index.treeattach(ul);
79            if (listitem.className!='open') {
80                listitem.appendChild(ul);
81                listitem.className='open';
82            }
83        };
84        ajax.runAJAX(clicky.search.substr(1)+'&call=index');
85        e.preventDefault();
86        return false;
87    }
88};
89
90
91addInitEvent(function(){
92    index.treeattach($('index__tree'));
93});
94