assetflow/static/js/toastHandler.js

71 lines
2.7 KiB
JavaScript
Raw Normal View History

currentToasts = [];
2023-07-07 14:02:54 +02:00
var forceSkipReload = false;
forceSkipReload = localStorage.getItem('forceSkipReload') === 'true';
if(forceSkipReload) {
setTimeout(() => {
createNewToast('Auto reload still disabled, click version number to reenable.', 'text-bg-warning', 3000, false);
}, 1000);
}
2023-05-21 00:42:05 +02:00
/**
* Generic function to create a new toast
* @param {String} message The message to be displayed
* @param {String} colorSelector The bootstrap color selector class, can be one of the following: text-bg-primary, text-bg-success, text-bg-danger, text-bg-warning, text-bg-info
* @param {Number} autoHideTime The time in milliseconds to auto hide the toast, default is 3000
* @param {Boolean} autoReload Should the page reload after the toast is hidden, default is true (for compatibility with old code)
* @returns {String} The id of the created toast, format: toast-<number>
*/
2023-07-07 14:02:54 +02:00
function createNewToast(message, colorSelector, autoHideTime = 1500, autoReload = true) {
2023-05-17 17:46:51 +02:00
const targetContainer = document.getElementById('toastMainController');
const masterToast = document.getElementById('masterToast');
const newToast = masterToast.cloneNode(true);
2023-05-21 01:45:27 +02:00
newToast.classList.add(colorSelector);
2023-05-17 17:46:51 +02:00
newToast.id = `toast-${currentToasts.length}`;
console.log(newToast.childNodes[1]);
newToast.childNodes[1].childNodes[1].innerHTML = message;
targetContainer.appendChild(newToast);
currentToasts.push(newToast);
$(newToast).toast('show');
setTimeout(() => {
destroyToast(newToast.id);
2023-07-07 14:02:54 +02:00
if (autoReload && !forceSkipReload) {
2023-05-17 17:46:51 +02:00
location.reload();
}
}, autoHideTime);
2023-05-21 00:42:05 +02:00
return newToast.id;
2023-05-17 17:46:51 +02:00
}
2023-05-21 00:42:05 +02:00
/**
* Generic function to destroy a toast
* @param {String} id The id of the toast to destroy
*/
2023-05-21 01:45:27 +02:00
function destroyToast(id) {
2023-05-17 17:46:51 +02:00
const targetContainer = document.getElementById('toastMainController');
const targetToast = document.getElementById(id);
targetContainer.removeChild(targetToast);
currentToasts.splice(currentToasts.indexOf(targetToast), 1);
2023-05-21 00:42:05 +02:00
}
// Moved here
2023-05-21 01:45:27 +02:00
function normalizeToast() {
console.warn('Something is using the deprecated function normalizeToast(). Please use createNewToast() instead.');
2023-05-21 00:42:05 +02:00
$('#generalToast').removeClass('text-bg-primary');
$('#generalToast').removeClass('text-bg-success');
$('#generalToast').removeClass('text-bg-danger');
$('#generalToast').removeClass('text-bg-warning');
$('#generalToast').removeClass('text-bg-info');
}
/**
* Function to handle the "secret" function to globally disable auto reload
*/
function toggleAutoReload() {
forceSkipReload = !forceSkipReload;
if(forceSkipReload) {
createNewToast('Auto reload disabled', 'text-bg-warning', 1500, false);
} else {
createNewToast('Auto reload enabled', 'text-bg-success', 1500, false);
}
// Store the value in local storage
localStorage.setItem('forceSkipReload', forceSkipReload);
}