xref: /template/sprintdoc/js/sidebar.js (revision f293a16b9c1ea8b393c72a061ef3b893b8dae4dd)
1/**
2 * Initialize the sidebar to have a toggleable menu system with icon support
3 */
4jQuery(function () {
5    const $nav = jQuery('#dokuwiki__aside').find('nav.nav-main');
6    if (!$nav.length) return;
7
8    const ELEMENT = 'h1,h2,h3,h4,h5'; // FIXME move to config
9    const $elements = $nav.find(ELEMENT);
10    $elements.each(function () {
11        const $me = jQuery(this);
12
13        // prepare text and the optional icon
14        const data = $me.text().split('@', 2);
15        const text = data[0].trim();
16        const $icon = jQuery('<span>')
17            .text(text.substr(0, 1).toUpperCase() + text.substr(1, 1).toLowerCase());
18        if (data[1]) {
19            const src = data[1].trim();
20            $icon.load(DOKU_BASE + 'lib/tpl/sprintdoc/svg.php?svg=' + src + '&e=1'); // directly embed
21        }
22
23        // make the new toggler
24        const $toggler = jQuery('<h6>')
25                .addClass('navi-toggle')
26                .text(text)
27                .prepend($icon)
28            ;
29
30        // wrap all following sibling til the next element in a wrapper
31        const $wrap = jQuery('<div>').addClass('navi-pane');
32        const $sibs = $me.nextAll();
33        for (let i = 0; i < $sibs.length; i++) {
34            const $sib = jQuery($sibs[i]);
35            if($sib.is(ELEMENT)) break;
36            $sib.detach().appendTo($wrap);
37        }
38        $wrap.hide();
39        $wrap.insertAfter($me);
40
41        // replace element with toggler
42        $me.replaceWith($toggler);
43
44        // add toggling the wrapper
45        $toggler.click(function () {
46            $wrap.dw_toggle(undefined, function () {
47                $me.toggleClass('open');
48            });
49        });
50
51    });
52
53});
54