297 lines
9.2 KiB
JavaScript
297 lines
9.2 KiB
JavaScript
(function (root, factory) {
|
|
// 谱文模块同时支持浏览器页面和 Node 单元测试。
|
|
if (typeof module === 'object' && module.exports) {
|
|
module.exports = factory(root);
|
|
return;
|
|
}
|
|
|
|
root.ArticlePages = factory(root);
|
|
if (root.document) {
|
|
root.document.addEventListener('DOMContentLoaded', function () {
|
|
root.ArticlePages.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 queryAll(selector, rootNode) {
|
|
return Array.prototype.slice.call((rootNode || documentRef).querySelectorAll(selector));
|
|
}
|
|
|
|
function showMessage(message) {
|
|
if (root.layui && root.layui.layer) {
|
|
root.layui.layer.msg(message);
|
|
return;
|
|
}
|
|
|
|
root.alert(message);
|
|
}
|
|
|
|
function getQueryParam(search, name) {
|
|
var params = new URLSearchParams(String(search || '').replace(/^\?/, ''));
|
|
return params.get(name) || '';
|
|
}
|
|
|
|
function getCurrentGenealogyId() {
|
|
var search = root.location && root.location.search;
|
|
var page = query('[data-article-page], [data-article-edit-page], [data-article-detail-page], [data-content-page]');
|
|
|
|
return getQueryParam(search, 'genealogyId') ||
|
|
getQueryParam(search, 'familyId') ||
|
|
(page && page.getAttribute('data-genealogy-id')) ||
|
|
getQueryParam(search, 'id') ||
|
|
'';
|
|
}
|
|
|
|
function getCurrentArticleId() {
|
|
var search = root.location && root.location.search;
|
|
|
|
return getQueryParam(search, 'articleId') || getQueryParam(search, 'id') || '';
|
|
}
|
|
|
|
function trimOrUndefined(value) {
|
|
var text = String(value === undefined || value === null ? '' : value).trim();
|
|
|
|
return text || undefined;
|
|
}
|
|
|
|
function toNumber(value, fallback) {
|
|
var text = String(value === undefined || value === null ? '' : value).trim();
|
|
var number = Number(text);
|
|
|
|
return text && Number.isFinite(number) ? number : fallback;
|
|
}
|
|
|
|
function formatStatus(status) {
|
|
var value = String(status === undefined || status === null ? '' : status);
|
|
|
|
if (value === '0' || value === 'published') return '已发布';
|
|
if (value === '1' || value === 'draft') return '草稿';
|
|
return value || '未知';
|
|
}
|
|
|
|
function buildArticleBody(values) {
|
|
var source = values || {};
|
|
|
|
return {
|
|
categoryId: toNumber(source.categoryId),
|
|
articleTitle: String(source.articleTitle || '').trim(),
|
|
articleSummary: trimOrUndefined(source.articleSummary),
|
|
coverOssId: toNumber(source.coverOssId),
|
|
articleContent: String(source.articleContent || '').trim(),
|
|
authorName: trimOrUndefined(source.authorName),
|
|
sortOrder: toNumber(source.sortOrder, 1),
|
|
status: String(source.status || '').trim() || '0'
|
|
};
|
|
}
|
|
|
|
function getArticleId(item) {
|
|
return pick(item, ['articleId', 'id'], '');
|
|
}
|
|
|
|
function buildArticleRow(item, genealogyId) {
|
|
var articleId = getArticleId(item);
|
|
var title = pick(item, ['articleTitle', 'title'], '未命名谱文');
|
|
var category = pick(item, ['categoryName', 'categoryTitle', 'typeName'], '谱文');
|
|
var status = formatStatus(pick(item, ['status'], '0'));
|
|
var updateTime = pick(item, ['updateTime', 'publishTime', 'createTime'], '未记录时间');
|
|
|
|
return '<a class="module-row article-row" href="article-detail.html?id=' + encodeURIComponent(articleId) + '&genealogyId=' + encodeURIComponent(genealogyId || '') + '">' +
|
|
'<div><h3>' + escapeHtml(title) + '</h3><p>' + escapeHtml(category) + ' · ' + escapeHtml(status) + ' · ' + escapeHtml(updateTime) + '</p></div>' +
|
|
'<span class="pill">查看</span></a>';
|
|
}
|
|
|
|
function buildCategoryOption(item) {
|
|
var categoryId = pick(item, ['categoryId', 'id'], '');
|
|
var name = pick(item, ['categoryName', 'name', 'title'], '未命名分类');
|
|
|
|
return '<option value="' + escapeHtml(categoryId) + '">' + escapeHtml(name) + '</option>';
|
|
}
|
|
|
|
function renderArticles(items, genealogyId) {
|
|
var container = query('[data-article-list], [data-recent-content]');
|
|
var list = normalizeList(items);
|
|
|
|
if (!container) return;
|
|
if (!list.length) {
|
|
container.innerHTML = '<div class="api-empty">暂无谱文内容</div>';
|
|
return;
|
|
}
|
|
|
|
container.innerHTML = list.map(function (item) {
|
|
return buildArticleRow(item, genealogyId);
|
|
}).join('');
|
|
}
|
|
|
|
function renderCategories(items) {
|
|
var select = query('[data-article-category]');
|
|
var list = normalizeList(items);
|
|
|
|
if (!select) return;
|
|
select.innerHTML = '<option value="">请选择分类</option>' + list.map(buildCategoryOption).join('');
|
|
}
|
|
|
|
function renderArticleDetail(article) {
|
|
var source = article || {};
|
|
var title = query('[data-article-title]');
|
|
var summary = query('[data-article-summary]');
|
|
var meta = query('[data-article-meta]');
|
|
var content = query('[data-article-content]');
|
|
|
|
if (title) title.textContent = pick(source, ['articleTitle', 'title'], '未命名谱文');
|
|
if (summary) summary.textContent = pick(source, ['articleSummary', 'summary'], '暂无摘要');
|
|
if (meta) {
|
|
meta.innerHTML = '<span>' + escapeHtml(pick(source, ['categoryName'], '谱文')) + '</span><span>' + escapeHtml(pick(source, ['authorName'], '佚名')) + '</span><span>' + escapeHtml(pick(source, ['updateTime', 'publishTime', 'createTime'], '未记录时间')) + '</span>';
|
|
}
|
|
if (content) content.innerHTML = pick(source, ['articleContent', 'content'], '<p>暂无正文</p>');
|
|
}
|
|
|
|
function fillArticleForm(article) {
|
|
var source = article || {};
|
|
|
|
queryAll('[name]').forEach(function (field) {
|
|
var value = pick(source, [field.name], '');
|
|
if (value !== '') field.value = value;
|
|
});
|
|
}
|
|
|
|
function getFormValues(form) {
|
|
var values = {};
|
|
|
|
queryAll('[name]', form).forEach(function (field) {
|
|
values[field.name] = field.value;
|
|
});
|
|
|
|
return values;
|
|
}
|
|
|
|
async function loadArticles() {
|
|
var api = getApi();
|
|
var genealogyId = getCurrentGenealogyId();
|
|
var articleId = getCurrentArticleId();
|
|
|
|
if (!api || !genealogyId) return;
|
|
|
|
try {
|
|
if (query('[data-article-list], [data-recent-content]')) {
|
|
renderArticles(await api.articles(genealogyId), genealogyId);
|
|
}
|
|
|
|
if (query('[data-article-category]')) {
|
|
renderCategories(await api.articleCategories(genealogyId));
|
|
}
|
|
|
|
if (articleId && query('[data-article-detail-page], [data-article-edit-page]')) {
|
|
if (query('[data-article-detail-page]')) {
|
|
renderArticleDetail(await api.articleDetail(genealogyId, articleId));
|
|
} else {
|
|
fillArticleForm(await api.articleDetail(genealogyId, articleId));
|
|
}
|
|
}
|
|
} catch (error) {
|
|
showMessage(error.message || '谱文数据加载失败');
|
|
}
|
|
}
|
|
|
|
async function submitArticle(form) {
|
|
var api = getApi();
|
|
var genealogyId = getCurrentGenealogyId();
|
|
var articleId = getCurrentArticleId();
|
|
var body = buildArticleBody(getFormValues(form));
|
|
|
|
if (!api || !genealogyId) {
|
|
showMessage('请从具体家谱进入谱文编辑');
|
|
return;
|
|
}
|
|
|
|
if (!body.articleTitle || !body.articleContent) {
|
|
showMessage('请填写谱文标题和正文');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
if (articleId) {
|
|
await api.updateArticle(genealogyId, articleId, body);
|
|
} else {
|
|
await api.createArticle(genealogyId, body);
|
|
}
|
|
|
|
showMessage('谱文已保存');
|
|
root.location.href = 'profile-article.html?genealogyId=' + encodeURIComponent(genealogyId);
|
|
} catch (error) {
|
|
showMessage(error.message || '保存谱文失败');
|
|
}
|
|
}
|
|
|
|
function bindActions() {
|
|
if (!documentRef) return;
|
|
|
|
documentRef.addEventListener('submit', function (event) {
|
|
var form = event.target.closest('[data-article-form]');
|
|
|
|
if (!form) return;
|
|
event.preventDefault();
|
|
submitArticle(form);
|
|
});
|
|
}
|
|
|
|
function init() {
|
|
if (!documentRef) return;
|
|
|
|
bindActions();
|
|
loadArticles();
|
|
}
|
|
|
|
return {
|
|
normalizeList: normalizeList,
|
|
buildArticleBody: buildArticleBody,
|
|
buildArticleRow: buildArticleRow,
|
|
formatStatus: formatStatus,
|
|
init: init
|
|
};
|
|
});
|