From e7e7970353c595a35765a3b253f22008c15ba2b8 Mon Sep 17 00:00:00 2001 From: adityajoshi94 Date: Wed, 21 Feb 2024 11:09:03 -0800 Subject: [PATCH] Modified the script: (#875) - set the timeout to 200ms - Added new comments for better understanding of code Co-authored-by: Aditya R Joshi --- source/_patterns/02-molecules/alert/alert.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/source/_patterns/02-molecules/alert/alert.js b/source/_patterns/02-molecules/alert/alert.js index ee8998418..273fbf9d0 100644 --- a/source/_patterns/02-molecules/alert/alert.js +++ b/source/_patterns/02-molecules/alert/alert.js @@ -1,32 +1,41 @@ $(document).ready(function () { setTimeout(function () { + //Check for alerts that haven't been processed yet. var alerts = $(".usa-alert:not(.cookie-processed)"); if (alerts) { + // Process each alert. $(alerts).each(function (idx, alert) { let messageContentObject = alert.getElementsByClassName("usa-alert__text"), cookieId; if (messageContentObject[0]) { + // Generate a unique Id for cthe cookie based on alert's content. cookieId = "patternlab-alert-hide-" + stringToHash(messageContentObject[0].innerText); } // Message loads hidden to avoid flash if cookie is found. if (!getCookie(cookieId)) { + // If the cookie doesn't exist , display the alert. alert.classList.add("active"); } + + //Attach an onclick handler to the close button of the alert. var alert_close = alert.getElementsByClassName("usa-alert__close"); if (alert_close[0]) { alert_close[0].onclick = function alertClose() { if (alert.classList.contains("active")) { + // Set a cookie to remember that alert has been dismissed. setCookie(cookieId, true, 7); + // Hide the alert. alert.classList.remove("active"); } }; } + // Mark the alert as procssed to avoid re-processing. alert.classList.add("cookie-processed"); }); } - }, 10); + }, 200); // Helper functions function setCookie(name, value, days) {