From 1ac8df2614336b34109375e62f201cf6bac46734 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 15 Apr 2020 22:46:25 +0200 Subject: [PATCH] luci-theme-bootstrap: rework menu rendering Utilize the LuCI.ui.menu class to load, traverse and cache the menu tree in the local session store. Signed-off-by: Jo-Philipp Wich --- .../htdocs/luci-static/bootstrap/cascade.css | 5 +- .../luci-static/resources/menu-bootstrap.js | 118 +++++++++++++++ .../luasrc/view/themes/bootstrap/footer.htm | 1 + .../luasrc/view/themes/bootstrap/header.htm | 10 +- .../view/themes/bootstrap/json-menu.htm | 134 ------------------ 5 files changed, 121 insertions(+), 147 deletions(-) create mode 100644 themes/luci-theme-bootstrap/htdocs/luci-static/resources/menu-bootstrap.js delete mode 100644 themes/luci-theme-bootstrap/luasrc/view/themes/bootstrap/json-menu.htm diff --git a/themes/luci-theme-bootstrap/htdocs/luci-static/bootstrap/cascade.css b/themes/luci-theme-bootstrap/htdocs/luci-static/bootstrap/cascade.css index 4387e9588..63eb51cce 100644 --- a/themes/luci-theme-bootstrap/htdocs/luci-static/bootstrap/cascade.css +++ b/themes/luci-theme-bootstrap/htdocs/luci-static/bootstrap/cascade.css @@ -1787,6 +1787,7 @@ header [data-indicator] { background-color: #bfbfbf; border-radius: 3px; text-shadow: none; + margin-left: .4em; } header [data-indicator][data-clickable] { @@ -1822,10 +1823,6 @@ header [data-indicator][data-style="active"] { /* LuCI specific items */ .hidden { display: none } -#xhr_poll_status { - cursor: pointer; -} - form.inline { display: inline; margin-bottom: 0; } header .pull-right { padding-top: 8px; } diff --git a/themes/luci-theme-bootstrap/htdocs/luci-static/resources/menu-bootstrap.js b/themes/luci-theme-bootstrap/htdocs/luci-static/resources/menu-bootstrap.js new file mode 100644 index 000000000..5400276b0 --- /dev/null +++ b/themes/luci-theme-bootstrap/htdocs/luci-static/resources/menu-bootstrap.js @@ -0,0 +1,118 @@ +'use strict'; +'require baseclass'; +'require ui'; + +return baseclass.extend({ + __init__: function() { + ui.menu.load().then(L.bind(this.render, this)); + }, + + render: function(tree) { + var node = tree, + url = ''; + + this.renderModeMenu(tree); + + if (L.env.dispatchpath.length >= 3) { + for (var i = 0; i < 3 && node; i++) { + node = node.children[L.env.dispatchpath[i]]; + url = url + (url ? '/' : '') + L.env.dispatchpath[i]; + } + + if (node) + this.renderTabMenu(node, url); + } + + document.addEventListener('poll-start', this.handleBodyMargin); + document.addEventListener('poll-stop', this.handleBodyMargin); + document.addEventListener('uci-new-changes', this.handleBodyMargin); + document.addEventListener('uci-clear-changes', this.handleBodyMargin); + window.addEventListener('resize', this.handleBodyMargin); + + this.handleBodyMargin(); + }, + + renderTabMenu: function(tree, url, level) { + var container = document.querySelector('#tabmenu'), + ul = E('ul', { 'class': 'tabs' }), + children = ui.menu.getChildren(tree), + activeNode = null; + + for (var i = 0; i < children.length; i++) { + var isActive = (L.env.dispatchpath[3 + (level || 0)] == children[i].name), + activeClass = isActive ? ' active' : '', + className = 'tabmenu-item-%s %s'.format(children[i].name, activeClass); + + ul.appendChild(E('li', { 'class': className }, [ + E('a', { 'href': L.url(url, children[i].name) }, [ _(children[i].title) ] )])); + + if (isActive) + activeNode = children[i]; + } + + if (ul.children.length == 0) + return E([]); + + container.appendChild(ul); + container.style.display = ''; + + if (activeNode) + this.renderTabMenu(activeNode, url + '/' + activeNode.name, (level || 0) + 1); + + return ul; + }, + + renderMainMenu: function(tree, url, level) { + var ul = level ? E('ul', { 'class': 'dropdown-menu' }) : document.querySelector('#topmenu'), + children = ui.menu.getChildren(tree); + + if (children.length == 0 || level > 1) + return E([]); + + for (var i = 0; i < children.length; i++) { + var submenu = this.renderMainMenu(children[i], url + '/' + children[i].name, (level || 0) + 1), + subclass = (!level && submenu.firstElementChild) ? 'dropdown' : null, + linkclass = (!level && submenu.firstElementChild) ? 'menu' : null, + linkurl = submenu.firstElementChild ? '#' : L.url(url, children[i].name); + + var li = E('li', { 'class': subclass }, [ + E('a', { 'class': linkclass, 'href': linkurl }, [ _(children[i].title) ]), + submenu + ]); + + ul.appendChild(li); + } + + ul.style.display = ''; + + return ul; + }, + + renderModeMenu: function(tree) { + var ul = document.querySelector('#modemenu'), + children = ui.menu.getChildren(tree); + + for (var i = 0; i < children.length; i++) { + var isActive = (L.env.requestpath.length ? children[i].name == L.env.requestpath[0] : i == 0); + + ul.appendChild(E('li', { 'class': isActive ? 'active' : null }, [ + E('a', { 'href': L.url(children[i].name) }, [ _(children[i].title) ]), + ' ', + E('span', { 'class': 'divider' }, [ '|' ]) + ])); + + if (isActive) + this.renderMainMenu(children[i], children[i].name); + } + + if (ul.children.length > 1) + ul.style.display = ''; + }, + + handleBodyMargin: function(ev) { + var body = document.querySelector('body'), + head = document.querySelector('header'); + + body.style.marginTop = head.offsetHeight + 'px'; + } +}); diff --git a/themes/luci-theme-bootstrap/luasrc/view/themes/bootstrap/footer.htm b/themes/luci-theme-bootstrap/luasrc/view/themes/bootstrap/footer.htm index ec6895f06..d0c395bf3 100644 --- a/themes/luci-theme-bootstrap/luasrc/view/themes/bootstrap/footer.htm +++ b/themes/luci-theme-bootstrap/luasrc/view/themes/bootstrap/footer.htm @@ -12,6 +12,7 @@ + diff --git a/themes/luci-theme-bootstrap/luasrc/view/themes/bootstrap/header.htm b/themes/luci-theme-bootstrap/luasrc/view/themes/bootstrap/header.htm index 39415154a..89aa33815 100644 --- a/themes/luci-theme-bootstrap/luasrc/view/themes/bootstrap/header.htm +++ b/themes/luci-theme-bootstrap/luasrc/view/themes/bootstrap/header.htm @@ -35,9 +35,6 @@ <% end -%> - - - <% include("themes/bootstrap/json-menu") %> "> @@ -46,12 +43,7 @@
<%=boardinfo.hostname or "?"%> -
- -
+
diff --git a/themes/luci-theme-bootstrap/luasrc/view/themes/bootstrap/json-menu.htm b/themes/luci-theme-bootstrap/luasrc/view/themes/bootstrap/json-menu.htm deleted file mode 100644 index ea11cf226..000000000 --- a/themes/luci-theme-bootstrap/luasrc/view/themes/bootstrap/json-menu.htm +++ /dev/null @@ -1,134 +0,0 @@ - -- 2.25.1