summaryrefslogtreecommitdiffstats
path: root/theme/javascript/core/sidebar.js
blob: 2d9417ec7bf496312689c83b0d1657b7411492ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
define([
    'jQuery',
    'lodash',
    'utils/storage',
    'utils/platform',
    'core/state'
], function($, _, storage, platform, state) {
    // Toggle sidebar with or withour animation
    function toggleSidebar(_state, animation) {
        if (state != null && isOpen() == _state) return;
        if (animation == null) animation = true;

        state.$book.toggleClass('without-animation', !animation);
        state.$book.toggleClass('with-summary', _state);

        storage.set('sidebar', isOpen());
    }

    // Return true if sidebar is open
    function isOpen() {
        return state.$book.hasClass('with-summary');
    }

    // Prepare sidebar: state and toggle button
    function init() {
        // Init last state if not mobile
        if (!platform.isMobile) {
            toggleSidebar(storage.get('sidebar', true), false);
        }
    }

    // Filter summary with a list of path
    function filterSummary(paths) {
        var $summary = $('.book-summary');

        $summary.find('li').each(function() {
            var path = $(this).data('path');
            var st = paths == null || _.contains(paths, path);

            $(this).toggle(st);
            if (st) $(this).parents('li').show();
        });
    }

    return {
        init: init,
        isOpen: isOpen,
        toggle: toggleSidebar,
        filter: filterSummary
    };
});