summaryrefslogtreecommitdiffstats
path: root/theme/static/js/main.js
blob: 28dd6912e70b35a1574bd38edb7a4a44a7343703 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* jslint browser:true */
/* global body, options */
"use strict";

document.addEventListener("DOMContentLoaded", function(){

var menu = function (ev) {
    var target = ev.target;
    if (target.tagName.toLowerCase() === "span") {
        target = target.parentNode;
    }
    var id = target.id;
    if (!id) {
        ev.stopPropagation();
        return;
    }
    if (body.classList.contains(id)) {
        body.classList.remove(id);
        body.classList.remove("sidebar-active");
        document.activeElement.blur();
    } else if (body.classList.contains("sidebar-active")) {
        var other = null;
        for (var i = 0; i < body.classList.length; i+=1) {
            if (body.classList.item(i) !== id &&
                    body.classList.item(i) !== "sidebar-active") {
                other = body.classList.item(i);
                break;
            }
        }
        body.classList.remove(other);
        body.classList.add(id);
    } else {
        body.classList.add(id);
        body.classList.add("sidebar-active");
    }
    ev.stopPropagation();
    options.classname = body.className;
    localStorage.setItem("options", JSON.stringify(options));

};

var settings = function(attr, dir, amount) {
    var chapter = document.getElementsByClassName("chapter")[0];
    var cursize = parseInt(window.getComputedStyle(chapter)[attr]);
    chapter.style[attr] = cursize + dir*amount + "px";
};

var contrast = function() {
    var main = document.getElementsByClassName("main")[0];
    var curcolor = window.getComputedStyle(main).color;
    if (curcolor === "rgb(51, 51, 51)") {
        main.style.color = "white";
        main.style.backgroundColor = "#222";
    } else {
        main.style.color = "#333";
        main.style.backgroundColor = "white";
    }
};

var align = function(dir) {
    var chapter = document.getElementsByClassName("chapter")[0];
    chapter.style.textAlign = dir; 
};

var option = function(el, name) {
    return function() {
        options[name] = el.checked;
        localStorage.setItem("options", JSON.stringify(options));
    };
};

document.getElementsByClassName("menu")[0].addEventListener("click", menu);
document.getElementById("font-minus").addEventListener("click", function() {
    settings("fontSize", -1, 2);
});
document.getElementById("font-plus").addEventListener("click", function() {
    settings("fontSize", 1, 2);
});
document.getElementById("line-minus").addEventListener("click", function() {
    settings("lineHeight", -1, 4);
});
document.getElementById("line-plus").addEventListener("click", function() {
    settings("lineHeight", 1, 4);
});
document.getElementById("width-minus").addEventListener("click", function() {
    settings("maxWidth", -1, 50);
});
document.getElementById("width-plus").addEventListener("click", function() {
    settings("maxWidth", 1, 50);
});
document.getElementById("contrast").addEventListener("click", contrast);
document.getElementById("left").addEventListener("click", function() {
    align("left");
});
document.getElementById("justify").addEventListener("click", function() {
    align("justify");
});

var main = document.getElementsByClassName("main")[0];

main.addEventListener("click", function() {
    if (options.click) {
        body.className = "";
        options.classname = body.className;
        localStorage.setItem("options", JSON.stringify(options));
    }
});

window.addEventListener("scroll", function() {
    if (!options.position) {
        return;
    } else {
        options.positions[window.location.pathname] = this.scrollY;
        localStorage.setItem("options", JSON.stringify(options));
    }
});

if(options.position && options.positions[window.location.pathname]) {
    window.scrollTo(0, options.positions[window.location.pathname]);
}

var labels = document.getElementsByClassName("option");
for (var i = 0; i < labels.length; i+=1) {
    var label = labels[i];
    var input = label.getElementsByTagName("input")[0];
    input.checked = options[label.dataset.name];
    label.addEventListener("click", option(input, label.dataset.name));
}

var cur_path = window.location.pathname;
var contents = document.getElementsByClassName("contents")[0];
var chapters = contents.getElementsByTagName("li");
for (i = 0; i < chapters.length; i+=1) {
    var anchor = chapters[i].getElementsByTagName("a")[0];
    if (anchor.pathname === cur_path) {
        chapters[i].classList.add("active");
    }
}

});