134 lines
4.3 KiB
JavaScript
134 lines
4.3 KiB
JavaScript
(function () {
|
|
function getBootstrapToast(toast) {
|
|
if (!toast || !window.bootstrap || !window.bootstrap.Toast) {
|
|
return null;
|
|
}
|
|
|
|
return window.bootstrap.Toast.getOrCreateInstance(toast, {
|
|
autohide: true,
|
|
delay: 4000
|
|
});
|
|
}
|
|
|
|
function removeToast(toast) {
|
|
if (toast && toast.parentNode) {
|
|
toast.parentNode.removeChild(toast);
|
|
}
|
|
}
|
|
|
|
function dismissToast(toast) {
|
|
var instance = getBootstrapToast(toast);
|
|
if (instance) {
|
|
instance.hide();
|
|
return;
|
|
}
|
|
removeToast(toast);
|
|
}
|
|
|
|
function setToastVariant(toast, variant) {
|
|
if (!toast || !toast.classList) {
|
|
return;
|
|
}
|
|
|
|
var nextVariant = String(variant || 'info').trim().toLowerCase();
|
|
var variants = ['primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light', 'dark'];
|
|
variants.forEach(function (value) {
|
|
toast.classList.remove('text-bg-' + value);
|
|
});
|
|
toast.classList.add('text-bg-' + (variants.indexOf(nextVariant) === -1 ? 'info' : nextVariant));
|
|
}
|
|
|
|
function getMessageVariant(message, fallbackVariant) {
|
|
var text = String(message || '').trim();
|
|
if (/\b(?:unable to|cannot|can't|could not|failed to)\s+delete\b/i.test(text) || /\bdelete\b.*\b(?:before|first)\b/i.test(text) || /\bstill (?:in use|linked|assigned|used)\b/i.test(text)) {
|
|
return 'danger';
|
|
}
|
|
if (/\b(?:already exists|already exist|already taken|duplicate|must be unique|name already exists)\b/i.test(text)) {
|
|
return 'warning';
|
|
}
|
|
return String(fallbackVariant || 'info').trim().toLowerCase() || 'info';
|
|
}
|
|
|
|
function showToast(message, variant) {
|
|
var text = String(message || '').trim();
|
|
if (!text) {
|
|
return;
|
|
}
|
|
|
|
var container = document.getElementById('app-toast-container');
|
|
if (!container) {
|
|
return;
|
|
}
|
|
|
|
var existingToast = document.getElementById('app-toast');
|
|
if (existingToast) {
|
|
var existingBody = existingToast.querySelector('.toast-body');
|
|
if (existingBody) {
|
|
existingBody.textContent = text;
|
|
}
|
|
var nextVariant = getMessageVariant(text, variant);
|
|
existingToast.setAttribute('data-toast-variant', nextVariant);
|
|
setToastVariant(existingToast, nextVariant);
|
|
var existingInstance = getBootstrapToast(existingToast);
|
|
if (existingInstance) {
|
|
existingInstance.show();
|
|
}
|
|
return;
|
|
}
|
|
|
|
var toast = document.createElement('div');
|
|
toast.className = 'toast align-items-center border-0';
|
|
toast.id = 'app-toast';
|
|
toast.setAttribute('role', 'status');
|
|
toast.setAttribute('aria-live', 'polite');
|
|
toast.setAttribute('aria-atomic', 'true');
|
|
toast.setAttribute('data-bs-autohide', 'true');
|
|
toast.setAttribute('data-bs-delay', '4000');
|
|
toast.innerHTML = '<div class="d-flex"><div class="toast-body"></div><button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Dismiss notification"></button></div>';
|
|
var toastVariant = getMessageVariant(text, variant);
|
|
toast.setAttribute('data-toast-variant', toastVariant);
|
|
setToastVariant(toast, toastVariant);
|
|
toast.querySelector('.toast-body').textContent = text;
|
|
|
|
toast.addEventListener('hidden.bs.toast', function () {
|
|
removeToast(toast);
|
|
});
|
|
|
|
container.appendChild(toast);
|
|
var instance = getBootstrapToast(toast);
|
|
if (instance) {
|
|
instance.show();
|
|
}
|
|
}
|
|
|
|
function initToast() {
|
|
var toast = document.getElementById('app-toast');
|
|
if (!toast) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
var url = new URL(window.location.href);
|
|
if (url.searchParams.has('message')) {
|
|
url.searchParams.delete('message');
|
|
window.history.replaceState({}, document.title, url.pathname + url.search + url.hash);
|
|
}
|
|
} catch (_error) {
|
|
// ignore URL cleanup failures
|
|
}
|
|
|
|
var existingVariant = String(toast.getAttribute('data-toast-variant') || '').trim().toLowerCase() || getMessageVariant((toast.querySelector('.toast-body') && toast.querySelector('.toast-body').textContent) || '', 'info');
|
|
setToastVariant(toast, existingVariant);
|
|
|
|
var instance = getBootstrapToast(toast);
|
|
if (instance) {
|
|
instance.show();
|
|
}
|
|
}
|
|
|
|
window.dismissToast = dismissToast;
|
|
window.showToast = showToast;
|
|
window.initToast = initToast;
|
|
|
|
initToast();
|
|
}()); |