xref: /dokuwiki/lib/scripts/tree.js (revision 1420e73d5e21de82e3e1a7a88bdaac5cd6827c92)
1jQuery.fn.dw_tree = function(overrides) {
2    var dw_tree = {
3
4        /**
5         * Delay in ms before showing the throbber.
6         * Used to skip the throbber for fast AJAX calls.
7         */
8        throbber_delay: 500,
9
10        $obj: this,
11
12        toggle_selector: 'a.idx_dir',
13
14        init: function () {
15            this.$obj.delegate(this.toggle_selector, 'click', this,
16                               this.toggle);
17        },
18
19        /**
20         * Open or close a subtree using AJAX
21         * The contents of subtrees are "cached" until the page is reloaded.
22         * A "loading" indicator is shown only when the AJAX call is slow.
23         *
24         * @author Andreas Gohr <andi@splitbrain.org>
25         * @author Ben Coburn <btcoburn@silicodon.net>
26         * @author Pierre Spring <pierre.spring@caillou.ch>
27         */
28        toggle: function (e) {
29            var $listitem, $sublist, timeout, $clicky, show_sublist, dw_tree, opening;
30
31            e.preventDefault();
32
33            dw_tree = e.data;
34            $clicky = jQuery(this);
35            $listitem = $clicky.closest('li');
36            $sublist = $listitem.find('ul').first();
37            opening = $listitem.hasClass('closed');
38            $listitem.toggleClass('open closed');
39            dw_tree.toggle_display($clicky, opening);
40
41            // if already open, close by hiding the sublist
42            if (!opening) {
43                $sublist.dw_hide();
44                return;
45            }
46
47            show_sublist = function (data) {
48                $sublist.hide();
49                if (typeof data !== 'undefined') {
50                    $sublist.html(data);
51                }
52                if ($listitem.hasClass('open')) {
53                    // Only show if user didn’t close the list since starting
54                    // to load the content
55                    $sublist.dw_show();
56                }
57            };
58
59            // just show if already loaded
60            if ($sublist.length > 0) {
61                show_sublist();
62                return;
63            }
64
65            //prepare the new ul
66            $sublist = jQuery('<ul class="idx"/>');
67            $listitem.append($sublist);
68
69            timeout = window.setTimeout(
70                bind(show_sublist, '<li><img src="' + DOKU_BASE + 'lib/images/throbber.gif" alt="loading..." title="loading..." /></li>'), dw_tree.throbber_delay);
71
72            dw_tree.load_data(function (data) {
73                                  window.clearTimeout(timeout);
74                                  show_sublist(data);
75                              }, $clicky);
76        },
77
78        toggle_display: function ($clicky, opening) {
79        },
80
81        load_data: function (show_data, $clicky) {
82            show_data();
83        }
84    };
85
86    jQuery.extend(dw_tree, overrides);
87
88    if (!overrides.deferInit) {
89        dw_tree.init();
90    }
91
92    return dw_tree;
93};
94