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.
222 lines
8.1 KiB
JavaScript
222 lines
8.1 KiB
JavaScript
(function () {
|
|
'use strict';
|
|
|
|
var dialog = document.getElementById('panel-user-dialog');
|
|
var i18n = window.VenixUserCopy;
|
|
var form = document.getElementById('panel-user-dialog-form');
|
|
var title = document.getElementById('panel-user-dialog-title');
|
|
var message = document.getElementById('panel-user-dialog-message');
|
|
var kicker = dialog ? dialog.querySelector('.panel-user-dialog-kicker') : null;
|
|
var closeButton = document.getElementById('panel-user-dialog-close');
|
|
var cancelButton = document.getElementById('panel-user-dialog-cancel');
|
|
var confirmButton = document.getElementById('panel-user-dialog-confirm');
|
|
var verification = document.getElementById('panel-user-dialog-verification');
|
|
var verificationInput = document.getElementById('panel-user-dialog-verification-input');
|
|
var verificationLabel = document.getElementById('panel-user-dialog-verification-label');
|
|
var verificationHint = document.getElementById('panel-user-dialog-verification-hint');
|
|
var activeRequest = null;
|
|
var fallbackMode = false;
|
|
var ignoredCloseEvents = 0;
|
|
|
|
function stringValue(value, fallback) {
|
|
if (value === null || value === undefined || String(value).trim() === '') return fallback;
|
|
return String(value);
|
|
}
|
|
|
|
function safeTone(value) {
|
|
return ['info', 'success', 'warning', 'danger'].includes(value) ? value : 'warning';
|
|
}
|
|
|
|
function canOpen() {
|
|
return dialog && form && title && message && kicker && closeButton && cancelButton && confirmButton
|
|
&& verification && verificationInput && verificationLabel && verificationHint;
|
|
}
|
|
|
|
function matchesRequiredText() {
|
|
return activeRequest && activeRequest.requiredText !== null
|
|
&& verificationInput.value === activeRequest.requiredText;
|
|
}
|
|
|
|
function updateVerification() {
|
|
if (!activeRequest || activeRequest.requiredText === null) {
|
|
confirmButton.disabled = false;
|
|
verificationInput.removeAttribute('aria-invalid');
|
|
verification.classList.remove('is-matched');
|
|
verificationHint.classList.remove('is-mismatch');
|
|
return;
|
|
}
|
|
var matched = matchesRequiredText();
|
|
var hasValue = verificationInput.value.length > 0;
|
|
confirmButton.disabled = !matched;
|
|
verificationInput.setAttribute('aria-invalid', hasValue && !matched ? 'true' : 'false');
|
|
verification.classList.toggle('is-matched', matched);
|
|
verificationHint.classList.toggle('is-mismatch', hasValue && !matched);
|
|
verificationHint.textContent = matched
|
|
? i18n.text('common.dialog.verification.matched')
|
|
: i18n.text(
|
|
hasValue
|
|
? 'common.dialog.verification.mismatch'
|
|
: 'common.dialog.verification.prompt',
|
|
{ name: 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() {
|
|
document.documentElement.classList.remove('panel-user-dialog-open');
|
|
dialog.classList.remove('is-dialog-fallback');
|
|
if (dialog.open && typeof dialog.close === 'function') {
|
|
ignoredCloseEvents += 1;
|
|
dialog.close();
|
|
}
|
|
else dialog.removeAttribute('open');
|
|
fallbackMode = false;
|
|
}
|
|
|
|
function settle(value) {
|
|
if (!activeRequest) return;
|
|
var request = activeRequest;
|
|
activeRequest = null;
|
|
closeDialog();
|
|
verificationInput.value = '';
|
|
restoreFocus(request.opener);
|
|
request.resolve(value);
|
|
}
|
|
|
|
function show(options, mode) {
|
|
if (!canOpen() || activeRequest) return Promise.resolve(false);
|
|
var config = options && typeof options === 'object' ? options : {};
|
|
var requiredText = mode === 'confirm' && typeof config.requiredText === 'string'
|
|
&& config.requiredText.length > 0 ? config.requiredText : null;
|
|
var tone = safeTone(config.tone);
|
|
|
|
return new Promise(function (resolve) {
|
|
activeRequest = {
|
|
mode: mode,
|
|
opener: document.activeElement,
|
|
requiredText: requiredText,
|
|
resolve: resolve
|
|
};
|
|
|
|
dialog.dataset.tone = tone;
|
|
dialog.setAttribute('role', mode === 'alert' && tone !== 'danger' ? 'dialog' : 'alertdialog');
|
|
kicker.textContent = i18n.text(mode === 'alert'
|
|
? 'common.dialog.kicker.alert'
|
|
: requiredText === null
|
|
? 'common.dialog.kicker.confirm'
|
|
: 'common.dialog.kicker.danger');
|
|
title.textContent = stringValue(config.title, i18n.text(mode === 'alert'
|
|
? 'common.dialog.alert.title' : 'common.dialog.confirm.title'));
|
|
message.textContent = stringValue(
|
|
config.message,
|
|
i18n.text('common.dialog.confirm.body')
|
|
);
|
|
cancelButton.textContent = stringValue(
|
|
config.cancelText,
|
|
i18n.text('common.dialog.cancel')
|
|
);
|
|
confirmButton.textContent = stringValue(config.confirmText, i18n.text(
|
|
mode === 'alert'
|
|
? 'common.dialog.acknowledge' : 'common.dialog.confirm'
|
|
));
|
|
cancelButton.hidden = mode === 'alert';
|
|
verification.hidden = requiredText === null;
|
|
verificationInput.value = '';
|
|
if (requiredText === null) verificationInput.removeAttribute('required');
|
|
else verificationInput.setAttribute('required', 'required');
|
|
verificationInput.setAttribute('aria-required', requiredText === null ? 'false' : 'true');
|
|
verificationLabel.textContent = stringValue(
|
|
config.requiredLabel,
|
|
i18n.text('common.dialog.verification.label')
|
|
);
|
|
verificationHint.textContent = '';
|
|
confirmButton.className = 'button ' + (tone === 'danger' ? 'button-danger' : 'button-primary');
|
|
updateVerification();
|
|
|
|
document.documentElement.classList.add('panel-user-dialog-open');
|
|
if (typeof dialog.showModal === 'function') {
|
|
dialog.showModal();
|
|
} else {
|
|
fallbackMode = true;
|
|
dialog.classList.add('is-dialog-fallback');
|
|
dialog.setAttribute('open', 'open');
|
|
}
|
|
|
|
if (requiredText !== null) verificationInput.focus();
|
|
else if (mode === 'confirm') cancelButton.focus();
|
|
else confirmButton.focus();
|
|
});
|
|
}
|
|
|
|
function requestConfirmation(options) {
|
|
return show(options, 'confirm');
|
|
}
|
|
|
|
function showAlert(options) {
|
|
return show(options, 'alert').then(function () { return undefined; });
|
|
}
|
|
|
|
function focusableControls() {
|
|
return [closeButton, verification.hidden ? null : verificationInput,
|
|
cancelButton.hidden ? null : cancelButton, confirmButton]
|
|
.filter(function (control) { return control && !control.disabled && !control.hidden; });
|
|
}
|
|
|
|
if (canOpen()) {
|
|
closeButton.addEventListener('click', function () { settle(false); });
|
|
cancelButton.addEventListener('click', function () { settle(false); });
|
|
verificationInput.addEventListener('input', updateVerification);
|
|
form.addEventListener('submit', function (event) {
|
|
event.preventDefault();
|
|
if (activeRequest && activeRequest.requiredText !== null && !matchesRequiredText()) return;
|
|
settle(true);
|
|
});
|
|
dialog.addEventListener('cancel', function (event) {
|
|
event.preventDefault();
|
|
settle(false);
|
|
});
|
|
dialog.addEventListener('close', function () {
|
|
if (ignoredCloseEvents > 0) {
|
|
ignoredCloseEvents -= 1;
|
|
return;
|
|
}
|
|
settle(false);
|
|
});
|
|
dialog.addEventListener('click', function (event) {
|
|
if (event.target === dialog) settle(false);
|
|
});
|
|
dialog.addEventListener('keydown', function (event) {
|
|
if (event.key === 'Escape') {
|
|
event.preventDefault();
|
|
settle(false);
|
|
return;
|
|
}
|
|
if (!fallbackMode || event.key !== 'Tab') return;
|
|
var controls = focusableControls();
|
|
if (controls.length === 0) return;
|
|
var first = controls[0];
|
|
var last = controls[controls.length - 1];
|
|
if (event.shiftKey && document.activeElement === first) {
|
|
event.preventDefault();
|
|
last.focus();
|
|
} else if (!event.shiftKey && document.activeElement === last) {
|
|
event.preventDefault();
|
|
first.focus();
|
|
}
|
|
});
|
|
}
|
|
|
|
window.VenixDialog = Object.freeze({
|
|
confirm: requestConfirmation,
|
|
alert: showAlert
|
|
});
|
|
}());
|