1/**
2 * Javascript for index view
3 *
4 * @author Andreas Gohr <andi@splitbrain.org>
5 */
6
7indexmenu_nojs = {
8     /**
9     * Delay in ms before showing the throbber.
10     * Used to skip the throbber for fast AJAX calls.
11     */
12    throbber_delay: 500,
13
14    /**
15     * Attach event handlers to all "folders" below the given element
16     *
17     * @author Andreas Gohr <andi@splitbrain.org>
18     */
19    treeattach: function(iobj){
20	var obj=$('nojs_'+iobj[0]);
21	if (!obj) return;
22
23	var items = getElementsByClass('indexmenu_idx',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 indexmenu_nojs.toggle(e,this,iobj[1]); });
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     * Modified by Samuele Tognini <samuele@netsons.org> for the indexmenu plugin
40     */
41    toggle: function(e,clicky,jsajax){
42        var listitem = clicky.parentNode.parentNode;
43
44        // if already open, close by removing the sublist
45        var sublists = listitem.getElementsByTagName('ul');
46        if(sublists.length && listitem.className=='open'){
47            sublists[0].style.display='none';
48            listitem.className='closed';
49            e.preventDefault();
50            return false;
51        }
52
53        // just show if already loaded
54        if(sublists.length && listitem.className=='closed'){
55            sublists[0].style.display='';
56            listitem.className='open';
57            e.preventDefault();
58            return false;
59        }
60
61        // prepare an AJAX call to fetch the subtree
62	var ajax = new sack(DOKU_BASE+'lib/plugins/indexmenu/ajax.php');
63        ajax.AjaxFailedAlert = '';
64        ajax.encodeURIString = false;
65        if(ajax.failed) return true;
66
67        //prepare the new ul
68        var ul = document.createElement('ul');
69        ul.className = 'idx';
70        timeout = window.setTimeout(function(){
71            // show the throbber as needed
72            ul.innerHTML = '<li><img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="loading..." title="loading..." /></li>';
73            listitem.appendChild(ul);
74            listitem.className='open';
75        }, this.throbber_delay);
76        ajax.elementObj = ul;
77        ajax.afterCompletion = function(){
78            window.clearTimeout(timeout);
79            indexmenu_nojs.treeattach(ul);
80            if (listitem.className!='open') {
81                listitem.appendChild(ul);
82                listitem.className='open';
83            }
84        };
85        ajax.runAJAX(encodeURI('req=index&nojs=1&'+clicky.search.substr(1)+'&max=1'+decodeURIComponent(jsajax)));
86        e.preventDefault();
87        return false;
88    },
89
90    /* Find all nojs indexmenu objects */
91    treefind: function () {
92	var aobj=indexmenu_nojsqueue;
93	if (!aobj) return;
94
95	for (var i in aobj) {
96	    indexmenu_nojs.treeattach(aobj[i]);
97	}
98    }
99};
100
101indexmenu_nojs.treefind();
102