Skip to content

Commit 748d700

Browse files
committed
Fix bug where dismissible banners didn't
You can now click the button on a dismissible site-wide banner, and it actually dismisses.
1 parent 913cad9 commit 748d700

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

assets/js/banner-dismiss.js

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,53 @@
1-
$(document).ready(function() {
2-
function setCookie(name, value, days) {
3-
let expires = "";
4-
let date = new Date(); // Create a new Date object
5-
let dateToSecond = 24 * 60 * 60 * 1000;
6-
7-
if (days) {
8-
date.setTime(date.getTime() + days * dateToSecond); // Modify the existing Date object
9-
expires = "; expires=" + date.toUTCString();
10-
}
11-
12-
document.cookie = name + "=" + value + expires + "; path=/";
13-
}
14-
15-
function getCookie(name) {
16-
let matches = document.cookie.match(new RegExp(
17-
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
18-
));
19-
return matches ? "true" : undefined;
20-
}
21-
22-
/* Check the presence of a cookie */
23-
let announcement = document.querySelector("#announcement");
1+
$(document).ready(function() {
2+
function setCookie(name, value, days) {
3+
let expires = "";
4+
let date = new Date(); // Create a new Date object
5+
let dateToSecond = 24 * 60 * 60 * 1000;
6+
7+
if (days) {
8+
date.setTime(date.getTime() + days * dateToSecond); // Modify the existing Date object
9+
expires = "; expires=" + date.toUTCString();
10+
}
11+
12+
document.cookie = name + "=" + value + expires + "; path=/";
13+
}
14+
15+
function getCookie(name) {
16+
let matches = document.cookie.match(new RegExp(
17+
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
18+
));
19+
return matches ? "true" : undefined;
20+
}
21+
22+
function getTokenName() {
23+
let announcement_name_rewritten = announcement.getAttribute('data-announcement-name').replace(/\s/g, '_');
24+
let token = 'announcement_ack_'+announcement_name_rewritten; // Generate the unique token for this announcement
25+
return token;
26+
}
27+
28+
/* Check the presence of a cookie */
29+
let announcement = document.querySelector("#announcement");
2430
if (announcement) {
25-
let token = `announcement_ack_${announcement.getAttribute('data-announcement-name').replace(/\s/g, '_')}`; // Generate the unique token for announcement
26-
let acknowledged = getCookie(token);
31+
let announcement_name_rewritten = announcement.getAttribute('data-announcement-name').replace(/\s/g, '_');
32+
let tokenName = getTokenName();
33+
let acknowledged = getCookie(tokenName);
2734
if (acknowledged === "true") {
2835
announcement.remove(); // Remove the announcement if the cookie is set
2936
}
3037
else {
3138
announcement.classList.add('display-announcement') // Display the announcement if the cookie is not set
3239
}
3340
}
34-
35-
/* Driver code to set the cookie */
36-
let button = document.querySelector('#banner-dismiss');
41+
42+
/* Driver code to set the cookie */
43+
let button = document.querySelector('#banner-dismiss');
3744
if (button) {
45+
let tokenName = getTokenName();
3846
button.removeAttribute('style');
3947
button.addEventListener('click', function() {
40-
setCookie(token, "true",
48+
setCookie(tokenName, "true",
4149
button.getAttribute('data-ttl')); // Set a cookie with time to live parameter
4250
announcement.remove();
4351
});
4452
}
45-
});
53+
});

0 commit comments

Comments
 (0)