From 48269ade6163c34f348632bf225e2c6649b0a9c1 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 15 Apr 2020 22:43:05 +0200 Subject: [PATCH] luci-theme-openwrt: 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 --- .../luci-static/resources/menu-openwrt.js | 133 +++++++++++++++ .../luasrc/view/themes/openwrt.org/footer.htm | 3 + .../luasrc/view/themes/openwrt.org/header.htm | 156 +----------------- 3 files changed, 137 insertions(+), 155 deletions(-) create mode 100644 themes/luci-theme-openwrt/htdocs/luci-static/resources/menu-openwrt.js diff --git a/themes/luci-theme-openwrt/htdocs/luci-static/resources/menu-openwrt.js b/themes/luci-theme-openwrt/htdocs/luci-static/resources/menu-openwrt.js new file mode 100644 index 000000000..ca97838d3 --- /dev/null +++ b/themes/luci-theme-openwrt/htdocs/luci-static/resources/menu-openwrt.js @@ -0,0 +1,133 @@ +'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); + } + }, + + handleMenuExpand: function(ev) { + var a = ev.target, ul1 = a.parentNode.parentNode, ul2 = a.nextElementSibling; + + document.querySelectorAll('ul.mainmenu.l1 > li.active').forEach(function(li) { + if (li !== a.parentNode) + li.classList.remove('active'); + }); + + if (!ul2) + return; + + if (ul2.parentNode.offsetLeft + ul2.offsetWidth <= ul1.offsetLeft + ul1.offsetWidth) + ul2.classList.add('align-left'); + + ul1.classList.add('active'); + a.parentNode.classList.add('active'); + a.blur(); + + ev.preventDefault(); + ev.stopPropagation(); + }, + + renderMainMenu: function(tree, url, level) { + var l = (level || 0) + 1, + ul = E('ul', { 'class': 'mainmenu l%d'.format(l) }), + children = ui.menu.getChildren(tree); + + if (children.length == 0 || l > 2) + return E([]); + + for (var i = 0; i < children.length; i++) { + var isActive = (L.env.dispatchpath[l] == children[i].name), + activeClass = 'mainmenu-item-%s%s'.format(children[i].name, isActive ? ' selected' : ''); + + ul.appendChild(E('li', { 'class': activeClass }, [ + E('a', { + 'href': L.url(url, children[i].name), + 'click': (l == 1) ? this.handleMenuExpand : null, + }, [ _(children[i].title) ]), + this.renderMainMenu(children[i], url + '/' + children[i].name, l) + ])); + } + + if (l == 1) { + var container = document.querySelector('#mainmenu'); + + container.appendChild(ul); + container.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', {}, [ + E('a', { + 'href': L.url(children[i].name), + 'class': isActive ? 'active' : null + }, [ _(children[i].title) ]) + ])); + + if (isActive) + this.renderMainMenu(children[i], children[i].name); + } + + if (ul.children.length > 1) + ul.style.display = ''; + }, + + renderTabMenu: function(tree, url, level) { + var container = document.querySelector('#tabmenu'), + l = (level || 0) + 1, + ul = E('ul', { 'class': 'cbi-tabmenu' }), + children = ui.menu.getChildren(tree), + activeNode = null; + + if (children.length == 0) + return E([]); + + for (var i = 0; i < children.length; i++) { + var isActive = (L.env.dispatchpath[l + 2] == children[i].name), + activeClass = isActive ? ' cbi-tab' : '', + 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]; + } + + container.appendChild(ul); + container.style.display = ''; + + if (activeNode) + container.appendChild(this.renderTabMenu(activeNode, url + '/' + activeNode.name, l)); + + return ul; + } +}); diff --git a/themes/luci-theme-openwrt/luasrc/view/themes/openwrt.org/footer.htm b/themes/luci-theme-openwrt/luasrc/view/themes/openwrt.org/footer.htm index a056dd84f..356b6dac1 100644 --- a/themes/luci-theme-openwrt/luasrc/view/themes/openwrt.org/footer.htm +++ b/themes/luci-theme-openwrt/luasrc/view/themes/openwrt.org/footer.htm @@ -12,5 +12,8 @@ <% local ver = require "luci.version" -%> Powered by <%= ver.luciname %> (<%= ver.luciversion %>)

+ + + diff --git a/themes/luci-theme-openwrt/luasrc/view/themes/openwrt.org/header.htm b/themes/luci-theme-openwrt/luasrc/view/themes/openwrt.org/header.htm index fb7c7f22d..376aee99c 100644 --- a/themes/luci-theme-openwrt/luasrc/view/themes/openwrt.org/header.htm +++ b/themes/luci-theme-openwrt/luasrc/view/themes/openwrt.org/header.htm @@ -36,156 +36,7 @@ <% end -%> - - <%=striptags( (boardinfo.hostname or "?") .. ( (node and node.title) and ' - ' .. translate(node.title) or '')) %> - LuCI "> @@ -203,12 +54,7 @@ <%:Load%>: <%="%.2f" % (loadinfo[1] / 65535.0)%> <%="%.2f" % (loadinfo[2] / 65535.0)%> <%="%.2f" % (loadinfo[3] / 65535.0)%> -
- -
+
-- 2.25.1