summaryrefslogtreecommitdiffstats
path: root/theme/javascript/core/sidebar.js
blob: 5529be40e06a234fe453df23a6a96b124d375309 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
define([
    "utils/storage",
    "utils/platform",
    "core/state",
    "core/search"
], function(storage, platform, state, search) {

    // Toggle sidebar with or withour animation
    var toggleSidebar = function(_state, animation) {
        if (state != null && isOpen() == _state) return;
        if (animation == null) animation = true;

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

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

    // Toggle search
    var toggleSearch = function(_state) {
        if (state != null && isSearchOpen() == _state) return;

        var $summary = state().$book.find(".book-summary");
        $summary.toggleClass("with-search", _state);

        // If search bar is open: focus input
        if (isSearchOpen()) $summary.find(".book-search input").focus();
    };

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

    // Return true if search bar is open
    var isSearchOpen = function() {
        return state().$book.find(".book-summary").hasClass("with-search");
    };

    // Prepare sidebar: state and toggle button
    var init = function() {
        var $book = state().$book;

        // Toggle summary
        $book.find(".book-header .toggle-summary").click(function(e) {
            e.preventDefault();
            toggleSidebar();
        });

        // Toggle search
        $book.find(".book-header .toggle-search").click(function(e) {
            e.preventDefault();
            toggleSearch();
        });

        // Init last state if not mobile
        if (!platform.isMobile) {
            toggleSidebar(storage.get("sidebar", true), false);
        }
    };

    return {
        init: init,
        toggle: toggleSidebar,
        toggleSearch: toggleSearch
    }
});