-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
61 lines (57 loc) · 1.8 KB
/
main.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
(function() {
const $ = str => document.querySelector(str);
const $$ = str => document.querySelectorAll(str);
const app = {
init: function() {
fetch("nav.json").then(response=>response.json()).then(data=>{
app.data.nav = data;
console.dir(data);
app.buildNav(data["sidemenu"]);
app.state.loaded = true;
});
},
state: {
loaded: false
},
data: {
store: function(key, obj) {
if (key && obj) {
localStorage.setItem(key, JSON.stringify(obj));
}
},
get: function(key) {
if (key) {
return JSON.parse(localStorage.getItem(key));
}
},
nav: {}
},
buildNav: function(tree) {
let out = `<ul class="nav nav-treeview nav-pills nav-sidebar flex-column nav-child-indent nav-collapse-hide-child" data-widget="treeview" role="menu" data-accordion="false">`;
out += app.makeList(tree, true);
out += `</ul>`;
$("#sidemenu").innerHTML = out;
jQuery("ul").Treeview();
},
makeList: function(arr, noul=false) {
let out = (noul) ? "" : `<ul class="nav nav-treeview">`;
let haschild, toggle = '', arrow = `<i class="right fas fa-angle-left"></i>`;
let mopen = 0;
arr.forEach(item => {
haschild = item.hasOwnProperty("_children");
toggle = (haschild) ? arrow : "";
menuopen = (!mopen && noul && haschild) ? " menu-open" : '';
out += `<li class="nav-item${menuopen}"><a href="${item.link}" class="nav-link"><i class="nav-icon ${item.icon}"></i><p>${item.title}${toggle}</p></a>`;
if (haschild) {
out += app.makeList(item["_children"]);
}
out += "</li>";
mopen = 0;
});
out += (noul) ? "" : "</ul>";
return out;
}
};
window.app = app;
app.init();
})();