xref: /dokuwiki/lib/scripts/index.js (revision 449095130497f47d1f9ec3f67d70d2eb1c99446e)
1/**
2 * Javascript for index view
3 *
4 * @author Andreas Gohr <andi@splitbrain.org>
5 */
6
7var index = {
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            // get the listitem the elem belongs to
31            var listitem = elem.parentNode;
32            while (listitem.tagName != 'LI') {
33              listitem = listitem.parentNode;
34            }
35            //when there are uls under this listitem mark this listitem as opened
36            if (listitem.getElementsByTagName('ul').length) {
37              listitem.open = true;
38            }
39        }
40    },
41
42    /**
43     * Open or close a subtree using AJAX
44     * The contents of subtrees are "cached" untill the page is reloaded.
45     * A "loading" indicator is shown only when the AJAX call is slow.
46     *
47     * @author Andreas Gohr <andi@splitbrain.org>
48     * @author Ben Coburn <btcoburn@silicodon.net>
49     */
50    toggle: function(e,clicky){
51        var listitem = clicky.parentNode.parentNode;
52
53        listitem.open = !listitem.open;
54        // listitem.open represents now the action to be done
55
56        // if already open, close by removing the sublist
57        var sublists = listitem.getElementsByTagName('ul');
58        if(!listitem.open){
59            if (sublists.length) {
60              sublists[0].style.display='none';
61            }
62            listitem.className='closed';
63            e.preventDefault();
64            return false;
65        }
66
67        // just show if already loaded
68        if(sublists.length && listitem.open){
69            sublists[0].style.display='';
70            listitem.className='open';
71            e.preventDefault();
72            return false;
73        }
74
75        // prepare an AJAX call to fetch the subtree
76        var ajax = new sack(DOKU_BASE + 'lib/exe/ajax.php');
77        ajax.AjaxFailedAlert = '';
78        ajax.encodeURIString = false;
79        if(ajax.failed) return true;
80
81        //prepare the new ul
82        var ul = document.createElement('ul');
83        ul.className = 'idx';
84        timeout = window.setTimeout(function(){
85            // show the throbber as needed
86            if (listitem.open) {
87              ul.innerHTML = '<li><img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="loading..." title="loading..." /></li>';
88              listitem.appendChild(ul);
89              listitem.className='open';
90            }
91        }, this.throbber_delay);
92        ajax.elementObj = ul;
93        ajax.afterCompletion = function(){
94            window.clearTimeout(timeout);
95            index.treeattach(ul);
96            if (listitem.className!='open') {
97              if (!listitem.open) {
98                ul.style.display='none';
99              }
100              listitem.appendChild(ul);
101              if (listitem.open) {
102                listitem.className='open';
103              }
104            }
105        };
106        ajax.runAJAX(clicky.search.substr(1)+'&call=index');
107        e.preventDefault();
108        return false;
109    }
110};
111
112
113addInitEvent(function(){
114    index.treeattach($('index__tree'));
115});
116