/* router.js —— hash 路由。 * 约定:#/path?k=v&k2=v2&focus=objectId * #/overview 工作台(我的 / 池子 / 现场看板) * #/pool?priority=S&sort=score 机会卡片池(?opp= 打开卡片抽屉) * #/artifacts 成果 * #/insights?ins=id 归纳(可选打开某条) * #/projects #/project/ 项目 * #/members #/member/ 成员 * #/sources 原材料层 * #/record 记录层 * #/stages #/stage/ 阶段进度 * hash 每次变化都完整重建 URL,保证"复制链接 → 新标签打开 → 状态一致"。 */ (function () { "use strict"; const knownParams = ["q", "priority", "stage", "category", "evidence", "layer", "member", "ins", "art", "src", "ev", "back", "ch", "sort", "focus", "type", "status", "opp"]; function parseHash(hash) { const h = (hash || window.location.hash || "").replace(/^#\/?/, ""); const [pathPart, qs] = h.split("?"); const segs = pathPart.split("/").filter(Boolean).map(decodeURIComponent); const params = {}; new URLSearchParams(qs || "").forEach((v, k) => { params[k] = v; }); return { route: segs[0] || "overview", arg: segs[1] || null, params }; } /* 由当前 route + 参数生成 hash,参数按固定顺序排,空值不写入 */ function buildHash(route, arg, params) { const p = new URLSearchParams(); for (const k of knownParams) { const v = params[k]; if (v != null && v !== "") p.set(k, v); } const s = p.toString(); let path = route; if ((route === "project" || route === "member" || route === "stage" || route === "report") && arg) { path += "/" + encodeURIComponent(arg); } return "#/" + path + (s ? "?" + s : ""); } /* 用 replaceState 更新 hash,不触发页面滚动与额外 hashchange。 * params 是"完整的参数表":调用方先展开当前参数再覆盖,这样切筛选不会把抽屉参数带跑。 */ function write(params, opts) { const cur = parseHash(); const hash = buildHash(cur.route, cur.arg, params); if (hash === window.location.hash) return; try { history.replaceState(null, "", hash); } catch (e) { window.location.hash = hash; /* file:// 等环境对 replaceState 有限制时的退路 */ } if (!opts || !opts.silent) document.dispatchEvent(new CustomEvent("route:changed")); } function go(hash) { if (hash === window.location.hash) return; window.location.hash = hash; } window.KF_ROUTER = { parseHash, buildHash, write, go }; })();