summaryrefslogtreecommitdiffstats
path: root/theme/javascript/core/font-settings.js
blob: dbc08799c168a5e2c11d798cdc85f5553030329e (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
define([
    "jQuery",
    "utils/storage"
], function($, storage) {
    var fontState;

    var THEMES = {
        "white": 0,
        "sepia": 1,
        "night": 2
    };

    var FAMILY = {
        "serif": 0,
        "sans": 1
    };

    var togglePopover = function(e) {
        var $dropdown = $("#font-settings-wrapper .dropdown-menu");

        $dropdown.toggleClass("open");
        e.stopPropagation();
        e.preventDefault();
    };

    var closePopover = function(e) {
        var $dropdown = $("#font-settings-wrapper .dropdown-menu");

        $dropdown.removeClass("open");
    };

    var enlargeFontSize = function(e){
        if (fontState.size < 4){
            fontState.size++;
            fontState.save();
        }
    };

    var reduceFontSize = function(e){
        if (fontState.size > 0){
            fontState.size--;
            fontState.save();
        }
    };

    var changeFontFamily = function(){
        var index = $(this).data("font");

        fontState.family = index;
        fontState.save();
    };

    var changeColorTheme = function(){
        var $book = $(".book");
        var index = $(this).data("theme");

        if (fontState.theme !== 0)
            $book.removeClass("color-theme-"+fontState.theme);

        fontState.theme = index;
        if (fontState.theme !== 0)
            $book.addClass("color-theme-"+fontState.theme);

        fontState.save();
    };

    var update = function() {
        var $book = $(".book");

        $(".font-settings .font-family-list li").removeClass("active");
        $(".font-settings .font-family-list li:nth-child("+(fontState.family+1)+")").addClass("active");

        $book[0].className = $book[0].className.replace(/\bfont-\S+/g, '');
        $book.addClass("font-size-"+fontState.size);
        $book.addClass("font-family-"+fontState.family);

        if(fontState.theme !== 0) {
            $book[0].className = $book[0].className.replace(/\bcolor-theme-\S+/g, '');
            $book.addClass("color-theme-"+fontState.theme);
        }
    };

    var init = function(config) {
        var $toggle, $bookBody, $dropdown, $book;

        //Find DOM elements.
        $book = $(".book");
        $toggle = $(".book-header .toggle-font-settings");
        $dropdown = $("#font-settings-wrapper .dropdown-menu");
        $bookBody = $(".book-body");

        // Instantiate font state object
        fontState = storage.get("fontState", {
            size: config.size || 2,
            family: FAMILY[config.family || "sans"],
            theme: THEMES[config.theme || "white"]
        });
        fontState.save = function(){
            storage.set("fontState",fontState);
            update();
        };

        update();

        //Add event listeners
        $(document).on('click', "#enlarge-font-size", enlargeFontSize);
        $(document).on('click', "#reduce-font-size", reduceFontSize);

        $(document).on('click', "#font-settings-wrapper .font-family-list li", changeFontFamily);
        $(document).on('click', "#font-settings-wrapper .color-theme-list button", changeColorTheme);

        $(document).on('click', ".book-header .toggle-font-settings", togglePopover);
        $(document).on('click', "#font-settings-wrapper .dropdown-menu", function(e){ e.stopPropagation(); });
        $(document).on("click", closePopover);
    };

    return {
        init: init,
        update: update
    }
});