|
| 1 | +const { DismissTimer = 3000, RemoveDuration = 500 } = options ?? {} |
| 2 | + |
| 3 | +/** Toast direction constants */ |
| 4 | +const DIRECTION_TOP = 'top' |
| 5 | +const DIRECTION_BOTTOM = 'bottom' |
| 6 | + |
| 7 | +/** Auto-dismiss and removal durations */ |
| 8 | +const DISMISS_TIMER = DismissTimer |
| 9 | +const REMOVE_DURATION = RemoveDuration |
| 10 | + |
| 11 | +/** |
| 12 | + * Removes all CSS classes from the specified element that start with the given prefix. |
| 13 | + * @param {HTMLElement} element - The element from which to remove classes. |
| 14 | + * @param {string} prefix - The prefix of classes to be removed. |
| 15 | + */ |
| 16 | +function removeClassesWithPrefix(element, prefix) { |
| 17 | + element.classList.forEach((className) => { |
| 18 | + if (className.startsWith(prefix)) { |
| 19 | + element.classList.remove(className) |
| 20 | + } |
| 21 | + }) |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Performs the exit animation for a toast element and removes it from the DOM after the animation. |
| 26 | + * |
| 27 | + * @param {HTMLElement} element - The toast element to animate and remove. |
| 28 | + * @param {boolean} animated - Specifies whether to apply an exit animation. |
| 29 | + * @param {string} positionName - The position of the toast used to determine the direction of the exit animation. |
| 30 | + */ |
| 31 | +function performExitAnimationAndRemove(element, animated, positionName) { |
| 32 | + const classesToAdd = ['gttClose'] |
| 33 | + |
| 34 | + if (animated) { |
| 35 | + classesToAdd.push( |
| 36 | + positionName.startsWith(DIRECTION_TOP) |
| 37 | + ? 'gttCloseFromTop' |
| 38 | + : 'gttCloseFromBottom' |
| 39 | + ) |
| 40 | + } |
| 41 | + element.classList.add(...classesToAdd) |
| 42 | + |
| 43 | + removeClassesWithPrefix(element, 'gttShow') |
| 44 | + if (animated) removeClassesWithPrefix(element, 'gttShowFrom') |
| 45 | + |
| 46 | + setTimeout(() => element.remove(), REMOVE_DURATION) |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Closes the toast by stopping event propagation, clearing auto-dismiss timer, and performing exit animation. |
| 51 | + * @param {Event} e - The event object. |
| 52 | + */ |
| 53 | +function closeToast(e) { |
| 54 | + e.stopPropagation() |
| 55 | + const toast = e.target.closest('[class^="gttToast"]') |
| 56 | + if (toast) { |
| 57 | + clearTimeout(Number(toast.dataset.dismissTimer)) |
| 58 | + performExitAnimationAndRemove( |
| 59 | + toast, |
| 60 | + toaster.Animation, |
| 61 | + toaster.Position.Name |
| 62 | + ) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Initializes the automatic dismissal of toasts with an optional progress bar animation. |
| 68 | + */ |
| 69 | +const handleAutoDismiss = { |
| 70 | + init(isWithProgressBar) { |
| 71 | + document |
| 72 | + .querySelectorAll('[class^="gttToast"][data-auto-dismiss="true"]') |
| 73 | + .forEach((toast) => { |
| 74 | + if (isWithProgressBar) animateProgressBar(toast) |
| 75 | + |
| 76 | + toast.dataset.dismissTimer = setTimeout(() => { |
| 77 | + performExitAnimationAndRemove( |
| 78 | + toast, |
| 79 | + toaster.Animation, |
| 80 | + toaster.Position.Name |
| 81 | + ) |
| 82 | + }, DISMISS_TIMER) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Initializes and animates the progress bar within a toast notification element. |
| 89 | + * |
| 90 | + * @param {HTMLElement} element - The toast notification element containing the progress bar. |
| 91 | + */ |
| 92 | +function animateProgressBar(element) { |
| 93 | + const progressBarElement = element.querySelector('[class^="gttProgressBar"]') |
| 94 | + |
| 95 | + if (progressBarElement) { |
| 96 | + progressBarElement.style.transition = `width ${DISMISS_TIMER}ms linear` |
| 97 | + progressBarElement.style.width = '100%' |
| 98 | + |
| 99 | + requestAnimationFrame(() => { |
| 100 | + progressBarElement.style.width = '0%' |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/** Close toast on button click */ |
| 106 | +document.body.addEventListener('click', (e) => { |
| 107 | + const closeButton = e.target.closest('[class*="gttCloseBtn"]') |
| 108 | + if (closeButton) closeToast(e) |
| 109 | +}) |
| 110 | + |
| 111 | +/** Initialize auto-dismiss on page load */ |
| 112 | +document.addEventListener('DOMContentLoaded', () => { |
| 113 | + if (toaster) handleAutoDismiss.init(toaster.ProgressBar) |
| 114 | +}) |
0 commit comments