luci-theme-bootstrap: render menu on client side
[project/luci.git] / themes / luci-theme-bootstrap / luasrc / view / themes / bootstrap / json-menu.htm
1 <script type="text/javascript">
2 (function() {
3 function get_children(node) {
4 var children = [];
5
6 for (var k in node.children) {
7 if (!node.children.hasOwnProperty(k))
8 continue;
9
10 if (!node.children[k].satisfied)
11 continue;
12
13 if (!node.children[k].hasOwnProperty('title'))
14 continue;
15
16 children.push(Object.assign(node.children[k], { name: k }));
17 }
18
19 return children.sort(function(a, b) {
20 return ((a.order || 1000) - (b.order || 1000));
21 });
22 }
23
24 function render_tabmenu(tree, url, level) {
25 var container = document.querySelector('#tabmenu'),
26 ul = E('ul', { 'class': 'tabs' }),
27 children = get_children(tree),
28 activeNode = null;
29
30 for (var i = 0; i < children.length; i++) {
31 var isActive = (L.env.dispatchpath[3 + (level || 0)] == children[i].name),
32 activeClass = isActive ? ' active' : '',
33 className = 'tabmenu-item-%s %s'.format(children[i].name, activeClass);
34
35 ul.appendChild(E('li', { 'class': className }, [
36 E('a', { 'href': L.url(url, children[i].name) }, [ _(children[i].title) ] )]));
37
38 if (isActive)
39 activeNode = children[i];
40 }
41
42 if (ul.children.length == 0)
43 return E([]);
44
45 container.appendChild(ul);
46 container.style.display = '';
47
48 if (activeNode)
49 render_tabmenu(activeNode, url + '/' + activeNode.name, (level || 0) + 1);
50
51 return ul;
52 }
53
54 function render_mainmenu(tree, url, level) {
55 var ul = level ? E('ul', { 'class': 'dropdown-menu' }) : document.querySelector('#topmenu'),
56 children = get_children(tree);
57
58 if (children.length == 0 || level > 1)
59 return E([]);
60
61 for (var i = 0; i < children.length; i++) {
62 var submenu = render_mainmenu(children[i], url + '/' + children[i].name, (level || 0) + 1),
63 subclass = (!level && submenu.firstElementChild) ? 'dropdown' : null,
64 linkclass = (!level && submenu.firstElementChild) ? 'menu' : null,
65 linkurl = submenu.firstElementChild ? '#' : L.url(url, children[i].name);
66
67 var li = E('li', { 'class': subclass }, [
68 E('a', { 'class': linkclass, 'href': linkurl }, [ _(children[i].title) ]),
69 submenu
70 ]);
71
72 ul.appendChild(li);
73 }
74
75 ul.style.display = '';
76
77 return ul;
78 }
79
80 function render_modemenu(tree) {
81 var ul = document.querySelector('#modemenu'),
82 children = get_children(tree);
83
84 for (var i = 0; i < children.length; i++) {
85 var isActive = (L.env.requestpath.length ? children[i].name == L.env.requestpath[0] : i == 0);
86
87 ul.appendChild(E('li', { 'class': isActive ? 'active' : null }, [
88 E('a', { 'href': L.url(children[i].name) }, [ _(children[i].title) ]),
89 ' ',
90 E('span', { 'class': 'divider' }, [ '|' ])
91 ]));
92
93 if (isActive)
94 render_mainmenu(children[i], children[i].name);
95 }
96
97 if (ul.children.length > 1)
98 ul.style.display = '';
99 }
100
101 document.addEventListener('luci-loaded', function(ev) {
102 var tree = <%= luci.http.write_json(luci.dispatcher.context.authsession and luci.dispatcher.menu_json() or {}) %>,
103 node = tree,
104 url = '';
105
106 render_modemenu(tree);
107
108 if (L.env.dispatchpath.length >= 3) {
109 for (var i = 0; i < 3 && node; i++) {
110 node = node.children[L.env.dispatchpath[i]];
111 url = url + (url ? '/' : '') + L.env.dispatchpath[i];
112 }
113
114 if (node)
115 render_tabmenu(node, url);
116 }
117 });
118 })();
119 </script>