248 lines
6.8 KiB
JavaScript
248 lines
6.8 KiB
JavaScript
(function (root, factory) {
|
||
// 备忘录模块同时支持浏览器页面和 Node 单元测试。
|
||
if (typeof module === 'object' && module.exports) {
|
||
module.exports = factory(root);
|
||
return;
|
||
}
|
||
|
||
root.MemoPages = factory(root);
|
||
if (root.document) {
|
||
root.document.addEventListener('DOMContentLoaded', function () {
|
||
root.MemoPages.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-memo-page], [data-memo-edit-page]');
|
||
|
||
return getQueryParam(search, 'genealogyId') ||
|
||
(page && page.getAttribute('data-genealogy-id')) ||
|
||
getQueryParam(search, 'id') ||
|
||
'';
|
||
}
|
||
|
||
function getCurrentMemoId() {
|
||
var search = root.location && root.location.search;
|
||
|
||
return getQueryParam(search, 'memoId') || '';
|
||
}
|
||
|
||
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 formatCompleted(value) {
|
||
return String(value || '0') === '1' ? '已完成' : '未完成';
|
||
}
|
||
|
||
function buildMemoBody(values) {
|
||
// MemoBody 来自 APP.openapi.json,memoTitle 必填。
|
||
var source = values || {};
|
||
|
||
return {
|
||
memoTitle: String(source.memoTitle || '').trim(),
|
||
memoContent: trimOrUndefined(source.memoContent),
|
||
remindTime: trimOrUndefined(source.remindTime),
|
||
completed: String(source.completed || '').trim() || '0',
|
||
mediaOssIds: trimOrUndefined(source.mediaOssIds),
|
||
sortOrder: toNumber(source.sortOrder, 1),
|
||
status: String(source.status || '').trim() || '0'
|
||
};
|
||
}
|
||
|
||
function getMemoId(item) {
|
||
return pick(item, ['memoId', 'id'], '');
|
||
}
|
||
|
||
function buildMemoRow(item, genealogyId) {
|
||
var memoId = getMemoId(item);
|
||
var title = pick(item, ['memoTitle', 'title'], '未命名备忘');
|
||
var completed = formatCompleted(pick(item, ['completed'], '0'));
|
||
var remindTime = pick(item, ['remindTime', 'createTime'], '未设置提醒');
|
||
|
||
return '<a class="module-row memo-row" href="profile-memo-edit.html?memoId=' + encodeURIComponent(memoId) + '&genealogyId=' + encodeURIComponent(genealogyId || '') + '">' +
|
||
'<div><h3>' + escapeHtml(title) + '</h3><p>' + escapeHtml(completed) + ' · ' + escapeHtml(remindTime) + '</p></div>' +
|
||
'<span class="pill">编辑</span></a>';
|
||
}
|
||
|
||
function renderMemos(items, genealogyId) {
|
||
var container = query('[data-memo-list]');
|
||
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 buildMemoRow(item, genealogyId);
|
||
}).join('');
|
||
}
|
||
|
||
function getFormValues(form) {
|
||
var values = {};
|
||
|
||
queryAll('[name]', form).forEach(function (field) {
|
||
values[field.name] = field.value;
|
||
});
|
||
|
||
return values;
|
||
}
|
||
|
||
function fillMemoForm(memo) {
|
||
var source = memo || {};
|
||
|
||
queryAll('[name]').forEach(function (field) {
|
||
var value = pick(source, [field.name], '');
|
||
if (value !== '') field.value = value;
|
||
});
|
||
}
|
||
|
||
async function loadMemos() {
|
||
var api = getApi();
|
||
var genealogyId = getCurrentGenealogyId();
|
||
var memoId = getCurrentMemoId();
|
||
|
||
if (!api || !genealogyId) return;
|
||
|
||
try {
|
||
if (query('[data-memo-list]')) {
|
||
renderMemos(await api.memos(genealogyId), genealogyId);
|
||
}
|
||
|
||
if (memoId && query('[data-memo-edit-page]')) {
|
||
fillMemoForm(await api.memoDetail(genealogyId, memoId));
|
||
}
|
||
} catch (error) {
|
||
showMessage(error.message || '备忘录加载失败');
|
||
}
|
||
}
|
||
|
||
async function submitMemo(form) {
|
||
var api = getApi();
|
||
var genealogyId = getCurrentGenealogyId();
|
||
var memoId = getCurrentMemoId();
|
||
var body = buildMemoBody(getFormValues(form));
|
||
|
||
if (!api || !genealogyId) return;
|
||
if (!body.memoTitle) {
|
||
showMessage('请填写备忘标题');
|
||
return;
|
||
}
|
||
|
||
try {
|
||
if (memoId) {
|
||
await api.updateMemo(genealogyId, memoId, body);
|
||
} else {
|
||
await api.createMemo(genealogyId, body);
|
||
}
|
||
|
||
showMessage('备忘录已保存');
|
||
root.location.href = 'profile-memo.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-memo-form]');
|
||
|
||
if (!form) return;
|
||
event.preventDefault();
|
||
submitMemo(form);
|
||
});
|
||
}
|
||
|
||
function init() {
|
||
if (!documentRef) return;
|
||
|
||
bindActions();
|
||
loadMemos();
|
||
}
|
||
|
||
return {
|
||
normalizeList: normalizeList,
|
||
buildMemoBody: buildMemoBody,
|
||
buildMemoRow: buildMemoRow,
|
||
init: init
|
||
};
|
||
});
|