Skip to content

Commit

Permalink
Modified the script: (#875)
Browse files Browse the repository at this point in the history
- set the timeout to 200ms

- Added new comments for better understanding of code

Co-authored-by: Aditya R Joshi <aditya.joshi-t@jud.ca.gov>
  • Loading branch information
adityajoshi94 and Aditya R Joshi committed Feb 21, 2024
1 parent 85deaca commit e7e7970
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion source/_patterns/02-molecules/alert/alert.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit e7e7970

Please sign in to comment.