You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
180 lines
7.5 KiB
JavaScript
180 lines
7.5 KiB
JavaScript
(function (global) {
|
|
'use strict';
|
|
|
|
var activeRequest = null;
|
|
var fallbackMode = false;
|
|
var ignoredCloseEvents = 0;
|
|
|
|
function byId(id) { return global.document ? global.document.getElementById(id) : null; }
|
|
function value(input, fallback) {
|
|
return input === null || input === undefined || String(input).trim() === ''
|
|
? fallback : String(input);
|
|
}
|
|
function safeTone(input) { return input === 'warning' ? 'warning' : 'danger'; }
|
|
|
|
function elements() {
|
|
var dialog = byId('admin-danger-dialog');
|
|
var form = byId('admin-danger-dialog-form');
|
|
var result = {
|
|
dialog: dialog,
|
|
form: form,
|
|
title: byId('admin-danger-dialog-title'),
|
|
message: byId('admin-danger-dialog-message'),
|
|
kicker: dialog ? dialog.querySelector('.admin-danger-dialog-kicker') : null,
|
|
mark: dialog ? dialog.querySelector('.admin-danger-dialog-mark') : null,
|
|
close: byId('admin-danger-dialog-close'),
|
|
cancel: byId('admin-danger-dialog-cancel'),
|
|
confirm: byId('admin-danger-dialog-confirm'),
|
|
verification: byId('admin-danger-dialog-verification'),
|
|
verificationInput: byId('admin-danger-dialog-verification-input'),
|
|
verificationLabel: byId('admin-danger-dialog-verification-label'),
|
|
verificationHint: byId('admin-danger-dialog-verification-hint'),
|
|
};
|
|
result.ready = Object.keys(result).every(function (key) {
|
|
return key === 'ready' || result[key];
|
|
});
|
|
return result;
|
|
}
|
|
|
|
function matchesRequiredText(nodes) {
|
|
return activeRequest && activeRequest.requiredText !== null
|
|
&& nodes.verificationInput.value === activeRequest.requiredText;
|
|
}
|
|
|
|
function updateVerification(nodes) {
|
|
if (!activeRequest || activeRequest.requiredText === null) {
|
|
nodes.confirm.disabled = false;
|
|
nodes.verificationInput.removeAttribute('aria-invalid');
|
|
nodes.verification.classList.remove('is-matched');
|
|
nodes.verificationHint.classList.remove('is-mismatch');
|
|
nodes.verificationHint.textContent = '';
|
|
return;
|
|
}
|
|
var matched = matchesRequiredText(nodes);
|
|
var hasValue = nodes.verificationInput.value.length > 0;
|
|
nodes.confirm.disabled = !matched;
|
|
nodes.verificationInput.setAttribute('aria-invalid', hasValue && !matched ? 'true' : 'false');
|
|
nodes.verification.classList.toggle('is-matched', matched);
|
|
nodes.verificationHint.classList.toggle('is-mismatch', hasValue && !matched);
|
|
nodes.verificationHint.textContent = matched ? '名称已匹配,可以继续。'
|
|
: (hasValue ? '名称不匹配,请完整输入:' : '请输入:') + activeRequest.requiredText;
|
|
}
|
|
|
|
function restoreFocus(node) {
|
|
if (!node || typeof node.focus !== 'function' || node.isConnected === false) return;
|
|
try { node.focus({ preventScroll: true }); } catch (error) { node.focus(); }
|
|
}
|
|
|
|
function closeDialog(nodes) {
|
|
global.document.documentElement.classList.remove('admin-danger-dialog-open');
|
|
nodes.dialog.classList.remove('is-dialog-fallback');
|
|
if (nodes.dialog.open && typeof nodes.dialog.close === 'function') {
|
|
ignoredCloseEvents += 1;
|
|
nodes.dialog.close();
|
|
} else {
|
|
nodes.dialog.removeAttribute('open');
|
|
}
|
|
fallbackMode = false;
|
|
}
|
|
|
|
function settle(nodes, confirmed) {
|
|
if (!activeRequest) return;
|
|
var request = activeRequest;
|
|
activeRequest = null;
|
|
closeDialog(nodes);
|
|
nodes.verificationInput.value = '';
|
|
restoreFocus(request.opener);
|
|
request.resolve(confirmed === true);
|
|
}
|
|
|
|
function focusableControls(nodes) {
|
|
return [nodes.close, nodes.verification.hidden ? null : nodes.verificationInput,
|
|
nodes.cancel, nodes.confirm].filter(function (control) {
|
|
return control && !control.disabled && !control.hidden;
|
|
});
|
|
}
|
|
|
|
function ensureListeners(nodes) {
|
|
if (nodes.dialog.dataset.adminDangerReady === '1') return;
|
|
nodes.dialog.dataset.adminDangerReady = '1';
|
|
nodes.close.addEventListener('click', function () { settle(nodes, false); });
|
|
nodes.cancel.addEventListener('click', function () { settle(nodes, false); });
|
|
nodes.verificationInput.addEventListener('input', function () { updateVerification(nodes); });
|
|
nodes.form.addEventListener('submit', function (event) {
|
|
event.preventDefault();
|
|
if (activeRequest && activeRequest.requiredText !== null && !matchesRequiredText(nodes)) return;
|
|
settle(nodes, true);
|
|
});
|
|
nodes.dialog.addEventListener('cancel', function (event) {
|
|
event.preventDefault();
|
|
settle(nodes, false);
|
|
});
|
|
nodes.dialog.addEventListener('close', function () {
|
|
if (ignoredCloseEvents > 0) { ignoredCloseEvents -= 1; return; }
|
|
settle(nodes, false);
|
|
});
|
|
nodes.dialog.addEventListener('click', function (event) {
|
|
if (event.target === nodes.dialog) settle(nodes, false);
|
|
});
|
|
nodes.dialog.addEventListener('keydown', function (event) {
|
|
if (event.key === 'Escape') {
|
|
event.preventDefault();
|
|
settle(nodes, false);
|
|
return;
|
|
}
|
|
if (!fallbackMode || event.key !== 'Tab') return;
|
|
var controls = focusableControls(nodes);
|
|
if (!controls.length) return;
|
|
var first = controls[0];
|
|
var last = controls[controls.length - 1];
|
|
if (event.shiftKey && global.document.activeElement === first) {
|
|
event.preventDefault(); last.focus();
|
|
} else if (!event.shiftKey && global.document.activeElement === last) {
|
|
event.preventDefault(); first.focus();
|
|
}
|
|
});
|
|
}
|
|
|
|
function confirmAction(options) {
|
|
var nodes = elements();
|
|
if (!nodes.ready || activeRequest) return Promise.resolve(false);
|
|
ensureListeners(nodes);
|
|
var config = options && typeof options === 'object' ? options : {};
|
|
var requiredText = typeof config.requiredText === 'string' && config.requiredText.length > 0
|
|
? config.requiredText : null;
|
|
var tone = safeTone(config.tone);
|
|
return new Promise(function (resolve) {
|
|
activeRequest = {
|
|
opener: global.document.activeElement,
|
|
requiredText: requiredText,
|
|
resolve: resolve,
|
|
};
|
|
nodes.dialog.dataset.tone = tone;
|
|
nodes.kicker.textContent = tone === 'warning' ? '操作确认' : '高风险操作';
|
|
nodes.title.textContent = value(config.title, '确认操作');
|
|
nodes.message.textContent = value(config.message, '请确认是否继续。');
|
|
nodes.cancel.textContent = value(config.cancelText, '取消');
|
|
nodes.confirm.textContent = value(config.confirmText, '确认');
|
|
nodes.confirm.className = tone === 'warning' ? 'btn btn-warning' : 'btn btn-danger';
|
|
nodes.verification.hidden = requiredText === null;
|
|
nodes.verificationInput.value = '';
|
|
nodes.verificationLabel.textContent = value(config.requiredLabel, '输入完整名称以确认');
|
|
if (requiredText === null) nodes.verificationInput.removeAttribute('required');
|
|
else nodes.verificationInput.setAttribute('required', 'required');
|
|
nodes.verificationInput.setAttribute('aria-required', requiredText === null ? 'false' : 'true');
|
|
updateVerification(nodes);
|
|
global.document.documentElement.classList.add('admin-danger-dialog-open');
|
|
if (typeof nodes.dialog.showModal === 'function') nodes.dialog.showModal();
|
|
else {
|
|
fallbackMode = true;
|
|
nodes.dialog.classList.add('is-dialog-fallback');
|
|
nodes.dialog.setAttribute('open', 'open');
|
|
}
|
|
if (requiredText !== null) nodes.verificationInput.focus();
|
|
else nodes.cancel.focus();
|
|
});
|
|
}
|
|
|
|
global.VenixAdminDangerDialog = Object.freeze({ confirm: confirmAction });
|
|
}(typeof window !== 'undefined' ? window : globalThis));
|