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