'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(``); document.body.appendChild(pop); const input = pop.querySelector('input'), list = pop.querySelector('.td-pop-list'); const all = this.entries(); let sel = 0, shown = []; const draw = () => { const q = input.value.trim().toLowerCase(); shown = !q ? all.filter(e => e.path.split('/').length <= 3).slice(0, 40) : all.filter(e => (e.title + ' ' + e.en + ' ' + e.group + ' ' + e.path).toLowerCase().indexOf(q) > -1).slice(0, 40); sel = Math.min(sel, Math.max(0, shown.length - 1)); list.innerHTML = shown.length ? shown.map((e, i) => `${icon(e.icon)}${esc(e.title)}${esc(e.group)}`).join('') : `
${esc(T('没有找到相关功能', 'Nothing found'))}
`; const on = list.querySelector('.on'); if (on) on.scrollIntoView({ block: 'nearest' }); }; /* closing hands the keyboard back to whatever opened the search */ const close = pop._close = () => { td.leave(pop); if (from && from.focus) from.focus() }; input.addEventListener('input', () => { sel = 0; draw() }); input.addEventListener('keydown', ev => { if (ev.key == 'ArrowDown') { sel = Math.min(sel + 1, shown.length - 1); draw(); ev.preventDefault() } else if (ev.key == 'ArrowUp') { sel = Math.max(sel - 1, 0); draw(); ev.preventDefault() } else if (ev.key == 'Enter' && shown[sel]) { location.href = L.url(shown[sel].path) } else if (ev.key == 'Tab') ev.preventDefault(); // the arrows move through the results; Tab must not wander to the page behind }); list.addEventListener('mousemove', ev => { const a = ev.target.closest('a[data-i]'); if (a && +a.dataset.i != sel) { sel = +a.dataset.i; list.querySelectorAll('a').forEach(x => x.classList.toggle('on', x === a)) } }); pop.addEventListener('click', ev => { if (ev.target === pop) close() }); draw(); input.focus(); }, /* ---------- phone: "More" sheet ---------- */ openSheet() { if (document.querySelector('.td-pop:not(.td-out)')) return; const rest = (this.groups || []).filter(g => g.mob === false || g.id == 'system'); const body = rest.map(g => `
${esc(g.label)}
` + g.list.map(p => `${icon(g.id == 'apps' ? td.appIcon(p.path) : g.icon)}${esc(p.title)}`).join('')).join(''); const pop = td.html(``); document.body.appendChild(pop); pop.addEventListener('click', ev => { const b = ev.target.closest('[data-act]'); if (ev.target === pop) td.leave(pop); else if (b && b.dataset.act == 'search') { pop.remove(); this.openSearch() } else if (b && b.dataset.act == 'mode') this.toggleMode(); }); }, /* ---------- keyboard & dark mode ---------- */ bindKeys() { const kbd = document.querySelector('.td-search kbd'); if (kbd && !/Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent)) kbd.textContent = 'Ctrl K'; document.addEventListener('keydown', ev => { const typing = /^(INPUT|TEXTAREA|SELECT)$/.test((ev.target || {}).tagName || '') || (ev.target || {}).isContentEditable; if ((ev.key == 'k' || ev.key == 'K') && (ev.metaKey || ev.ctrlKey)) { ev.preventDefault(); this.openSearch() } else if (ev.key == '/' && !typing && !document.body.classList.contains('modal-overlay-active')) { ev.preventDefault(); this.openSearch() } else if (ev.key == 'Escape') { // closes the search or the phone sheet, whichever is open const pop = document.querySelector('.td-pop:not(.td-out)'); if (pop) (pop._close || (() => td.leave(pop)))(); } }); }, bindMode() { const btn = document.getElementById('td-mode'); const sync = () => { if (!btn) return; const dark = document.documentElement.getAttribute('data-theme') == 'dark'; btn.innerHTML = icon(dark ? 'sun' : 'moon'); btn.title = dark ? T('切换到浅色', 'Light mode') : T('切换到深色', 'Dark mode'); }; if (btn) btn.addEventListener('click', () => this.toggleMode()); document.addEventListener('td-look', sync); matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { if (document.documentElement.getAttribute('data-mode') == 'auto') td.applyLook({ palette: document.documentElement.getAttribute('data-palette'), mode: 'auto' }); }); sync(); }, toggleMode() { const r = document.documentElement; const mode = r.getAttribute('data-theme') == 'dark' ? 'light' : 'dark'; td.applyLook({ palette: r.getAttribute('data-palette') || 'clay', mode: mode }); td.saveSettings({ mode: mode }).catch(() => {}); } });