-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidebarcontroller.js
78 lines (75 loc) · 1.9 KB
/
sidebarcontroller.js
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
// Copyright 2018-2019 Campbell Crowley. All rights reserved.
// Author: Campbell Crowley ([email protected])
/**
* Controls a sidebar menu.
* @class Sidebar
*/
(function(Sidebar, undefined) {
// Screen size (x/y) in pixels.
const w = window;
const d = document;
const e = d.documentElement;
const g = d.getElementsByTagName('body')[0];
const x = w.innerWidth || e.clientWidth || g.clientWidth;
// let y = w.innerHeight || e.clientHeight || g.clientHeight;
/**
* The minimum width of the screen in pixels at which the sidebar
* automatically
* opens.
* @default
* @constant
* @private
* @type {number}
*/
const minWidthToOpen = 1710;
/**
* Is the sidebar currently open.
* @default
* @private
* @type {boolean}
*/
let isOpen = false;
/**
* Stores the reference to the sidebar Element.
* @private
* @type {Element}
*/
let sidebarDom;
/**
* Initialize Sidebar.
*
* @public
*/
Sidebar.init = function() {
sidebarDom = document.getElementById('sidebar');
if (x <= minWidthToOpen) {
sidebarOptionDoms = document.getElementsByClassName('sidebarOption');
for (let i = 0; i < sidebarOptionDoms.length; i++) {
sidebarOptionDoms[i].addEventListener('click', function() {
Sidebar.toggleOpen(false);
});
}
}
};
/**
* Toggle the sidebar open and closed.
*
* @pulbic
* @param {?boolean|string} [force=undefined] "default" sets to default
* setting,
* a boolean sets it to that value, undefined toggles state.
*/
Sidebar.toggleOpen = function(force) {
if (typeof force === 'undefined') {
force = !isOpen;
} else if (force === 'default') {
if (x > minWidthToOpen) {
force = true;
} else {
force = false;
}
}
isOpen = force;
sidebarDom.classList.toggle('open', isOpen);
};
}(window.Sidebar = window.Sidebar || {}));