等待后端更新接口
This commit is contained in:
+124
-37
@@ -15,6 +15,7 @@
|
||||
|
||||
var documentRef = root.document;
|
||||
var recordsById = Object.create(null);
|
||||
var canEditContent = false;
|
||||
var writePending = false;
|
||||
|
||||
function getApi() {
|
||||
@@ -280,19 +281,26 @@
|
||||
return relativeId ? url + '&relativeId=' + encodeURIComponent(relativeId) : url;
|
||||
}
|
||||
|
||||
function canEditRecords(genealogy) {
|
||||
return Boolean(genealogy && (genealogy.canEditContent === true || genealogy.canManage === true));
|
||||
}
|
||||
|
||||
function renderRelativeRow(record) {
|
||||
var summary = [];
|
||||
var actions = '<button class="pill" type="button" data-relative-detail-id="' +
|
||||
escapeHtml(record.relativeId) + '">查看详情</button>';
|
||||
|
||||
if (record.relationName) summary.push(record.relationName);
|
||||
if (record.eventName) summary.push(record.eventName);
|
||||
if (record.eventTime) summary.push(record.eventTime);
|
||||
if (!summary.length) summary.push('未填写关系和事件');
|
||||
if (canEditContent) {
|
||||
actions += '<a class="pill" href="' + escapeHtml(buildEditUrl(record.genealogyId, record.relativeId)) +
|
||||
'">编辑</a><button class="pill is-danger" type="button" data-relative-delete-id="' +
|
||||
escapeHtml(record.relativeId) + '">删除</button>';
|
||||
}
|
||||
return '<article class="module-row relative-row"><div><h3>' + escapeHtml(record.relativeName) + '</h3><p>' +
|
||||
escapeHtml(summary.join(' · ')) + '</p></div><div class="row-actions">' +
|
||||
'<button class="pill" type="button" data-relative-detail-id="' + escapeHtml(record.relativeId) + '">查看详情</button>' +
|
||||
'<a class="pill" href="' + escapeHtml(buildEditUrl(record.genealogyId, record.relativeId)) + '">编辑</a>' +
|
||||
'<button class="pill is-danger" type="button" data-relative-delete-id="' + escapeHtml(record.relativeId) + '">删除</button>' +
|
||||
'</div></article>';
|
||||
escapeHtml(summary.join(' · ')) + '</p></div><div class="row-actions">' + actions + '</div></article>';
|
||||
}
|
||||
|
||||
function detailValue(label, value) {
|
||||
@@ -301,9 +309,16 @@
|
||||
|
||||
function renderRelativeDetail(record) {
|
||||
var count;
|
||||
var actions = '';
|
||||
|
||||
if (!record) return '<div class="api-empty">请选择一条亲友记录查看详情</div>';
|
||||
if (!record) return stateHtml('empty', '请选择一条亲友记录查看详情');
|
||||
count = attachmentCount(record.mediaOssIds);
|
||||
if (canEditContent) {
|
||||
actions = '<div class="bottom-actions"><a class="btn ghost" href="' +
|
||||
escapeHtml(buildEditUrl(record.genealogyId, record.relativeId)) + '">编辑记录</a>' +
|
||||
'<button class="btn ghost" type="button" data-relative-delete-id="' +
|
||||
escapeHtml(record.relativeId) + '">删除记录</button></div>';
|
||||
}
|
||||
return '<div class="form-like relative-detail">' +
|
||||
detailValue('亲友姓名', record.relativeName) +
|
||||
detailValue('关系名称', record.relationName) +
|
||||
@@ -315,9 +330,7 @@
|
||||
detailValue('记录人', record.appUserNickName) +
|
||||
detailValue('状态', record.status === '0' ? '正常' : '停用') +
|
||||
detailValue('备注', record.remark) +
|
||||
'</div><div class="bottom-actions"><a class="btn ghost" href="' +
|
||||
escapeHtml(buildEditUrl(record.genealogyId, record.relativeId)) + '">编辑记录</a>' +
|
||||
'<button class="btn ghost" type="button" data-relative-delete-id="' + escapeHtml(record.relativeId) + '">删除记录</button></div>';
|
||||
'</div>' + actions;
|
||||
}
|
||||
|
||||
function showMessage(message) {
|
||||
@@ -335,6 +348,10 @@
|
||||
return Number(error && (error.status || error.code)) === 403;
|
||||
}
|
||||
|
||||
function getApiStateType(error) {
|
||||
return isForbidden(error) ? 'forbidden' : 'error';
|
||||
}
|
||||
|
||||
function redirectUnauthorized(api, error) {
|
||||
if (!shouldRedirectToLogin(api, error)) return false;
|
||||
if (api && api.clearToken) api.clearToken();
|
||||
@@ -344,13 +361,38 @@
|
||||
return true;
|
||||
}
|
||||
|
||||
function setFormStatus(message) {
|
||||
var target = query('[data-relative-form-status]');
|
||||
|
||||
if (target) target.textContent = message || '';
|
||||
function stateHtml(type, message) {
|
||||
if (root.ProfileUI && root.ProfileUI.renderApiState) return root.ProfileUI.renderApiState(type, message);
|
||||
return '<div class="api-state api-state--' + type + '" role="' +
|
||||
(type === 'error' || type === 'forbidden' ? 'alert' : 'status') +
|
||||
'" aria-live="polite">' + escapeHtml(message) + '</div>';
|
||||
}
|
||||
|
||||
function setEditorEnabled(enabled, message) {
|
||||
function setState(container, type, message) {
|
||||
if (!container) return;
|
||||
if (root.ProfileUI && root.ProfileUI.setApiState) {
|
||||
root.ProfileUI.setApiState(container, type, message);
|
||||
return;
|
||||
}
|
||||
container.innerHTML = stateHtml(type, message);
|
||||
}
|
||||
|
||||
function setFormStatus(message, type) {
|
||||
var target = query('[data-relative-form-status]');
|
||||
|
||||
if (!target) return;
|
||||
if (!message) {
|
||||
target.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
if (type && root.ProfileUI && root.ProfileUI.setApiState) {
|
||||
root.ProfileUI.setApiState(target, type, message);
|
||||
return;
|
||||
}
|
||||
target.textContent = message;
|
||||
}
|
||||
|
||||
function setEditorEnabled(enabled, message, type) {
|
||||
var placeholder = query('[data-relative-editor-placeholder]');
|
||||
|
||||
queryAll('[data-relative-editor], [data-relative-editor-action]').forEach(function (element) {
|
||||
@@ -358,15 +400,39 @@
|
||||
});
|
||||
if (placeholder) {
|
||||
placeholder.hidden = Boolean(enabled);
|
||||
if (message) placeholder.textContent = message;
|
||||
if (!enabled && message) {
|
||||
if (type) setState(placeholder, type, message);
|
||||
else placeholder.textContent = message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setWritePending(value) {
|
||||
function setEditAccess(enabled) {
|
||||
canEditContent = Boolean(enabled);
|
||||
queryAll('[data-relative-create-link]').forEach(function (element) {
|
||||
element.hidden = !canEditContent;
|
||||
});
|
||||
}
|
||||
|
||||
async function loadCapability(api, genealogyId) {
|
||||
var genealogy = await api.genealogyDetail(genealogyId);
|
||||
|
||||
setEditAccess(canEditRecords(genealogy));
|
||||
return genealogy;
|
||||
}
|
||||
|
||||
function setWritePending(value, label) {
|
||||
var submit = query('[data-relative-form] [type="submit"]');
|
||||
|
||||
writePending = Boolean(value);
|
||||
queryAll('[data-relative-form] button, [data-relative-form] input, [data-relative-form] textarea, [data-relative-delete-id]').forEach(function (control) {
|
||||
control.disabled = writePending;
|
||||
});
|
||||
if (submit) {
|
||||
if (!submit.dataset.defaultLabel) submit.dataset.defaultLabel = submit.textContent;
|
||||
submit.textContent = writePending && label ? label : submit.dataset.defaultLabel;
|
||||
submit.setAttribute('aria-busy', writePending ? 'true' : 'false');
|
||||
}
|
||||
}
|
||||
|
||||
function syncLinks() {
|
||||
@@ -378,10 +444,11 @@
|
||||
var detail = query('[data-relative-detail]');
|
||||
var message = '请先选择家谱,再查看或维护亲友记录。';
|
||||
|
||||
if (list) list.innerHTML = '<div class="api-empty">' + message + '</div>';
|
||||
if (detail) detail.innerHTML = '<div class="api-empty">当前没有家谱上下文。</div>';
|
||||
setEditorEnabled(false, message);
|
||||
setFormStatus(message);
|
||||
setState(list, 'empty', message);
|
||||
setState(detail, 'empty', '当前没有家谱上下文。');
|
||||
setEditAccess(false);
|
||||
setEditorEnabled(false, message, 'empty');
|
||||
setFormStatus(message, 'empty');
|
||||
}
|
||||
|
||||
function requireGenealogyId() {
|
||||
@@ -403,11 +470,11 @@
|
||||
});
|
||||
if (!container) return records;
|
||||
if (!Array.isArray(data) || invalidCount) {
|
||||
container.innerHTML = '<div class="api-empty">亲友记录响应缺少稳定 RelativeRecordVo 字段,请联系后端核对。</div>';
|
||||
setState(container, 'error', '亲友记录响应缺少稳定 RelativeRecordVo 字段,请联系后端核对。');
|
||||
return [];
|
||||
}
|
||||
if (!records.length) {
|
||||
container.innerHTML = '<div class="api-empty">暂无亲友往来记录</div>';
|
||||
setState(container, 'empty', '暂无亲友往来记录');
|
||||
return [];
|
||||
}
|
||||
container.innerHTML = records.map(renderRelativeRow).join('');
|
||||
@@ -426,6 +493,7 @@
|
||||
var record;
|
||||
|
||||
if (!genealogyId || !normalizeId(relativeId) || redirectUnauthorized(api)) return null;
|
||||
setState(query('[data-relative-detail]'), 'loading', '正在加载亲友记录详情…');
|
||||
try {
|
||||
record = normalizeRelativeRecord(await api.relativeRecordDetail(genealogyId, relativeId));
|
||||
if (!record) throw new Error('亲友记录详情响应缺少稳定 RelativeRecordVo 字段');
|
||||
@@ -434,7 +502,11 @@
|
||||
return record;
|
||||
} catch (error) {
|
||||
if (redirectUnauthorized(api, error)) return null;
|
||||
renderDetail(null);
|
||||
setState(
|
||||
query('[data-relative-detail]'),
|
||||
getApiStateType(error),
|
||||
isForbidden(error) ? '当前账号无权查看该亲友记录。' : '亲友记录详情加载失败,请稍后重试。'
|
||||
);
|
||||
showMessage(isForbidden(error) ? '当前账号无权查看该亲友记录。' : (error.message || '亲友记录详情加载失败'));
|
||||
return null;
|
||||
}
|
||||
@@ -450,7 +522,9 @@
|
||||
genealogyId = requireGenealogyId();
|
||||
if (!genealogyId) return;
|
||||
syncLinks();
|
||||
setState(query('[data-relative-list]'), 'loading', '正在加载亲友记录…');
|
||||
try {
|
||||
await loadCapability(api, genealogyId);
|
||||
records = renderRelativeRecords(await api.relativeRecords(genealogyId));
|
||||
requestedRelativeId = getCurrentRelativeId();
|
||||
if (requestedRelativeId && recordsById[requestedRelativeId]) {
|
||||
@@ -462,11 +536,12 @@
|
||||
}
|
||||
} catch (error) {
|
||||
if (redirectUnauthorized(api, error)) return;
|
||||
if (query('[data-relative-list]')) {
|
||||
query('[data-relative-list]').innerHTML = '<div class="api-empty">' +
|
||||
(isForbidden(error) ? '当前账号无权查看该家谱的亲友记录。' : '亲友记录加载失败,请稍后重试。') +
|
||||
'</div>';
|
||||
}
|
||||
setEditAccess(false);
|
||||
setState(
|
||||
query('[data-relative-list]'),
|
||||
getApiStateType(error),
|
||||
isForbidden(error) ? '当前账号无权查看该家谱的亲友记录。' : '亲友记录加载失败,请稍后重试。'
|
||||
);
|
||||
showMessage(isForbidden(error) ? '当前账号无权查看该家谱的亲友记录。' : (error.message || '亲友记录加载失败'));
|
||||
}
|
||||
}
|
||||
@@ -515,8 +590,11 @@
|
||||
if (!genealogyId) return;
|
||||
relativeId = getCurrentRelativeId();
|
||||
syncLinks();
|
||||
setEditorEnabled(false, '正在加载亲友记录编辑信息…');
|
||||
setEditorEnabled(false, '正在加载亲友记录编辑信息…', 'loading');
|
||||
setFormStatus('正在读取家谱权限…', 'loading');
|
||||
try {
|
||||
await loadCapability(api, genealogyId);
|
||||
if (!canEditContent) throw { status: 403, message: '当前账号无权维护该家谱的亲友记录。' };
|
||||
if (relativeId) {
|
||||
record = normalizeRelativeRecord(await api.relativeRecordDetail(genealogyId, relativeId));
|
||||
if (!record) throw new Error('亲友记录详情响应缺少稳定 RelativeRecordVo 字段');
|
||||
@@ -524,12 +602,17 @@
|
||||
if (query('[data-relative-editor-title]')) query('[data-relative-editor-title]').textContent = '编辑亲友记录';
|
||||
}
|
||||
setEditorEnabled(true);
|
||||
setFormStatus('');
|
||||
} catch (error) {
|
||||
if (redirectUnauthorized(api, error)) return;
|
||||
setEditAccess(false);
|
||||
setEditorEnabled(false, isForbidden(error)
|
||||
? '当前账号无权维护该家谱的亲友记录。'
|
||||
: (error.message || '亲友记录编辑信息加载失败'));
|
||||
setFormStatus(error.message || '亲友记录编辑信息加载失败');
|
||||
: (error.message || '亲友记录编辑信息加载失败'), getApiStateType(error));
|
||||
setFormStatus(
|
||||
isForbidden(error) ? '当前账号无权维护该家谱的亲友记录。' : (error.message || '亲友记录编辑信息加载失败'),
|
||||
getApiStateType(error)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -544,18 +627,18 @@
|
||||
var savedId;
|
||||
var verifiedDetail;
|
||||
|
||||
if (writePending || !genealogyId || redirectUnauthorized(api)) return;
|
||||
if (writePending || !canEditContent || !genealogyId || redirectUnauthorized(api)) return;
|
||||
body = buildRelativeRecordBody(getFormValues(form));
|
||||
validation = validateRelativeRecordBody(body);
|
||||
if (validation) {
|
||||
setFormStatus(validation);
|
||||
setFormStatus(validation, 'error');
|
||||
return;
|
||||
}
|
||||
delete body.invalidGiftAmount;
|
||||
delete body.invalidGiftPrecision;
|
||||
delete body.invalidSortOrder;
|
||||
setWritePending(true);
|
||||
setFormStatus('正在保存亲友记录…');
|
||||
setWritePending(true, '正在保存…');
|
||||
setFormStatus('正在保存亲友记录…', 'loading');
|
||||
try {
|
||||
result = relativeId
|
||||
? await api.updateRelativeRecord(genealogyId, relativeId, body)
|
||||
@@ -568,7 +651,10 @@
|
||||
if (root.location) root.location.href = buildListUrl(genealogyId, savedId);
|
||||
} catch (error) {
|
||||
if (redirectUnauthorized(api, error)) return;
|
||||
setFormStatus(isForbidden(error) ? '当前账号无权保存该亲友记录。' : (error.message || '亲友记录保存失败'));
|
||||
setFormStatus(
|
||||
isForbidden(error) ? '当前账号无权保存该亲友记录。' : (error.message || '亲友记录保存失败'),
|
||||
getApiStateType(error)
|
||||
);
|
||||
} finally {
|
||||
setWritePending(false);
|
||||
}
|
||||
@@ -579,7 +665,7 @@
|
||||
var genealogyId = requireGenealogyId();
|
||||
var record = recordsById[relativeId];
|
||||
|
||||
if (writePending || !genealogyId || !normalizeId(relativeId) || redirectUnauthorized(api)) return;
|
||||
if (writePending || !canEditContent || !genealogyId || !normalizeId(relativeId) || redirectUnauthorized(api)) return;
|
||||
if (root.confirm && !root.confirm('确认删除“' + (record ? record.relativeName : '该亲友记录') + '”吗?删除后不可恢复,并会释放附件引用。')) return;
|
||||
setWritePending(true);
|
||||
try {
|
||||
@@ -649,6 +735,7 @@
|
||||
validateRelativeRecordBody: validateRelativeRecordBody,
|
||||
normalizeRelativeRecord: normalizeRelativeRecord,
|
||||
matchesSavedRecord: matchesSavedRecord,
|
||||
canEditRecords: canEditRecords,
|
||||
isEditableRecord: isEditableRecord,
|
||||
renderRelativeDetail: renderRelativeDetail,
|
||||
shouldRedirectToLogin: shouldRedirectToLogin,
|
||||
|
||||
Reference in New Issue
Block a user