(message, duration = 5000, error = false)
| 873 | |
| 874 | /* TOAST NOTIFICATIONS */ |
| 875 | function showToast(message, duration = 5000, error = false) { |
| 876 | const toast = document.createElement("div") |
| 877 | toast.classList.add("toast-notification") |
| 878 | if (error === true) { |
| 879 | toast.classList.add("toast-notification-error") |
| 880 | } |
| 881 | toast.innerHTML = message |
| 882 | document.body.appendChild(toast) |
| 883 | |
| 884 | // Set the position of the toast on the screen |
| 885 | const toastCount = document.querySelectorAll(".toast-notification").length |
| 886 | const toastHeight = toast.offsetHeight |
| 887 | const previousToastsHeight = Array.from(document.querySelectorAll(".toast-notification")) |
| 888 | .slice(0, -1) // exclude current toast |
| 889 | .reduce((totalHeight, toast) => totalHeight + toast.offsetHeight + 10, 0) // add 10 pixels for spacing |
| 890 | toast.style.bottom = `${10 + previousToastsHeight}px` |
| 891 | toast.style.right = "10px" |
| 892 | |
| 893 | // Delay the removal of the toast until animation has completed |
| 894 | const removeToast = () => { |
| 895 | toast.classList.add("hide") |
| 896 | const removeTimeoutId = setTimeout(() => { |
| 897 | toast.remove() |
| 898 | // Adjust the position of remaining toasts |
| 899 | const remainingToasts = document.querySelectorAll(".toast-notification") |
| 900 | const removedToastBottom = toast.getBoundingClientRect().bottom |
| 901 | |
| 902 | remainingToasts.forEach((toast) => { |
| 903 | if (toast.getBoundingClientRect().bottom < removedToastBottom) { |
| 904 | toast.classList.add("slide-down") |
| 905 | } |
| 906 | }) |
| 907 | |
| 908 | // Wait for the slide-down animation to complete |
| 909 | setTimeout(() => { |
| 910 | // Remove the slide-down class after the animation has completed |
| 911 | const slidingToasts = document.querySelectorAll(".slide-down") |
| 912 | slidingToasts.forEach((toast) => { |
| 913 | toast.classList.remove("slide-down") |
| 914 | }) |
| 915 | |
| 916 | // Adjust the position of remaining toasts again, in case there are multiple toasts being removed at once |
| 917 | const remainingToastsDown = document.querySelectorAll(".toast-notification") |
| 918 | let heightSoFar = 0 |
| 919 | remainingToastsDown.forEach((toast) => { |
| 920 | toast.style.bottom = `${10 + heightSoFar}px` |
| 921 | heightSoFar += toast.offsetHeight + 10 // add 10 pixels for spacing |
| 922 | }) |
| 923 | }, 0) // The duration of the slide-down animation (in milliseconds) |
| 924 | }, 500) |
| 925 | } |
| 926 | |
| 927 | // Remove the toast after specified duration |
| 928 | setTimeout(removeToast, duration) |
| 929 | } |
| 930 | |
| 931 | function alert(msg, title) { |
| 932 | title = title || "" |
no test coverage detected