'use strict'; 'require baseclass'; 'require ui'; 'require poll'; 'require 7dollar.common as td'; 'require 7dollar.tidy as tidy'; /* * Navigation for the 7Dollar theme. * Native LuCI pages are regrouped into seven plain-language sections; * anything unknown falls back to "Apps" so third-party plugins always show up. */ const T = td.T, icon = td.icon, esc = td.esc; const mqPhone = window.matchMedia('(max-width:760px)'); return baseclass.extend({ __init__() { td.loadMenu().then(L.bind(this.render, this)); this.bindKeys(); this.bindMode(); this.bindVisibility(); }, /* Every LuCI page refreshes through one shared poll loop; stop it while the tab is hidden so a * forgotten tab costs the router nothing. Coming back restarts it, which refreshes at once. */ bindVisibility() { let paused = false; document.addEventListener('visibilitychange', () => { if (document.hidden) { if (poll.active()) { poll.stop(); paused = true; } } else if (paused) { paused = false; poll.start(); } }); }, render(tree) { const pages = td.flatten(tree); const groups = td.grouped(pages); const here = L.env.dispatchpath.join('/'); let current = null; pages.forEach(p => { if ((here == p.path || here.indexOf(p.path + '/') == 0) && (!current || p.path.length > current.path.length)) current = p; }); this.pages = pages; this.groups = groups; this.current = current; this.renderNav(groups, current); this.renderPills(groups, current); if (current) this.renderTabs(current.node, current.path, current.path.split('/').length); const btn = document.getElementById('td-search'); if (btn) btn.addEventListener('click', () => this.openSearch()); }, href(group) { const first = group.list[0]; return L.url(first.path); }, renderNav(groups, current) { const nav = document.getElementById('td-nav'); if (!nav) return; nav.innerHTML = groups.map((g, i) => { const on = current && current.group == g.id; const short = g.short ? `${esc(g.label)}${esc(g.short)}` : `${esc(g.label)}`; return `${icon(g.icon)}${short}`; }).join(''); const sys = nav.querySelector('[data-g="system"]'); if (sys) sys.addEventListener('click', ev => { if (!mqPhone.matches) return; ev.preventDefault(); this.openSheet(); }); }, renderPills(groups, current) { const box = document.getElementById('td-pills'); if (!box || !current) return; const g = groups.find(x => x.id == current.group); let items = []; if (g.id == 'apps') { if (current.path == 'admin/apps') return; const all = g.list.find(p => p.path == 'admin/apps'); if (all) box.innerHTML = `${icon('chev')}${esc(all.title)}`; box.innerHTML += `${esc(current.title)}`; return; } items = g.list; if (items.length < 2) return; /* everyday pages first; the rest wait behind "More" unless one of them is open */ const pill = (p, cls) => `${esc(p.title)}`; const rest = items.filter(p => g.pages.indexOf(p.path) < 0); const fold = rest.length > 1 && rest.indexOf(current) < 0; box.innerHTML = (fold ? items.filter(p => rest.indexOf(p) < 0) : items).map(p => pill(p)).join('') + (fold ? `${esc(T('更多', 'More'))}${icon('chev')}` : ''); if (fold) box.lastChild.addEventListener('click', ev => { ev.preventDefault(); ev.currentTarget.outerHTML = rest.map(p => pill(p, 'td-in')).join(''); edge(); }); const on = box.querySelector('.on'); if (on && on.scrollIntoView) box.scrollLeft = Math.max(0, on.offsetLeft - 40); const edge = () => { box.classList.toggle('scrolls', box.scrollWidth > box.clientWidth + 2); box.classList.toggle('end', box.scrollLeft + box.clientWidth >= box.scrollWidth - 2); }; box.addEventListener('scroll', edge, { passive: true }); window.addEventListener('resize', edge); edge(); }, renderTabs(node, url, level) { const container = document.getElementById('tabmenu'); const children = ui.menu.getChildren(node); if (!container || !children.length) return; const ul = E('ul', { 'class': 'tabs' }); let active = null; children.forEach(child => { const isActive = (L.env.dispatchpath[level] == child.name); ul.appendChild(E('li', { 'class': 'tabmenu-item-%s%s'.format(child.name, isActive ? ' active' : '') }, [ E('a', { 'href': L.url(url, child.name) }, [ _(child.title) ]) ])); if (isActive) active = child; }); container.appendChild(ul); container.style.display = ''; if (active) this.renderTabs(td.realNode(node, active), url + '/' + active.name, level + 1); }, /* ---------- search (⌘K) ---------- */ entries() { if (this._entries) return this._entries; const out = []; (this.groups || []).forEach(g => g.list.forEach(p => { out.push({ path: p.path, title: p.title, group: g.label, icon: g.icon, en: p.node.title || '' }); ui.menu.getChildren(p.node).forEach(c => out.push({ path: p.path + '/' + c.name, title: p.title + ' · ' + _(c.title), group: g.label, icon: g.icon, en: c.title || '' })); })); return (this._entries = out); }, openSearch() { if (document.querySelector('.td-pop:not(.td-out)')) return; const from = document.activeElement; const pop = td.html(`