xref: /dokuwiki/lib/scripts/index.js (revision 7def82c02606c39102316009184138cdcbd904d3)
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 the new ul
76        var ul = jQuery('<ul class="idx"/>');
77
78        var timeout = window.setTimeout(function(){
79            // show the throbber as needed
80            if (listitem.open) {
81              ul.html('<li><img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="loading..." title="loading..." /></li>')
82                .appendTo(listitem);
83              listitem.className='open';
84            }
85        }, this.throbber_delay);
86
87        jQuery.post(
88            DOKU_BASE + 'lib/exe/ajax.php',
89            clicky.search.substr(1)+'&call=index',
90            function (data) {
91                window.clearTimeout(timeout);
92                ul.html(data);
93                index.treeattach(ul[0]);
94                if (listitem.className!='open') {
95                  if (!listitem.open) {
96                    this.style.display='none';
97                  }
98                  listitem.appendChild(ul[0]);
99                  if (listitem.open) {
100                    listitem.className='open';
101                  }
102                }
103            },
104            'html'
105        );
106        e.preventDefault();
107        return;
108
109    }
110};
111
112
113addInitEvent(function(){
114    index.treeattach($('index__tree'));
115});
116