feat(n7web): add pet settings and day night themes
This commit is contained in:
@@ -761,6 +761,11 @@
|
||||
<details class="v2-gscu-result" id="extensionGscuTcpResultDetails"><summary>JSON 调试结果</summary><pre id="extensionGscuTcpResult">等待操作</pre></details>
|
||||
</section>
|
||||
</dialog>
|
||||
<dialog class="v2-extension-dialog v2-pet-settings-dialog" id="extensionPetSettingsDialog" aria-labelledby="extensionPetSettingsTitle" aria-describedby="extensionPetSettingsDescription">
|
||||
<header><div><h2 id="extensionPetSettingsTitle">宠物设置</h2><p id="extensionPetSettingsDescription">添加或编辑宠物资料;保存后可在 NFC 设备授权中绑定卡片。</p></div><button type="button" data-close-extension-dialog="extensionPetSettingsDialog" data-dialog-close aria-label="关闭宠物设置"><i data-lucide="x"></i></button></header>
|
||||
<div class="v2-pet-settings-body" id="extensionPetSettingsBody"></div>
|
||||
<footer class="v2-pet-settings-footnote"><i data-lucide="database"></i><span>宠物资料由后端永久保存,只有在此处手动删除才会移除。</span></footer>
|
||||
</dialog>
|
||||
<div class="v2-extension-toast-stack" id="extensionToastStack" aria-live="polite"></div>`;
|
||||
document.body.append(layer);
|
||||
}
|
||||
@@ -2941,6 +2946,7 @@
|
||||
|
||||
function bindEvents() {
|
||||
bindGscuTcpDialogEvents();
|
||||
bindPetSettingsDialogEvents();
|
||||
$$('[data-extension-mode]').forEach((button) => {
|
||||
button.addEventListener("click", () => setMode(button.dataset.extensionMode));
|
||||
});
|
||||
@@ -3396,7 +3402,10 @@
|
||||
}
|
||||
if (render) {
|
||||
renderNfcDialog();
|
||||
if (cardsChanged) renderUnifiedExtensionView?.();
|
||||
if (cardsChanged) {
|
||||
renderPetSettingsDialog();
|
||||
renderUnifiedExtensionView?.();
|
||||
}
|
||||
else updatePetPublisherIndicators();
|
||||
}
|
||||
return true;
|
||||
@@ -3427,7 +3436,7 @@
|
||||
applyPetCardSnapshot(response);
|
||||
showToast("已添加宠物卡片,请编辑并保存后再绑定 NFC。", "success");
|
||||
window.setTimeout(() => {
|
||||
const card = $(`[data-pet-card-id="${selectedPetCardId}"]`);
|
||||
const card = $(`[data-pet-card-id="${selectedPetCardId}"]`, $("#extensionPetSettingsDialog"));
|
||||
card?.scrollIntoView({ behavior: "smooth", block: "nearest" });
|
||||
$('[data-pet-field="pet_name"]', card)?.focus();
|
||||
}, 0);
|
||||
@@ -3482,7 +3491,7 @@
|
||||
if (!petId) return;
|
||||
const petCard = petCards().find((card) => card.id === petId);
|
||||
if (!petCardCanBind(petCard)) {
|
||||
showToast("请先在右侧宠物卡片中完成编辑并保存。", "error");
|
||||
showToast("请先在“宠物设置”中完成编辑并保存。", "error");
|
||||
return;
|
||||
}
|
||||
const button = $("[data-nfc-bind-pet]");
|
||||
@@ -3531,11 +3540,11 @@
|
||||
</section>`;
|
||||
}
|
||||
if (!petCards().length) {
|
||||
return `<div class="v2-nfc-no-devices v2-nfc-no-pets"><i data-lucide="paw-print"></i><div><strong>暂无宠物卡片</strong><span>请先返回右侧“宠物卡片”区域添加、编辑并保存卡片。</span></div></div>`;
|
||||
return `<div class="v2-nfc-no-devices v2-nfc-no-pets"><i data-lucide="paw-print"></i><div><strong>暂无宠物卡片</strong><span>请先打开“宠物设置”添加并保存宠物资料。</span></div><button type="button" data-open-pet-settings>打开宠物设置</button></div>`;
|
||||
}
|
||||
const bindableCards = petCards().filter(petCardCanBind);
|
||||
if (!bindableCards.length) {
|
||||
return `<div class="v2-nfc-no-devices v2-nfc-no-pets"><i data-lucide="save"></i><div><strong>暂无可绑定的已保存卡片</strong><span>请先在右侧完成宠物名与动作编辑并点击保存,再打开此弹窗绑定。</span></div></div>`;
|
||||
return `<div class="v2-nfc-no-devices v2-nfc-no-pets"><i data-lucide="save"></i><div><strong>暂无可绑定的已保存卡片</strong><span>请先在“宠物设置”中完成宠物名与动作编辑并保存。</span></div><button type="button" data-open-pet-settings>打开宠物设置</button></div>`;
|
||||
}
|
||||
const selected = bindableCards.some((card) => card.id === selectedPetCardId)
|
||||
? selectedPetCardId
|
||||
@@ -3543,11 +3552,11 @@
|
||||
return `<div class="v2-nfc-existing v2-nfc-pet-existing"><span>绑定宠物卡片</span><div><select id="extensionNfcExistingPet" data-nfc-pet-select>${bindableCards.map((card) => `<option value="${escapeHtml(card.id)}" ${card.id === selected ? "selected" : ""}>${escapeHtml(card.pet_name)} — ${escapeHtml(card.action)}${card.nfc_id ? ` — 当前已绑定 ${escapeHtml(card.nfc_id)}` : ""}</option>`).join("")}</select><button class="v2-extension-primary" type="button" data-nfc-bind-pet>关联宠物卡片</button></div><small>卡片 ID 由当前读卡器中的 NFC 自动写入,不能手动编辑;同一 NFC 只绑定一张宠物卡片。</small></div>`;
|
||||
}
|
||||
|
||||
function renderPetCardRegion() {
|
||||
function renderPetCardEditor() {
|
||||
const publisher = petCardSnapshot.publisher || {};
|
||||
const cards = petCards();
|
||||
return `<section class="v2-pet-card-region" aria-labelledby="v2PetCardTitle">
|
||||
<header><div><span><i data-lucide="paw-print"></i></span><h4 id="v2PetCardTitle">宠物卡片</h4></div><button type="button" data-pet-add><i data-lucide="plus"></i><span>添加宠物</span></button></header>
|
||||
return `<section class="v2-pet-card-region" aria-labelledby="v2PetCardEditorTitle">
|
||||
<header><div><span><i data-lucide="paw-print"></i></span><h4 id="v2PetCardEditorTitle">宠物卡片</h4></div><button type="button" data-pet-add><i data-lucide="plus" data-icon="inline-start"></i><span>添加宠物</span></button></header>
|
||||
<div class="v2-pet-card-list">
|
||||
${cards.length ? cards.map((card) => {
|
||||
const draft = petCardDrafts.get(card.id) || card;
|
||||
@@ -3569,19 +3578,82 @@
|
||||
</section>`;
|
||||
}
|
||||
|
||||
function renderPetSettingsDialog() {
|
||||
const body = $("#extensionPetSettingsBody");
|
||||
if (!body) return;
|
||||
body.innerHTML = renderPetCardEditor();
|
||||
window.lucide?.createIcons({ attrs: { "aria-hidden": "true" } });
|
||||
}
|
||||
|
||||
function openPetSettingsDialog() {
|
||||
renderPetSettingsDialog();
|
||||
openDialog("extensionPetSettingsDialog");
|
||||
}
|
||||
|
||||
function bindPetSettingsDialogEvents() {
|
||||
const dialog = $("#extensionPetSettingsDialog");
|
||||
if (!dialog || dialog.dataset.petSettingsBound === "true") return;
|
||||
dialog.dataset.petSettingsBound = "true";
|
||||
dialog.addEventListener("click", (event) => {
|
||||
if (event.target.closest('[data-close-extension-dialog="extensionPetSettingsDialog"]')) {
|
||||
closeDialog("extensionPetSettingsDialog");
|
||||
return;
|
||||
}
|
||||
const addPet = event.target.closest("[data-pet-add]");
|
||||
const article = event.target.closest("[data-pet-card-id]");
|
||||
const savePet = event.target.closest("[data-pet-save]");
|
||||
const deletePet = event.target.closest("[data-pet-delete]");
|
||||
if (addPet) void createPetCard(addPet);
|
||||
else if (savePet) void savePetCard(article, savePet);
|
||||
else if (deletePet) void deletePetCard(article, deletePet);
|
||||
});
|
||||
dialog.addEventListener("input", (event) => {
|
||||
const field = event.target.closest("[data-pet-field]");
|
||||
const article = field?.closest("[data-pet-card-id]");
|
||||
if (!field || !article) return;
|
||||
const current = petCardDrafts.get(article.dataset.petCardId)
|
||||
|| petCards().find((card) => card.id === article.dataset.petCardId)
|
||||
|| {};
|
||||
petCardDrafts.set(article.dataset.petCardId, {
|
||||
...current,
|
||||
[field.dataset.petField]: field.value,
|
||||
});
|
||||
const title = $("header strong", article);
|
||||
if (field.dataset.petField === "pet_name" && title) {
|
||||
title.textContent = field.value.trim() || "未命名宠物";
|
||||
}
|
||||
updatePetPublisherIndicators();
|
||||
});
|
||||
}
|
||||
|
||||
function renderDetectedPetCard(card) {
|
||||
if (!card) return "";
|
||||
const status = petCardStatus(card);
|
||||
const publisher = petCardSnapshot.publisher || {};
|
||||
const activeError = publisher.active_pet_id === card.id
|
||||
? String(publisher.last_error || "")
|
||||
: "";
|
||||
return `<article class="v2-pet-presence-card" data-detected-pet-id="${escapeHtml(card.id)}" data-pet-state="${escapeHtml(status.state)}" aria-label="当前识别宠物 ${escapeHtml(card.pet_name)}">
|
||||
<header><span><i data-lucide="paw-print"></i></span><div><small>当前识别宠物</small><strong>${escapeHtml(card.pet_name)}</strong></div><b data-pet-publish-state="${escapeHtml(card.id)}" data-state="${escapeHtml(status.state)}" title="${escapeHtml(activeError || status.label)}">${escapeHtml(status.label)}</b></header>
|
||||
<dl><div><dt>宠物动作</dt><dd>${escapeHtml(card.action)}</dd></div><div><dt>卡片 ID</dt><dd><code>${escapeHtml(card.nfc_id)}</code></dd></div></dl>
|
||||
<footer><span><i data-lucide="radio-tower"></i>${escapeHtml(publisher.topic || "n7/pet")}</span><strong>3 帧 / 1 秒</strong></footer>
|
||||
</article>`;
|
||||
}
|
||||
|
||||
function updatePetPublisherIndicators() {
|
||||
const publisher = petCardSnapshot.publisher || {};
|
||||
petCards().forEach((card) => {
|
||||
const status = petCardStatus(card);
|
||||
const badge = $(`[data-pet-publish-state="${card.id}"]`);
|
||||
if (badge) {
|
||||
$$(`[data-pet-publish-state="${card.id}"]`).forEach((badge) => {
|
||||
badge.dataset.state = status.state;
|
||||
badge.textContent = status.label;
|
||||
badge.title = publisher.active_pet_id === card.id && publisher.last_error
|
||||
? String(publisher.last_error)
|
||||
: status.label;
|
||||
}
|
||||
const article = $(`[data-pet-card-id="${card.id}"]`);
|
||||
if (article) article.dataset.petState = status.state;
|
||||
});
|
||||
$$(`[data-pet-card-id="${card.id}"], [data-detected-pet-id="${card.id}"]`).forEach((article) => {
|
||||
article.dataset.petState = status.state;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3821,44 +3893,25 @@
|
||||
|
||||
function bindUnifiedNfcEvents(mount) {
|
||||
mount.addEventListener("click", (event) => {
|
||||
const addPet = event.target.closest("[data-pet-add]");
|
||||
const petArticle = event.target.closest("[data-pet-card-id]");
|
||||
const savePet = event.target.closest("[data-pet-save]");
|
||||
const deletePet = event.target.closest("[data-pet-delete]");
|
||||
if (addPet) void createPetCard(addPet);
|
||||
else if (savePet) void savePetCard(petArticle, savePet);
|
||||
else if (deletePet) void deletePetCard(petArticle, deletePet);
|
||||
if (event.target.closest("[data-open-pet-settings]")) openPetSettingsDialog();
|
||||
else if (event.target.closest("[data-open-nfc-binding]")) openNfcDialog();
|
||||
else if (event.target.closest("[data-open-gscu-tcp]")) openGscuTcpDialog();
|
||||
});
|
||||
mount.addEventListener("input", (event) => {
|
||||
const field = event.target.closest("[data-pet-field]");
|
||||
const article = field?.closest("[data-pet-card-id]");
|
||||
if (!field || !article) return;
|
||||
const current = petCardDrafts.get(article.dataset.petCardId)
|
||||
|| petCards().find((card) => card.id === article.dataset.petCardId)
|
||||
|| {};
|
||||
petCardDrafts.set(article.dataset.petCardId, {
|
||||
...current,
|
||||
[field.dataset.petField]: field.value,
|
||||
});
|
||||
const title = $("header strong", article);
|
||||
if (field.dataset.petField === "pet_name" && title) {
|
||||
title.textContent = field.value.trim() || "未命名宠物";
|
||||
}
|
||||
updatePetPublisherIndicators();
|
||||
});
|
||||
bindGscuTcpDialogEvents();
|
||||
bindPetSettingsDialogEvents();
|
||||
const dialog = $("#extensionNfcDialog");
|
||||
dialog.addEventListener("click", (event) => {
|
||||
if (event.target.closest("[data-open-pet-settings]")) {
|
||||
closeDialog("extensionNfcDialog");
|
||||
openPetSettingsDialog();
|
||||
return;
|
||||
}
|
||||
if (event.target.closest("[data-close-nfc-dialog]")) closeDialog("extensionNfcDialog");
|
||||
if (event.target.closest("[data-nfc-bind-existing]")) bindCurrentNfcToExisting();
|
||||
if (event.target.closest("[data-nfc-unbind]")) unbindCurrentNfc();
|
||||
if (event.target.closest("[data-nfc-bind-pet]")) void bindCurrentNfcToPet();
|
||||
const unbindPet = event.target.closest("[data-nfc-unbind-pet]");
|
||||
if (unbindPet) void unbindPetCard(unbindPet.dataset.nfcUnbindPet);
|
||||
const addPet = event.target.closest("[data-pet-add]");
|
||||
if (addPet) void createPetCard(addPet);
|
||||
});
|
||||
dialog.addEventListener("change", (event) => {
|
||||
const petSelect = event.target.closest("[data-nfc-pet-select]");
|
||||
@@ -3931,10 +3984,6 @@
|
||||
}
|
||||
}
|
||||
if (health.pet_cards) applyPetCardSnapshot(health.pet_cards, { render: false });
|
||||
if (document.activeElement?.closest?.("#vehicleExtensionSync [data-pet-field]")) {
|
||||
updatePetPublisherIndicators();
|
||||
return;
|
||||
}
|
||||
const physicalConnected = health.transport !== "simulation"
|
||||
&& Boolean(health.connected || health.peer_connected);
|
||||
const listening = Boolean(health.listening);
|
||||
@@ -4028,10 +4077,10 @@
|
||||
? `${nfcCard.uid} · 点击关联设备 ID`
|
||||
: nfcReaderNote(nfcReader);
|
||||
mount.dataset.hasDevices = String(devices.length > 0);
|
||||
mount.dataset.hasPets = String(petCards().length > 0);
|
||||
mount.dataset.hasPets = String(Boolean(nfcPet));
|
||||
mount.innerHTML = `
|
||||
<header class="v2-extension-sync-heading">
|
||||
<div><h3>外设与扩展能力</h3><button class="v2-extension-nfc-launch" type="button" data-open-nfc-binding><i data-lucide="scan-line"></i><span>NFC 设备绑定</span></button></div>
|
||||
<div><h3>外设与扩展能力</h3><div class="v2-extension-sync-actions"><button class="v2-pet-settings-launch" type="button" data-open-pet-settings><i data-lucide="paw-print"></i><span>宠物设置</span></button><button class="v2-extension-nfc-launch" type="button" data-open-nfc-binding><i data-lucide="scan-line"></i><span>NFC 设备绑定</span></button></div></div>
|
||||
<span data-state="${nfcPet || devices.length ? "online" : nfcReader.online ? "listening" : "offline"}"><i></i>${statusLabel}</span>
|
||||
</header>
|
||||
<section class="v2-nfc-inline" data-state="${nfcState}">
|
||||
@@ -4045,15 +4094,15 @@
|
||||
<b>${String(health.transport || "GSCU").toUpperCase()}</b>
|
||||
<button type="button" data-open-gscu-tcp><i data-lucide="settings-2"></i><span>TCP 设置</span></button>
|
||||
</div>
|
||||
${renderPetCardRegion()}
|
||||
<div class="v2-extension-device-list">
|
||||
${nfcPet ? renderDetectedPetCard(nfcPet) : ""}
|
||||
${devices.length ? devices.map((device) => `
|
||||
<article class="v2-extension-synced-device">
|
||||
<header><span><i data-lucide="${device.cardUid ? "badge-check" : "cpu"}"></i></span><div><strong>${escapeHtml(device.name)}</strong><small>${escapeHtml(device.type)}${device.cardUid ? " · 已绑定 NFC" : ""}</small></div><b>${device.actions.length} 项能力</b></header>
|
||||
<div>${device.actions.slice(0, 8).map((action) => `<span>${escapeHtml(action)}</span>`).join("")}</div>
|
||||
</article>`).join("") : `
|
||||
</article>`).join("") : nfcPet ? "" : `
|
||||
<div class="v2-extension-empty">
|
||||
<i data-lucide="${nfcPet ? "paw-print" : sensedCardUid ? "badge-alert" : "scan-line"}"></i><div><strong>${nfcPet ? "当前 NFC 已绑定宠物卡片" : sensedCardUid ? "当前 NFC 暂无已授权命令" : "当前未感应到外设"}</strong><span>${nfcPet ? "本次识别完成 3 帧后保持静默,移开再靠近才会再次发送" : sensedCardUid ? "请检查设备 ID 绑定及命令复选框" : "将已绑定设备 ID 的 NFC 卡靠近读卡器后显示命令"}</span></div>
|
||||
<i data-lucide="${sensedCardUid ? "badge-alert" : "scan-line"}"></i><div><strong>${sensedCardUid ? "当前 NFC 暂无已授权命令" : "当前未感应到外设"}</strong><span>${sensedCardUid ? "请检查设备 ID 绑定及命令复选框" : "将已绑定设备 ID 的 NFC 卡靠近读卡器后显示命令"}</span></div>
|
||||
</div>`}
|
||||
</div>
|
||||
<footer class="v2-extension-linkage" data-active="${String(Boolean(visibleLastAction))}">
|
||||
|
||||
Reference in New Issue
Block a user