xref: /dokuwiki/lib/scripts/tree.js (revision a1dee2b998bc3dc8436bb076435d405ec412e054)
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, opening;
33
34            e.preventDefault();
35
36            dw_tree = e.data;
37            $clicky = jQuery(this);
38            $listitem = $clicky.closest('li');
39            $sublist = $listitem.find('ul').first();
40            opening = $listitem.hasClass('closed');
41            $listitem.toggleClass('open closed');
42            dw_tree.toggle_display($clicky, opening);
43
44            // if already open, close by hiding the sublist
45            if (!opening) {
46                $sublist.dw_hide();
47                return;
48            }
49
50            show_sublist = function (data) {
51                $sublist.hide();
52                if (typeof data !== 'undefined') {
53                    $sublist.html(data);
54                }
55                if ($listitem.hasClass('open')) {
56                    // Only show if user didn’t close the list since starting
57                    // to load the content
58                    $sublist.dw_show();
59                }
60            };
61
62            // just show if already loaded
63            if ($sublist.length > 0) {
64                show_sublist();
65                return;
66            }
67
68            //prepare the new ul
69            $sublist = jQuery('<ul class="idx"/>');
70            $listitem.append($sublist);
71
72            timeout = window.setTimeout(
73                bind(show_sublist, '<li><img src="' + DOKU_BASE + 'lib/images/throbber.gif" alt="loading..." title="loading..." /></li>'), dw_tree.throbber_delay);
74
75            dw_tree.load_data(function (data) {
76                                  window.clearTimeout(timeout);
77                                  show_sublist(data);
78                              }, $clicky);
79        },
80
81        toggle_display: function ($clicky, opening) {
82        },
83
84        load_data: function (show_data, $clicky) {
85            show_data();
86        }
87    };
88
89    jQuery.extend(dw_tree, overrides);
90
91    if (!overrides.deferInit) {
92        dw_tree.init();
93    }
94
95    return dw_tree;
96};
97