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