assetflow/static/js/toastHandler.js

27 lines
951 B
JavaScript
Raw Normal View History

2023-05-17 17:46:51 +02:00
const currentToasts = [];
function createNewToast(message, colorSelector, autoHideTime = 3000, autoReload = true){
const targetContainer = document.getElementById('toastMainController');
const masterToast = document.getElementById('masterToast');
const newToast = masterToast.cloneNode(true);
newToast.classList.add(colorSelector)
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);
if(autoReload){
location.reload();
}
}, autoHideTime);
}
function destroyToast(id){
const targetContainer = document.getElementById('toastMainController');
const targetToast = document.getElementById(id);
targetContainer.removeChild(targetToast);
currentToasts.splice(currentToasts.indexOf(targetToast), 1);
}