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