-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cookie banner and cookie handling
- Loading branch information
Showing
11 changed files
with
491 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 90 additions & 8 deletions
98
src/nationalarchives/components/cookie-banner/cookie-banner.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,104 @@ | ||
import Cookies from "../../lib/cookies.mjs"; | ||
|
||
export class CookieBanner { | ||
constructor($module) { | ||
this.$module = $module; | ||
this.$accept = $module && $module.querySelector('[value="accept"]'); | ||
this.$reject = $module && $module.querySelector('[value="reject"]'); | ||
this.$acceptButton = $module && $module.querySelector('[value="accept"]'); | ||
this.$rejectButton = $module && $module.querySelector('[value="reject"]'); | ||
this.$prompt = | ||
$module && $module.querySelector(".tna-cookie-banner__message--prompt"); | ||
this.$acceptedMessage = | ||
$module && $module.querySelector(".tna-cookie-banner__message--accepted"); | ||
this.$rejectedMessage = | ||
$module && $module.querySelector(".tna-cookie-banner__message--rejected"); | ||
this.$closeButtons = $module && $module.querySelectorAll('[value="close"]'); | ||
} | ||
|
||
init() { | ||
if (!this.$module || !this.$accept || !this.$reject) { | ||
if ( | ||
!this.$module || | ||
!this.$acceptButton || | ||
!this.$rejectButton || | ||
!this.$prompt || | ||
!this.$acceptedMessage || | ||
!this.$rejectedMessage || | ||
!this.$closeButtons | ||
) { | ||
return; | ||
} | ||
|
||
const policies = this.$module.getAttribute("data-policies"); | ||
if (!policies) { | ||
return; | ||
} | ||
this.cookies = new Cookies( | ||
policies.split(",").map((policy) => policy.trim()), | ||
); | ||
|
||
this.loadScriptsOnAccept = this.$module.getAttribute("data-acceptscripts"); | ||
|
||
this.hideCookieBannerKey = this.$module.getAttribute("data-hidekey"); | ||
const cookiePreferencesSet = this.cookies.exists(this.hideCookieBannerKey); | ||
|
||
if (!cookiePreferencesSet) { | ||
this.$module.removeAttribute("hidden"); | ||
|
||
this.$acceptButton.addEventListener("click", () => this.accept()); | ||
this.$rejectButton.addEventListener("click", () => this.reject()); | ||
|
||
this.$closeButtons.forEach(($closeButton) => { | ||
$closeButton.addEventListener("click", () => this.close()); | ||
}); | ||
} | ||
|
||
// ==================== DEV ==================== | ||
// document.getElementById("reset").addEventListener("click", () => { | ||
// this.cookies.delete(this.hideCookieBannerKey); | ||
// this.cookies.delete("cookies_policy"); | ||
// window.scrollY = 0; | ||
// window.location = window.location; | ||
// }); | ||
// document | ||
// .getElementById("accept-analytics-policy") | ||
// .addEventListener("click", () => this.cookies.acceptPolicy("analytics")); | ||
// document | ||
// .getElementById("reject-analytics-policy") | ||
// .addEventListener("click", () => this.cookies.rejectPolicy("analytics")); | ||
// document | ||
// .getElementById("accept-settings-policy") | ||
// .addEventListener("click", () => this.cookies.acceptPolicy("settings")); | ||
// document | ||
// .getElementById("reject-settings-policy") | ||
// .addEventListener("click", () => this.cookies.rejectPolicy("settings")); | ||
// ==================== END ==================== | ||
} | ||
|
||
this.$module.removeAttribute("hidden"); | ||
accept() { | ||
this.$prompt.setAttribute("hidden", true); | ||
this.complete(); | ||
this.$acceptedMessage.removeAttribute("hidden"); | ||
this.cookies.acceptAllPolicies(); | ||
if (this.loadScriptsOnAccept) { | ||
this.loadScriptsOnAccept.split(",").forEach((script) => { | ||
const $script = document.createElement("script"); | ||
$script.src = script; | ||
document.head.appendChild($script); | ||
}); | ||
} | ||
} | ||
|
||
this.$accept.addEventListener("click", () => this.accept()); | ||
this.$reject.addEventListener("click", () => this.reject()); | ||
reject() { | ||
this.$prompt.setAttribute("hidden", true); | ||
this.complete(); | ||
this.$rejectedMessage.removeAttribute("hidden"); | ||
this.cookies.rejectAllPolicies(); | ||
} | ||
|
||
accept() {} | ||
complete() { | ||
this.cookies.set(this.hideCookieBannerKey, true); | ||
} | ||
|
||
reject() {} | ||
close() { | ||
this.$module.setAttribute("hidden", true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.