Files
2026-11-06 15:54:55 +08:00

60 lines
4.2 KiB
JavaScript

// 顶部导航栏:普通脚本(不是模块),放在 <body> 最前面同步执行。
// 页面一出来导航栏就在,不用等模块脚本和接口;登录状态也按本地保存的信息直接画好,不会从「登录」跳成头像。
// 交互(搜索、菜单、登录弹窗)仍由 layout.js / auth.js 绑定在这个导航栏上。
(function () {
var NAV = [
{ id: 'home', label: '首页', href: '/' },
{ id: 'explore', label: '社区', href: '/explore/' },
{ id: 'studio', label: '工作台', href: '/studio/' },
{ id: 'cad', label: 'CAD', href: '/cad/', desktopOnly: true },
{ id: 'pcb', label: 'PCB', href: '/pcb/', desktopOnly: true },
{ id: 'workspace', label: '我的', href: '/workspace/' },
];
var p = location.pathname;
var logoImage = /^\/cad\/editor(?:\/|$)/.test(p)
? '<img src="/assets/brand/make-cad.svg" alt="make CAD" width="92" height="34">'
: '<img src="/assets/brand/make-web-384.png" srcset="/assets/brand/make-web-256.png 256w, /assets/brand/make-web-384.png 384w, /assets/brand/make-web-512.png 512w, /assets/brand/make-web-768.png 768w" sizes="(max-width: 820px) 96px, 112px" alt="make" width="112" height="42">';
var active = /^\/(explore|project)/.test(p) ? 'explore' : /^\/studio/.test(p) ? 'studio' : /^\/cad/.test(p) ? 'cad' : /^\/pcb/.test(p) ? 'pcb' : /^\/workspace/.test(p) ? 'workspace' : 'home';
var overlay = active === 'studio';
var svg = function (path, size) {
return '<svg class="ic" width="' + size + '" height="' + size + '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round">' + path + '</svg>';
};
var esc = function (s) { return String(s == null ? '' : s).replace(/[&<>"']/g, function (c) { return { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]; }); };
// 本地保存的登录信息(与 js/api.js 的 vm.auth 一致;过期就当未登录)
var user = null;
try {
var a = JSON.parse(localStorage.getItem('vm.auth'));
if (a && a.token && (!a.expiresAt || Date.now() < a.expiresAt)) user = a.user;
} catch (e) { /* 忽略 */ }
var PALETTE = ['#c8ff3e', '#5ea8ff', '#ff7a9a', '#ffb347', '#7cf0c8', '#b79cff', '#ffd166', '#6ee7ff'];
var authHtml = '<button class="btn btn-sm btn-primary" data-login>登录</button>';
if (user) {
var key = user.userCode || user.nickname || '?', h = 0;
for (var i = 0; i < key.length; i++) h = (h * 31 + key.charCodeAt(i)) >>> 0;
var name = user.nickname || '已注销用户';
authHtml = '<button class="nav-user" data-user-menu title="' + esc(user.email || '') + '"><span class="avatar" style="--c:' + PALETTE[h % PALETTE.length] + ';width:28px;height:28px;font-size:12px" title="' + esc(name) + '">' + esc(name.slice(0, 1).toUpperCase()) + '</span><span>' + esc(name) + '</span></button>';
}
var links = NAV.map(function (n) {
var target = n.newTab ? ' target="_blank" rel="noopener noreferrer"' : '';
return '<a href="' + n.href + '"' + target + (n.desktopOnly ? ' data-desktop-only' : '') + ' class="' + (n.id === active ? 'active' : '') + '">' + n.label + '</a>';
}).join('');
var nav = document.createElement('header');
nav.className = 'nav' + (overlay ? ' overlay' : '');
nav.dataset.active = active;
nav.innerHTML =
'<div class="nav-inner">' +
'<a class="nav-logo" href="/" aria-label="make 首页">' + logoImage + '</a>' +
'<nav class="nav-links">' + links + '</nav>' +
'<div class="nav-actions">' +
'<button class="nav-search" data-palette>' + svg('<circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/>', 15) + '<span>搜索</span><kbd>⌘K</kbd></button>' +
'<button class="btn btn-sm btn-ghost hide-sm" data-publish>' + svg('<path d="M12 16V4M6 10l6-6 6 6M4 20h16"/>', 14) + '发布项目</button>' +
'<span class="hide-sm" data-auth>' + authHtml + '</span>' +
'<button class="icon-btn nav-menu" aria-label="菜单">' + svg('<path d="M4 7h16M4 12h16M4 17h16"/>', 20) + '</button>' +
'</div>' +
'</div>' +
'<div class="sheet">' + links + '<div class="sheet-actions"><button class="btn" data-publish>发布项目</button><span data-auth>' + authHtml + '</span></div></div>';
document.body.prepend(nav);
})();