rename chapter/lesson
This commit is contained in:
@@ -45,24 +45,43 @@
|
||||
const cancelBtn = title_change_modal.querySelector('#cancel-btn');
|
||||
const errBox = title_change_modal.querySelector('#title-error');
|
||||
const prevFocus = document.activeElement;
|
||||
|
||||
const setLoading = (on) => {
|
||||
okBtn.disabled = on;
|
||||
cancelBtn.disabled = on;
|
||||
okBtn.textContent = on ? '保存中…' : '确定';
|
||||
};
|
||||
const close = () => {
|
||||
document.body.removeChild(backdrop);
|
||||
if (prevFocus && prevFocus.focus) prevFocus.focus();
|
||||
};
|
||||
|
||||
const submit = () => {
|
||||
const raw = input.value;
|
||||
const result = onSubmit ? onSubmit(raw) : { ok: true };
|
||||
if (result && result.ok) {
|
||||
close();
|
||||
} else if (result && result.message) {
|
||||
errBox.textContent = result.message;
|
||||
input.setAttribute('aria-invalid', 'true');
|
||||
input.focus();
|
||||
}
|
||||
errBox.textContent = '';
|
||||
setLoading(true);
|
||||
|
||||
const maybePromise = onSubmit ? onSubmit(input.value) : { ok: true };
|
||||
|
||||
Promise
|
||||
.resolve(maybePromise)
|
||||
.then(result => {
|
||||
if (result && result.ok) {
|
||||
close();
|
||||
} else {
|
||||
// 显示错误但不关闭
|
||||
const msg = result && result.message ? result.message : '提交失败';
|
||||
errBox.textContent = msg;
|
||||
input.setAttribute('aria-invalid', 'true');
|
||||
input.focus();
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('Submit error:', err);
|
||||
errBox.textContent = '提交异常,请稍后再试';
|
||||
input.setAttribute('aria-invalid', 'true');
|
||||
input.focus();
|
||||
})
|
||||
.finally(() => setLoading(false));
|
||||
};
|
||||
|
||||
okBtn.addEventListener('click', submit);
|
||||
cancelBtn.addEventListener('click', close);
|
||||
backdrop.addEventListener('click', (e) => {
|
||||
|
||||
Reference in New Issue
Block a user