/* nomadro 小红书小工具 — 对齐 H5 门户首页,纯离线 SPA */
(function () {
"use strict";
var portal = window.PORTAL_DATA || (typeof PORTAL_DATA !== "undefined" ? PORTAL_DATA : null) || {};
var cfg = portal.config || {};
var nav = portal.nav || window.NAV_DATA || (typeof NAV_DATA !== "undefined" ? NAV_DATA : {}) || {};
var book = portal.book || { title: "小册", chapters: [] };
var community = portal.community || { meetups: [] };
var categories = nav.categories || [];
var state = {
view: "home",
theme: "dark",
activeId: categories[0] ? categories[0].id : "",
drawerOpen: false,
sheetOpen: false,
currentLink: null,
jumping: false,
bookChapter: null,
};
var els = {};
function $(id) {
return document.getElementById(id);
}
function escapeHtml(str) {
return String(str == null ? "" : str)
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """);
}
function applySafeArea() {
try {
var status = 28;
var padTop = Math.max(status + 44, 72);
document.documentElement.style.setProperty("--app-pad-top", padTop + "px");
} catch (e) {
document.documentElement.style.setProperty("--app-pad-top", "80px");
}
}
function setTheme(theme) {
state.theme = theme;
document.documentElement.setAttribute("data-theme", theme);
try {
localStorage.setItem("nomadro-xhs-theme", theme);
} catch (e) {}
var ic = theme === "dark" ? "☀" : "☾";
if (els.homeThemeIcon) els.homeThemeIcon.textContent = ic;
}
function showToast(msg) {
if (!els.toast) return;
els.toast.textContent = msg;
els.toast.classList.add("show");
clearTimeout(showToast._t);
showToast._t = setTimeout(function () {
els.toast.classList.remove("show");
}, 1600);
}
function showView(name) {
state.view = name;
var views = ["home", "nav", "book", "community"];
for (var i = 0; i < views.length; i++) {
var el = $("view-" + views[i]);
if (el) el.classList.toggle("is-active", views[i] === name);
}
window.scrollTo(0, 0);
if (name === "nav") {
renderNav();
}
if (name === "book") {
state.bookChapter = null;
renderBook();
}
if (name === "community") {
renderCommunity();
}
}
/* ----- 首页(对齐 H5) ----- */
function renderHome() {
var brand = cfg.homeBrand || "nomadro";
if (els.homeBrand) els.homeBrand.textContent = brand;
if (els.homeTagline) els.homeTagline.textContent = cfg.homeTagline || "数字游民操作系统";
if (els.homeLead) els.homeLead.textContent = cfg.homeLead || "导航 · 小册 · 社群 · 离线可用";
if (els.homeFoot) els.homeFoot.textContent = brand + " · 离线包";
var mods = cfg.modules || {};
var order = [
{ key: "nav", emoji: "🧭", title: "出海导航", desc: "工具与出海资源" },
{ key: "book", emoji: "📖", title: "数字游民小册", desc: "离线预览章节" },
{ key: "community", emoji: "🌏", title: "旅居社群", desc: "活动与目的地" },
];
var html = "";
for (var i = 0; i < order.length; i++) {
var o = order[i];
var m = mods[o.key] || {};
if (m.enabled === false) continue;
html +=
'';
}
if (!html) {
html = '
模块已关闭(离线配置)
';
}
if (els.homeCards) els.homeCards.innerHTML = html;
}
/* ----- 导航 ----- */
function countLinks(cat) {
var n = 0;
var groups = cat.groups || [];
for (var i = 0; i < groups.length; i++) n += (groups[i].links || []).length;
return n;
}
function findCategory(id) {
for (var i = 0; i < categories.length; i++) {
if (categories[i].id === id) return categories[i];
}
return categories[0] || null;
}
function renderDrawer() {
if (!els.drawerNav) return;
var html = "";
for (var i = 0; i < categories.length; i++) {
var cat = categories[i];
html +=
'";
}
els.drawerNav.innerHTML = html || '
暂无分类数据
';
}
function renderNavContent() {
if (!els.content) return;
if (!categories.length) {
els.content.innerHTML =
'
离线导航数据未打包。请重新 assemble。
';
return;
}
var html = "";
for (var i = 0; i < categories.length; i++) {
var cat = categories[i];
html +=
'
' +
escapeHtml(cat.title) +
'
' +
countLinks(cat) +
"";
var groups = cat.groups || [];
for (var g = 0; g < groups.length; g++) {
var group = groups[g];
html +=
'
' +
escapeHtml(group.title) +
'
';
var links = group.links || [];
for (var l = 0; l < links.length; l++) {
var link = links[l];
html +=
'";
}
html += "
";
}
html += "";
}
els.content.innerHTML = html;
}
function renderNav() {
if (!categories.length) {
console.error("[nomadro-xhs] NAV_DATA.categories empty");
}
var cat = findCategory(state.activeId);
if (els.brandCurrent && cat) els.brandCurrent.textContent = cat.title;
renderDrawer();
renderNavContent();
}
function setDrawer(open) {
state.drawerOpen = open;
if (!els.drawer) return;
els.drawer.classList.toggle("open", open);
els.drawer.setAttribute("aria-hidden", open ? "false" : "true");
}
function setSheet(open) {
state.sheetOpen = open;
if (!els.sheet) return;
els.sheet.classList.toggle("open", open);
els.sheet.setAttribute("aria-hidden", open ? "false" : "true");
}
function findLink(catId, groupId, linkId) {
var cat = findCategory(catId);
if (!cat) return null;
var groups = cat.groups || [];
for (var i = 0; i < groups.length; i++) {
if (groups[i].id !== groupId) continue;
var links = groups[i].links || [];
for (var j = 0; j < links.length; j++) {
if (links[j].id === linkId) return links[j];
}
}
return null;
}
function openLinkSheet(link) {
state.currentLink = link;
if (els.sheetIcon) els.sheetIcon.src = link.icon || "./icons/nomadro.webp";
if (els.sheetTitle) els.sheetTitle.textContent = link.title || "";
if (els.sheetSub) els.sheetSub.textContent = link.subtitle || "";
if (els.sheetUrlBox) els.sheetUrlBox.textContent = link.href || "";
if (els.sheetStatus) els.sheetStatus.textContent = "点下方按钮选中网址,再按住复制";
setSheet(true);
}
function selectUrlText() {
try {
var node = els.sheetUrlBox;
if (!node) return false;
var range = document.createRange();
range.selectNodeContents(node);
var sel = window.getSelection();
if (!sel) return false;
sel.removeAllRanges();
sel.addRange(range);
return true;
} catch (e) {
return false;
}
}
function jumpToCategory(id) {
state.activeId = id;
renderDrawer();
setDrawer(false);
var cat = findCategory(id);
if (els.brandCurrent && cat) els.brandCurrent.textContent = cat.title;
state.jumping = true;
var el = document.getElementById("cat-" + id);
if (el) el.scrollIntoView({ behavior: "smooth", block: "start" });
setTimeout(function () {
state.jumping = false;
}, 700);
}
/* ----- 小册 ----- */
function renderBook() {
if (!els.bookList || !els.bookReader) return;
if (state.bookChapter) {
els.bookList.classList.add("is-hidden");
els.bookReader.classList.remove("is-hidden");
var ch = null;
for (var i = 0; i < (book.chapters || []).length; i++) {
if (book.chapters[i].id === state.bookChapter) ch = book.chapters[i];
}
if (!ch) {
els.bookReader.innerHTML = '
";
var back = $("book-back-list");
if (back) {
back.onclick = function () {
state.bookChapter = null;
renderBook();
};
}
return;
}
els.bookReader.classList.add("is-hidden");
els.bookList.classList.remove("is-hidden");
var html =
'