157 lines
4.7 KiB
JavaScript
157 lines
4.7 KiB
JavaScript
(function (root, factory) {
|
|
// 帮助中心模块同时支持浏览器页面和 Node 单元测试。
|
|
if (typeof module === 'object' && module.exports) {
|
|
module.exports = factory(root);
|
|
return;
|
|
}
|
|
|
|
root.HelpPages = factory(root);
|
|
if (root.document) {
|
|
root.document.addEventListener('DOMContentLoaded', function () {
|
|
root.HelpPages.init();
|
|
});
|
|
}
|
|
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
|
|
'use strict';
|
|
|
|
var documentRef = root.document;
|
|
|
|
function normalizeList(data) {
|
|
// 通用列表响应可能是数组,也可能包在 rows、records、list 或 data 中。
|
|
if (Array.isArray(data)) return data;
|
|
if (!data || typeof data !== 'object') return [];
|
|
if (Array.isArray(data.rows)) return data.rows;
|
|
if (Array.isArray(data.records)) return data.records;
|
|
if (Array.isArray(data.list)) return data.list;
|
|
if (Array.isArray(data.data)) return data.data;
|
|
return [];
|
|
}
|
|
|
|
function pick(item, keys, fallback) {
|
|
var source = item || {};
|
|
var index;
|
|
var value;
|
|
|
|
for (index = 0; index < keys.length; index += 1) {
|
|
value = source[keys[index]];
|
|
if (value !== undefined && value !== null && value !== '') return value;
|
|
}
|
|
|
|
return fallback || '';
|
|
}
|
|
|
|
function escapeHtml(value) {
|
|
// 帮助文章来自接口,进入 innerHTML 前先转义。
|
|
return String(value === undefined || value === null ? '' : value)
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''');
|
|
}
|
|
|
|
function getApi() {
|
|
return root.GenealogyApi && root.GenealogyApi.defaultClient;
|
|
}
|
|
|
|
function query(selector, rootNode) {
|
|
return (rootNode || documentRef).querySelector(selector);
|
|
}
|
|
|
|
function showMessage(message) {
|
|
if (root.layui && root.layui.layer) {
|
|
root.layui.layer.msg(message);
|
|
return;
|
|
}
|
|
|
|
root.alert(message);
|
|
}
|
|
|
|
function buildHelpItem(item, index) {
|
|
// 列表接口字段未在文档中细化,这里兼容常见标题和摘要字段。
|
|
var id = pick(item, ['helpId', 'id', 'articleId'], '');
|
|
var title = pick(item, ['title', 'articleTitle', 'name'], '帮助文章');
|
|
var content = pick(item, ['summary', 'description', 'content', 'articleContent'], '暂无详细说明');
|
|
var hasFullContent = Boolean(pick(item, ['content', 'articleContent'], ''));
|
|
var open = index === 0 ? ' open' : '';
|
|
|
|
return '<details' + open + ' class="tilt-card" data-help-id="' + escapeHtml(id) + '" data-help-loaded="' + (hasFullContent ? 'true' : 'false') + '">' +
|
|
'<summary>' + escapeHtml(title) + '</summary>' +
|
|
'<p>' + escapeHtml(content) + '</p>' +
|
|
'</details>';
|
|
}
|
|
|
|
function renderHelpArticles(items) {
|
|
var container = query('[data-help-list]');
|
|
|
|
if (!container) return;
|
|
if (!items.length) {
|
|
container.innerHTML = '<div class="api-empty">暂无帮助文章</div>';
|
|
return;
|
|
}
|
|
|
|
container.innerHTML = items.map(buildHelpItem).join('');
|
|
}
|
|
|
|
async function loadHelpArticles() {
|
|
var api = getApi();
|
|
var container = query('[data-help-list]');
|
|
|
|
if (!api || !container) return;
|
|
|
|
container.classList.add('is-loading');
|
|
try {
|
|
renderHelpArticles(normalizeList(await api.helpArticles()));
|
|
} catch (error) {
|
|
showMessage(error.message || '帮助文章加载失败');
|
|
} finally {
|
|
container.classList.remove('is-loading');
|
|
}
|
|
}
|
|
|
|
async function loadHelpDetail(details) {
|
|
var api = getApi();
|
|
var helpId = details && details.getAttribute('data-help-id');
|
|
var paragraph = details && query('p', details);
|
|
var content;
|
|
|
|
if (!api || !details || !helpId || details.getAttribute('data-help-loaded') === 'true') return;
|
|
|
|
details.classList.add('is-loading');
|
|
try {
|
|
content = await api.helpArticleDetail(helpId);
|
|
paragraph.textContent = pick(content, ['content', 'articleContent', 'summary', 'description'], paragraph.textContent);
|
|
details.setAttribute('data-help-loaded', 'true');
|
|
} catch (error) {
|
|
showMessage(error.message || '帮助详情加载失败');
|
|
} finally {
|
|
details.classList.remove('is-loading');
|
|
}
|
|
}
|
|
|
|
function bindDetailLoad() {
|
|
var container = query('[data-help-list]');
|
|
|
|
if (!container) return;
|
|
container.addEventListener('toggle', function (event) {
|
|
if (event.target && event.target.matches('details') && event.target.open) {
|
|
loadHelpDetail(event.target);
|
|
}
|
|
}, true);
|
|
}
|
|
|
|
function init() {
|
|
if (!documentRef) return;
|
|
|
|
bindDetailLoad();
|
|
loadHelpArticles();
|
|
}
|
|
|
|
return {
|
|
normalizeList: normalizeList,
|
|
buildHelpItem: buildHelpItem,
|
|
renderHelpArticles: renderHelpArticles,
|
|
init: init
|
|
};
|
|
});
|