Skip to content

Commit 7c1f91a

Browse files
authored
Merge pull request #44435 from Gauravpadam/20231912_banner_dismissal
The announcement banners can now be made dismissible
2 parents fc1c1b6 + d234b6c commit 7c1f91a

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

assets/scss/_custom.scss

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,8 @@ body.td-documentation {
732732
}
733733
}
734734

735+
/* Announcements */
736+
735737
#announcement {
736738
> * {
737739
color: inherit;
@@ -753,10 +755,21 @@ body.td-documentation {
753755
padding-top: 40px;
754756
}
755757

758+
// Don't show announcements when javascript is not available
759+
760+
html.no-js body div#announcement {
761+
display: none;
762+
}
763+
764+
#announcement.display-announcement{
765+
display: block; // apply this class to display the announcement
766+
}
767+
756768
#announcement {
757769
// default background is blue; overrides are possible
758770
color: #fff;
759-
771+
display: none; // When javascript is available, Let javascript handle the state of the announcement
772+
760773
.announcement-main {
761774
margin-left: auto;
762775
margin-right: auto;

data/i18n/en/en.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ other = """<p>This page is automatically generated.</p><p>If you plan to report
1212
[blog_post_show_more]
1313
other = "Show More Posts..."
1414

15+
[banner_acknowledgement]
16+
other = "Hide this notice"
17+
1518
[caution]
1619
other = "Caution:"
1720

layouts/partials/announcement.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
{{- if or (eq .endTime nil ) (gt ( time .endTime ) now ) -}}
1616
{{- if not $announcementShown -}}
1717
{{- $announcementShown = true -}}
18-
<div lang="en" id="announcement" style="background-color: #3371e3; color: #fff; {{ .style | safeCSS }}">
18+
<div lang="en" id="announcement" data-announcement-name="{{ .name }}" style="background-color: #3371e3; color: #fff; {{ .style | safeCSS }}">
1919
<aside>
2020
<div class="announcement-main" data-nosnippet>
2121
{{ if .title }}
@@ -24,6 +24,9 @@ <h4>
2424
</h4>
2525
{{ end }}
2626
<p>{{ .message | markdownify }}</p>
27+
{{- if eq .dismissParameters.dismissible true -}}
28+
<button id="banner-dismiss" class="button" style="display: none;" data-ttl={{ .dismiss_params.showDismissedAnnouncementAfterDays }}>{{ T "banner_acknowledgement" }}</button> <!-- Only JS enabled users will see this button -->
29+
{{- end -}}
2730
</div>
2831
</aside>
2932
</div>

layouts/partials/head.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@
8383
<!--script src="https://unpkg.com/split.js/dist/split.min.js"></script-->
8484
<script src="/js/split-1.6.0.js" intregrity="sha384-0blL3GqHy6+9fw0cyY2Aoiwg4onHAtslAs4OkqZY7UQBrR65/K4gI+hxLdWDrjpz"></script>
8585

86+
<!--Script for dismissing banners/notices-->
87+
<script defer src="{{ "js/dismiss_banner.js" | relURL }}"></script>
88+
89+
8690
{{- if or (.HasShortcode "table") (.HasShortcode "feature-gate-table") -}}
8791
<script defer src="{{ "js/sortable-table.js" | relURL }}"></script>
8892
{{- end -}}

static/js/dismiss_banner.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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");
24+
let token = `announcement_ack_${announcement.getAttribute('data-announcement-name').replace(/\s/g, '_')}`; // Generate the unique token for announcement
25+
let acknowledged = getCookie(token);
26+
if (acknowledged === "true") {
27+
announcement.remove(); // Remove the announcement if the cookie is set
28+
}
29+
else {
30+
announcement.classList.add('display-announcement') // Display the announcement if the cookie is not set
31+
}
32+
33+
/* Driver code to set the cookie */
34+
let button = document.querySelector('#banner-dismiss');
35+
button.removeAttribute('style');
36+
button.addEventListener('click', function() {
37+
setCookie(token, "true",
38+
button.getAttribute('data-ttl')); // Set a cookie with time to live parameter
39+
announcement.remove();
40+
});
41+
});

0 commit comments

Comments
 (0)