887 lines
30 KiB
TypeScript
887 lines
30 KiB
TypeScript
/** 4.1.1 */
|
|
|
|
export default class CookieNotice {
|
|
browserCookieName = "cn-init";
|
|
|
|
/** Cookie life time */
|
|
cookieTimeLife = 6;
|
|
|
|
/** The index on which store the expiration date */
|
|
cookieExpirationName = "_expiration";
|
|
|
|
/** Cookie policies for each functionality */
|
|
cookiePolicies: CookiePolicy = {
|
|
sameSite: CookiePolicySameSite.lax,
|
|
secure: "",
|
|
};
|
|
|
|
/** Cookie expiration date as specified in the cookie itself */
|
|
cookieExpirationDate: Date;
|
|
|
|
config: Record<string, CookieOptions>;
|
|
modalBg: HTMLDivElement;
|
|
cookieContainer: HTMLDivElement;
|
|
bannerContainer: HTMLDivElement;
|
|
modalContainer: HTMLDivElement;
|
|
|
|
cookieShowModalBtn: HTMLButtonElement;
|
|
cookieCloseModalBtn: HTMLButtonElement;
|
|
|
|
bannerContent: HTMLDivElement;
|
|
acceptAllBtn: HTMLButtonElement;
|
|
denyAllBtn: HTMLButtonElement;
|
|
customizeBtn: HTMLButtonElement;
|
|
cookiesUnorderedList: HTMLUListElement;
|
|
cookieLinkShowCustomizeModalList: NodeListOf<HTMLElement>;
|
|
modalWrapper: HTMLDivElement;
|
|
template: HTMLDivElement;
|
|
bannerDetailsAreExpanded: boolean;
|
|
|
|
restrictCloseModal: boolean;
|
|
|
|
static noConflict: (deep?: boolean) => typeof CookieNotice;
|
|
|
|
constructor(config: Record<string, CookieOptions>) {
|
|
// * Setup Config
|
|
this.config = config;
|
|
|
|
// * Setup cookie expiration date
|
|
let cookieExpirationDate: Date|null = this.readCookiesExpirationDate();
|
|
if (!cookieExpirationDate) {
|
|
cookieExpirationDate = new Date();
|
|
cookieExpirationDate.setMonth(cookieExpirationDate.getMonth() + this.cookieTimeLife);
|
|
}
|
|
this.cookieExpirationDate = cookieExpirationDate;
|
|
|
|
// * Inject DOM modal BG
|
|
const modalBg = document.createElement("div");
|
|
modalBg.classList.add("cn-modal-bg");
|
|
document.body.append(modalBg);
|
|
this.modalBg = modalBg;
|
|
|
|
// * Setup Dom Elements
|
|
const cookieContainer = document.querySelector(
|
|
"div#cn-wrapper"
|
|
) as HTMLDivElement | null,
|
|
cookieShowModalBtn = document.querySelector(
|
|
"button#cn-cookie-btn"
|
|
) as HTMLButtonElement | null,
|
|
cookieCloseModalBtn = document.querySelector(
|
|
"button#cn-close-modal"
|
|
) as HTMLButtonElement | null,
|
|
cookieLinkShowCustomizeModalPageList = document.querySelectorAll(
|
|
".cn-cookie-link"
|
|
) as NodeListOf<HTMLElement>,
|
|
bannerContainer = document.querySelector(
|
|
"div#cn-banner"
|
|
) as HTMLDivElement | null,
|
|
bannerContent = document.querySelector(
|
|
"div.cn-banner-content"
|
|
) as HTMLDivElement | null,
|
|
modalContainer = document.querySelector(
|
|
"div.cn-modal-container"
|
|
) as HTMLDivElement | null,
|
|
acceptAllBtn = document.querySelector(
|
|
"button#cn-accept-banner"
|
|
) as HTMLButtonElement | null,
|
|
denyAllBtn = document.querySelector(
|
|
"button#cn-deny-banner"
|
|
) as HTMLButtonElement | null,
|
|
customizeBtn = document.querySelector(
|
|
"button#cn-settings-banner"
|
|
) as HTMLButtonElement | null,
|
|
cookiesUnorderedList = document.querySelector(
|
|
"ul#cn-modal-list"
|
|
) as HTMLUListElement | null,
|
|
modalWrapper = document.querySelector(
|
|
"div.cn-modal-wrapper"
|
|
) as HTMLDivElement,
|
|
template = document.querySelector(
|
|
"div#cn-li-template"
|
|
) as HTMLDivElement | null;
|
|
if (!cookieContainer) {
|
|
throw new Error("Cannot find div#cn-wrapper");
|
|
}
|
|
if (!cookieShowModalBtn) {
|
|
throw new Error("Cannot find 'button#cn-cookie-btn' element");
|
|
}
|
|
if (!cookieCloseModalBtn) {
|
|
throw new Error("Cannot find 'button#cn-close-modal' element");
|
|
}
|
|
if (!bannerContainer) {
|
|
throw new Error("Cannot find 'div#cn-banner' element");
|
|
}
|
|
if (!bannerContent) {
|
|
throw new Error("Cannot find 'div.cn-banner-content' element");
|
|
}
|
|
if (!modalContainer) {
|
|
throw new Error("Cannot find 'div.cn-modal-container' element");
|
|
}
|
|
if (!acceptAllBtn) {
|
|
throw new Error("Cannot find 'button#cn-accept-banner' element");
|
|
}
|
|
if (!denyAllBtn) {
|
|
throw new Error("Cannot find 'button#cn-deny-banner' element");
|
|
}
|
|
if (!customizeBtn) {
|
|
throw new Error("Cannot find 'button.cn-settings-banner' element");
|
|
}
|
|
if (!cookiesUnorderedList) {
|
|
throw new Error("Cannot find 'ul#cn-modal-list' element");
|
|
}
|
|
if (!modalWrapper) {
|
|
throw new Error("Cannot find 'div.cn-modal-wrapper' element");
|
|
}
|
|
if (!template) {
|
|
throw new Error("Cannot find 'div#cn-li-template' element");
|
|
}
|
|
|
|
this.cookieShowModalBtn = cookieShowModalBtn;
|
|
this.cookieCloseModalBtn = cookieCloseModalBtn;
|
|
this.cookieLinkShowCustomizeModalList =
|
|
cookieLinkShowCustomizeModalPageList;
|
|
this.cookieContainer = cookieContainer;
|
|
this.bannerContainer = bannerContainer;
|
|
this.bannerContent = bannerContent;
|
|
this.cookiesUnorderedList = cookiesUnorderedList;
|
|
this.modalWrapper = modalWrapper;
|
|
this.modalContainer = modalContainer;
|
|
this.acceptAllBtn = acceptAllBtn;
|
|
this.denyAllBtn = denyAllBtn;
|
|
this.customizeBtn = customizeBtn;
|
|
|
|
this.bannerDetailsAreExpanded = false;
|
|
this.template = template;
|
|
const memoryTemplate = this.template.cloneNode(true) as HTMLDivElement;
|
|
this.template.remove();
|
|
this.template = memoryTemplate;
|
|
|
|
this.restrictCloseModal = true;
|
|
|
|
// * Setup Events
|
|
this.addEventPrevent(this.modalBg, "click", this.closeCookieModal);
|
|
this.addEventPrevent(
|
|
this.cookieCloseModalBtn,
|
|
"click",
|
|
this.closeCookieModal
|
|
);
|
|
this.addEventPrevent(this.acceptAllBtn, "click", this.acceptAllCookies);
|
|
this.addEventPrevent(this.denyAllBtn, "click", this.denyAllCookies);
|
|
this.addEventPrevent(
|
|
this.customizeBtn,
|
|
"click",
|
|
this.toggleCookieModalPages
|
|
);
|
|
this.addEventPrevent(
|
|
this.cookieShowModalBtn,
|
|
"click",
|
|
this.toggleCookieModal
|
|
);
|
|
this.cookieLinkShowCustomizeModalList.forEach((cookieLink) => {
|
|
this.addEventPrevent(
|
|
cookieLink,
|
|
"click",
|
|
this.openCookieModalToCookiePage
|
|
);
|
|
this.addEventKbdValidate(cookieLink, this.openCookieModalToCookiePage);
|
|
});
|
|
|
|
// * Setup CN Cookie
|
|
if (!this.cookiesMatchesConfig() || this.cookiesOlderThan(new Date())) {
|
|
// * Setup Browser Cookie if none.
|
|
this.resetBrowserCookie();
|
|
this.openCookieModalToBannerPage();
|
|
}
|
|
|
|
this.updateRestrictCloseModal();
|
|
|
|
// * Cookie should have been created from here.
|
|
Object.entries(this.getCookieStatusList()).forEach(([cookieName, cookieStatus]) => {
|
|
if (cookieStatus === CookieStatus.wait) {
|
|
// * Open modal if any cookie config is not set.
|
|
this.openCookieModalToBannerPage();
|
|
return;
|
|
}
|
|
if (!this.config[cookieName]) {
|
|
// * Ignore missing config
|
|
return;
|
|
}
|
|
if (cookieStatus === CookieStatus.true) {
|
|
// * Exec script part for accepted cookie.
|
|
this.config[cookieName].script();
|
|
return;
|
|
}
|
|
if (this.config[cookieName].fallback != null) {
|
|
// * Exec fallback part for accepted cookie if provided.
|
|
this.config[cookieName].fallback();
|
|
}
|
|
});
|
|
}
|
|
|
|
// COOKIES
|
|
|
|
/**
|
|
* Update restrict for closing cookie modal
|
|
*/
|
|
updateRestrictCloseModal() {
|
|
this.restrictCloseModal = (Object.values(this.getCookieStatusList()).includes(CookieStatus.wait)) ? true : false;
|
|
(this.restrictCloseModal) ? this.cookieCloseModalBtn.classList.add("d-none") : this.cookieCloseModalBtn.classList.remove("d-none");
|
|
(this.restrictCloseModal) ? this.modalBg.classList.add("cn-modal-bg-active") : this.modalBg.classList.remove("cn-modal-bg-active");
|
|
}
|
|
|
|
/**
|
|
* Get cookies expiration date or null if not defined alread
|
|
*/
|
|
readCookiesExpirationDate(): Date | null {
|
|
const cookies = this.getBrowserCookieValue() ?? "",
|
|
expirationDateString = cookies.split(",").find((cookieString) =>
|
|
cookieString.startsWith(this.cookieExpirationName));
|
|
if (!expirationDateString) {
|
|
return null;
|
|
}
|
|
try {
|
|
const epochExpiration = Number.parseInt(expirationDateString.replace(`${this.cookieExpirationName}=`, ""));
|
|
if (isNaN(epochExpiration)) {
|
|
return null;
|
|
}
|
|
return new Date(epochExpiration);
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if the cookies date are older than a specified date
|
|
|
|
* @param date The limit date for cookies
|
|
*/
|
|
cookiesOlderThan(date: Date): boolean {
|
|
return this.cookieExpirationDate.getTime() < date.getTime();
|
|
}
|
|
|
|
/**
|
|
* Check if the actual cookie matches the config
|
|
*/
|
|
cookiesMatchesConfig(): boolean {
|
|
const cookies = this.getCookieStatusList();
|
|
return Object.keys(cookies).length === Object.keys(this.config).length;
|
|
}
|
|
|
|
/**
|
|
* Accept a all cookies
|
|
*/
|
|
acceptAllCookies() {
|
|
Object.keys(this.config).forEach((cookieName) => this.acceptCookie(cookieName));
|
|
this.closeCookieModal();
|
|
}
|
|
|
|
/**
|
|
* Accept a specific cookie
|
|
*/
|
|
acceptCookie(cookieName: string) {
|
|
const cookieOptions = this.getCookie(cookieName);
|
|
this.setCookieStatus(cookieName, CookieStatus.true, cookieOptions);
|
|
this.cookieContainer.querySelector(`#${cookieName}-accept`)?.classList?.add("active");
|
|
this.cookieContainer.querySelector(`#${cookieName}-deny`)?.classList?.remove("active");
|
|
}
|
|
|
|
/**
|
|
* Deny a all cookies
|
|
*/
|
|
denyAllCookies() {
|
|
Object.keys(this.config).forEach((cookieName) => this.denyCookie(cookieName));
|
|
this.closeCookieModal();
|
|
}
|
|
|
|
/**
|
|
* Deny a specific cookie
|
|
*/
|
|
denyCookie(cookieName: string) {
|
|
const cookieOptions = this.getCookie(cookieName);
|
|
this.setCookieStatus(cookieName, CookieStatus.false, cookieOptions);
|
|
this.cookieContainer.querySelector(`#${cookieName}-deny`)?.classList?.add("active");
|
|
this.cookieContainer.querySelector(`#${cookieName}-accept`)?.classList?.remove("active");
|
|
}
|
|
|
|
/**
|
|
* Deny all waiting cookies
|
|
*/
|
|
denyAllWaitingCookies() {
|
|
Object.entries(this.getCookieStatusList()).forEach(([cookieName, cookieStatus]) => {
|
|
if (cookieStatus === CookieStatus.wait) {
|
|
this.setBrowserCookieStatus(cookieName, CookieStatus.false);
|
|
return;
|
|
}
|
|
});
|
|
this.updateRestrictCloseModal();
|
|
}
|
|
|
|
/**
|
|
* Get a specific cookie config
|
|
*/
|
|
getCookie(cookieName: string): CookieOptions {
|
|
const cookieOptions = Object.entries(this.config).find((entry) => entry[0] === cookieName)?.[1];
|
|
if (cookieOptions === undefined) {
|
|
throw new Error(`Cookie options ${cookieName} not found in list`);
|
|
}
|
|
return cookieOptions;
|
|
}
|
|
|
|
/**
|
|
* Fetch the cookie status list
|
|
*
|
|
* @returns the cookie status list
|
|
*/
|
|
getCookieStatusList(): CookieStatusList {
|
|
const cookies = this.getBrowserCookieValue(),
|
|
statusList: CookieStatusList = {};
|
|
if (cookies === undefined) {
|
|
return statusList;
|
|
}
|
|
/** specificCookieString contains youtube=true like */
|
|
cookies.split(",")
|
|
// * Ignore timestamp
|
|
.filter((specificCookieString) => !specificCookieString.startsWith(this.cookieExpirationName))
|
|
.forEach((specificCookieString) => {
|
|
const values = specificCookieString.split("=");
|
|
statusList[values[0]] = values[1] as CookieStatus;
|
|
});
|
|
return statusList;
|
|
}
|
|
|
|
/**
|
|
* Check has cookie status
|
|
*
|
|
* @param key
|
|
* @param value
|
|
*/
|
|
containsCookieStatus(key: string): boolean {
|
|
const statusList: CookieStatusList = this.getCookieStatusList();
|
|
return (
|
|
Object.keys(statusList).find((cookieName) => cookieName === key) !==
|
|
undefined
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Set the cookie value
|
|
*
|
|
* @param cookieName
|
|
* @param status
|
|
* @param cookieOption
|
|
*/
|
|
setCookieStatus(
|
|
cookieName: string,
|
|
status: CookieStatus,
|
|
cookieOption: CookieOptions
|
|
) {
|
|
if (status === CookieStatus.true) {
|
|
this.addCookieStatus(cookieName, cookieOption);
|
|
return;
|
|
}
|
|
if (status === CookieStatus.false) {
|
|
this.removeCookieStatus(cookieName, cookieOption);
|
|
return;
|
|
}
|
|
this.setBrowserCookieStatus(cookieName, CookieStatus.wait);
|
|
}
|
|
|
|
/**
|
|
* Add a specific cookie and
|
|
* change his status to true
|
|
*
|
|
* @param cookieName
|
|
* @param cookieOption
|
|
*/
|
|
addCookieStatus(cookieName: string, cookieOption: CookieOptions) {
|
|
if (cookieOption.script) {
|
|
cookieOption.script();
|
|
}
|
|
this.setBrowserCookieStatus(cookieName, CookieStatus.true);
|
|
}
|
|
|
|
/**
|
|
* Remove a specific cookie and
|
|
* change his status to false
|
|
*
|
|
* @param cookieName
|
|
* @param cookieOption
|
|
*/
|
|
removeCookieStatus(cookieName: string, cookieOption: CookieOptions) {
|
|
const cookieNameElement = document.getElementById(cookieName);
|
|
if (cookieNameElement) {
|
|
cookieNameElement.remove();
|
|
}
|
|
if (cookieOption.fallback) {
|
|
cookieOption.fallback();
|
|
}
|
|
this.setBrowserCookieStatus(cookieName, CookieStatus.false);
|
|
}
|
|
|
|
/**
|
|
* Change the status of a cookie
|
|
*
|
|
* @param cookieName
|
|
* @param status
|
|
*/
|
|
setBrowserCookieStatus(cookieName: string, status: CookieStatus) {
|
|
const statusList: CookieStatusList = this.getCookieStatusList();
|
|
// * Force refresh
|
|
this.deleteBrowserCookie();
|
|
statusList[cookieName] = status;
|
|
this.saveBrowserCookie(statusList);
|
|
this.updateRestrictCloseModal();
|
|
}
|
|
|
|
/**
|
|
* Has CN cokie setup ?
|
|
* @returns true if the cookie is setup
|
|
*/
|
|
hasBrowserCookie(): boolean {
|
|
return Boolean(this.getBrowserCookieValue());
|
|
}
|
|
|
|
/**
|
|
* Get the Ldcweb cookie
|
|
*
|
|
* @returns A string like analytics=true,youtube=true or undefined
|
|
*/
|
|
getBrowserCookieValue() {
|
|
const matches = document.cookie.match(
|
|
new RegExp(
|
|
"(?:^|; )" +
|
|
this.browserCookieName.replace(
|
|
/([.$?*|{}()[\]\\/+^])/g,
|
|
"\\$1"
|
|
) +
|
|
"=([^;]*)"
|
|
)
|
|
);
|
|
return matches ? decodeURIComponent(matches[1]) : undefined;
|
|
}
|
|
|
|
/**
|
|
* Save cookie content on web browser
|
|
*
|
|
* @param list
|
|
*/
|
|
saveBrowserCookie(statusList: CookieStatusList) {
|
|
const date = new Date(),
|
|
content = Object.entries(statusList)
|
|
.map(([cookieName, cookieStatus]) => {
|
|
return `${cookieName}=${cookieStatus}`;
|
|
});
|
|
// * Update expiration date
|
|
this.cookieExpirationDate = new Date(date.setMonth(date.getMonth() + this.cookieTimeLife));
|
|
// * Append expiration date
|
|
content.push(`${this.cookieExpirationName}=${this.cookieExpirationDate.getTime()}`);
|
|
document.cookie = `${this.browserCookieName}=${content.join(",")};path=/${
|
|
location.hostname ? `;domain=${location.hostname}` : ""
|
|
};expires=${this.cookieExpirationDate.toUTCString()};${this.getCookiePoliciesAsString()}`;
|
|
}
|
|
|
|
/**
|
|
* Set an older expire parameter to a specific cookie
|
|
*/
|
|
deleteBrowserCookie() {
|
|
if (this.hasBrowserCookie()) {
|
|
// * Suppression du cookie et de sa version precedé d'un point
|
|
document.cookie = `${this.browserCookieName}=;path=/${
|
|
location.hostname ? `;domain=.${location.hostname}` : ""
|
|
};expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
|
|
document.cookie = `${this.browserCookieName}=;path=/${
|
|
location.hostname ? `;domain=${location.hostname}` : ""
|
|
};expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
|
|
}
|
|
}
|
|
|
|
getCookiePoliciesAsString(): string {
|
|
return Object.entries(this.cookiePolicies)
|
|
.map(([policyName, policyValue]) => `${policyName}=${policyValue}`)
|
|
.join(";");
|
|
}
|
|
|
|
/**
|
|
* Reset Ldc Web CN cookie
|
|
*/
|
|
resetBrowserCookie() {
|
|
// * Create Cookie as It does not exists.
|
|
const newCookieList: CookieStatusList = {};
|
|
Object.entries(this.config).forEach(
|
|
([cookieName]) => (newCookieList[cookieName] = CookieStatus.wait)
|
|
);
|
|
this.saveBrowserCookie(newCookieList);
|
|
}
|
|
|
|
// LISTENERS
|
|
|
|
/**
|
|
* Add event on a specific element
|
|
*
|
|
* @param element
|
|
* @param evnt
|
|
* @param funct
|
|
* @returns
|
|
*/
|
|
addEvent(element: HTMLElement, evnt: string, funct: (e: Event) => void) {
|
|
return element.addEventListener(evnt, funct.bind(this), false);
|
|
}
|
|
|
|
/**
|
|
* Add event on a specific element and prevent default
|
|
*
|
|
* @param element
|
|
* @param evnt
|
|
* @param funct
|
|
* @returns
|
|
*/
|
|
addEventPrevent(
|
|
element: HTMLElement,
|
|
evnt: string,
|
|
funct: (e: Event) => void
|
|
) {
|
|
return element.addEventListener(
|
|
evnt,
|
|
(e) => {
|
|
e.preventDefault();
|
|
funct.call(this, e);
|
|
},
|
|
false
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Add event on a specific element
|
|
*
|
|
* @param element
|
|
* @param evnt
|
|
* @param funct
|
|
* @returns
|
|
*/
|
|
addEventKbdValidate(element: HTMLElement, funct: (e: Event) => void) {
|
|
return this.addEvent(element, "keydown", (e: Event) => {
|
|
const kbdEvt = e as KeyboardEvent;
|
|
if (
|
|
kbdEvt.key == " " ||
|
|
kbdEvt.code == "Space" ||
|
|
kbdEvt.keyCode == 32 ||
|
|
kbdEvt.key == "Enter" ||
|
|
kbdEvt.code == "Enter" ||
|
|
kbdEvt.keyCode == 13
|
|
) {
|
|
funct.call(this, kbdEvt);
|
|
e.preventDefault();
|
|
}
|
|
});
|
|
}
|
|
|
|
// COOKIE MODAL
|
|
|
|
/**
|
|
* Show or hide Cookie modal
|
|
* @returns
|
|
*/
|
|
toggleCookieModal() {
|
|
if (!document.body.classList.contains("cn-open")) {
|
|
this.openCookieModalToBannerPage();
|
|
return;
|
|
}
|
|
this.closeCookieModal();
|
|
}
|
|
|
|
/**
|
|
* Open the cookies modal to the banner page
|
|
*/
|
|
openCookieModalToBannerPage() {
|
|
this.openCookieModalToCookiePage();
|
|
this.toggleCookieModalPages();
|
|
}
|
|
|
|
/**
|
|
* Open the cookies modal to the cookies page
|
|
*/
|
|
openCookieModalToCookiePage() {
|
|
document.body.classList.add("cn-open");
|
|
this.resetCookieModal();
|
|
this.bannerContent.classList.add("d-none");
|
|
this.modalContainer.classList.remove("d-none");
|
|
this.bannerContainer.classList.add("cn-modal-page");
|
|
|
|
// Scroll to the top of the cookies list
|
|
this.modalWrapper.scrollTop = 0;
|
|
this.setupCookieModalList();
|
|
this.setTabIndex();
|
|
}
|
|
|
|
/**
|
|
* Hide the cookies modal
|
|
*/
|
|
closeCookieModal() {
|
|
this.updateRestrictCloseModal();
|
|
if (!this.restrictCloseModal) {
|
|
document.body.classList.remove("cn-open");
|
|
if (!this.isOnBannerPage()) {
|
|
// * Make toggle page to banner on closing
|
|
this.bannerContent.classList.toggle("d-none");
|
|
this.modalContainer.classList.toggle("d-none");
|
|
this.bannerContainer.classList.remove("cn-modal-page");
|
|
}
|
|
this.unsetTabIndex();
|
|
this.setupCookieModalList();
|
|
} else {
|
|
throw new Error("You need to accept or refuse all cookies");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Setup tab indexes on visible elements
|
|
*/
|
|
setTabIndex() {
|
|
// * Prevent focus html tag
|
|
document.documentElement.setAttribute("tabindex", "-1");
|
|
setTimeout(() => {
|
|
const focusableSelector =
|
|
"a[href], button, [role=\"button\"], input, embed, object, iframe, textarea, select, details, [tabindex]:not([tabindex=\"-1\"])";
|
|
|
|
// * Only focusable elements
|
|
[...document.body.querySelectorAll(focusableSelector)]
|
|
.filter(
|
|
(el) =>
|
|
!el.hasAttribute("disabled") &&
|
|
!el.getAttribute("aria-hidden")
|
|
)
|
|
.forEach((el) => {
|
|
(el as HTMLElement).setAttribute("tabindex", "-1");
|
|
(el as HTMLElement).classList.add("cn-disabled");
|
|
});
|
|
[...this.cookieContainer.querySelectorAll(focusableSelector)]
|
|
.filter(
|
|
(el) =>
|
|
!el.hasAttribute("disabled") &&
|
|
!el.getAttribute("aria-hidden") &&
|
|
// * Only visible elements
|
|
!!(
|
|
(el as HTMLElement).offsetWidth ||
|
|
(el as HTMLElement).offsetHeight ||
|
|
el.getClientRects().length
|
|
)
|
|
)
|
|
.forEach((el) => {
|
|
(el as HTMLElement).setAttribute("tabindex", "0");
|
|
(el as HTMLElement).classList.remove("cn-disabled");
|
|
});
|
|
this.cookieShowModalBtn.setAttribute("tabindex", "-1");
|
|
}, 500);
|
|
}
|
|
|
|
/**
|
|
* Undo setup tab indexes on visible elements
|
|
*/
|
|
unsetTabIndex() {
|
|
document.documentElement.setAttribute("tabindex", "0");
|
|
setTimeout(() => {
|
|
document.body.querySelectorAll(".cn-disabled").forEach((el) => {
|
|
(el as HTMLElement).setAttribute("tabindex", "0");
|
|
(el as HTMLElement).classList.remove("cn-disabled");
|
|
});
|
|
this.cookieShowModalBtn.setAttribute("tabindex", "0");
|
|
}, 500);
|
|
}
|
|
|
|
/**
|
|
* Reset the modal view state
|
|
*/
|
|
resetCookieModal() {
|
|
this.bannerContent.classList.remove("d-none");
|
|
this.modalContainer.classList.add("d-none");
|
|
this.bannerContainer.classList.remove("cn-modal-page");
|
|
if (!this.customizeBtn.dataset.openModal) {
|
|
throw new Error("Cannot find attribut data-open-modal");
|
|
}
|
|
this.customizeBtn.innerHTML = this.customizeBtn.dataset.openModal;
|
|
}
|
|
|
|
/**
|
|
* Set the cookies list in the modal
|
|
*/
|
|
setupCookieModalList() {
|
|
this.cookiesUnorderedList.innerHTML = "";
|
|
Object.entries(this.config).forEach(([cookieName, cookieConfig]) => {
|
|
const cookie = document.createElement("li");
|
|
const childItem = this.template.children.item(
|
|
0
|
|
) as HTMLDivElement | null;
|
|
if (childItem === null) {
|
|
throw new Error("Cannot find child item");
|
|
}
|
|
cookie.append(childItem.cloneNode(true));
|
|
const name = cookie.querySelector(
|
|
"span.cn-cookie-name"
|
|
) as HTMLSpanElement;
|
|
const description = cookie.querySelector(
|
|
"span.cn-cookie-description"
|
|
) as HTMLSpanElement;
|
|
const link = cookie.querySelector(
|
|
".cn-cookie-link"
|
|
) as HTMLElement;
|
|
const acceptInput = cookie.querySelector(
|
|
".accept > input"
|
|
) as HTMLInputElement;
|
|
const denyInput = cookie.querySelector(
|
|
".deny > input"
|
|
) as HTMLInputElement;
|
|
const acceptLabel = cookie.querySelector(
|
|
".accept > label"
|
|
) as HTMLLabelElement;
|
|
const denyLabel = cookie.querySelector(
|
|
".deny > label"
|
|
) as HTMLLabelElement;
|
|
|
|
name.textContent = cookieConfig.name;
|
|
description.textContent = cookieConfig.description;
|
|
link.textContent = cookieConfig.labelLink;
|
|
link.setAttribute("href", cookieConfig.link);
|
|
acceptInput.id = cookieName + "-accept";
|
|
acceptInput.name = cookieName;
|
|
acceptInput.dataset.name = cookieName;
|
|
acceptLabel.htmlFor = cookieName + "-accept";
|
|
acceptLabel.textContent = `${acceptLabel.textContent} ${cookieName}`;
|
|
acceptLabel.title = `${acceptLabel.textContent} ${cookieName}`;
|
|
denyInput.id = cookieName + "-deny";
|
|
denyInput.name = cookieName;
|
|
denyInput.dataset.name = cookieName;
|
|
denyLabel.htmlFor = cookieName + "-deny";
|
|
denyLabel.textContent = `${denyLabel.textContent} ${cookieName}`;
|
|
denyLabel.title = `${denyLabel.textContent} ${cookieName}`;
|
|
|
|
const acceptFn = () => {
|
|
acceptLabel.classList.add("active");
|
|
denyLabel.classList.remove("active");
|
|
this.setCookieStatus(
|
|
cookieName,
|
|
CookieStatus.true,
|
|
cookieConfig
|
|
);
|
|
},
|
|
denyFn = () => {
|
|
acceptLabel.classList.remove("active");
|
|
denyLabel.classList.add("active");
|
|
this.setCookieStatus(
|
|
cookieName,
|
|
CookieStatus.false,
|
|
cookieConfig
|
|
);
|
|
};
|
|
this.addEvent(acceptInput, "click", acceptFn);
|
|
this.addEventKbdValidate(acceptLabel, acceptFn);
|
|
this.addEvent(denyInput, "click", denyFn);
|
|
this.addEventKbdValidate(denyLabel, denyFn);
|
|
|
|
this.cookiesUnorderedList.append(cookie);
|
|
|
|
if (!this.hasBrowserCookie()) {
|
|
// * Ldcweb cookie is not set.
|
|
return;
|
|
}
|
|
// * Setup list status state
|
|
Object.entries(this.getCookieStatusList()).forEach(
|
|
([ckName, cookieStatus]) => {
|
|
if (ckName === cookieName) {
|
|
acceptLabel.classList.remove("active");
|
|
denyLabel.classList.remove("active");
|
|
switch (cookieStatus) {
|
|
case CookieStatus.true:
|
|
acceptLabel.classList.add("active");
|
|
break;
|
|
case CookieStatus.false:
|
|
denyLabel.classList.add("active");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Toggle Cookie modal pages
|
|
*/
|
|
toggleCookieModalPages() {
|
|
this.bannerContent.classList.toggle("d-none");
|
|
this.modalContainer.classList.toggle("d-none");
|
|
this.bannerContainer.classList.toggle("cn-modal-page");
|
|
if (!this.isOnBannerPage()) {
|
|
this.bannerContainer.classList.add("cn-modal-page");
|
|
this.denyAllWaitingCookies();
|
|
}
|
|
this.setTabIndex();
|
|
this.setupCookieModalList();
|
|
this.togglePersonalizeBtn();
|
|
}
|
|
|
|
/**
|
|
* Toggle customize btn
|
|
*/
|
|
togglePersonalizeBtn() {
|
|
const customizeBtnBannerDataset = this.customizeBtn.dataset;
|
|
this.modalWrapper.scrollTop = 0;
|
|
if (this.bannerContent.classList.contains("d-none")) {
|
|
this.customizeBtn.innerHTML =
|
|
customizeBtnBannerDataset.openModal as string;
|
|
this.customizeBtn.title = customizeBtnBannerDataset.openModal as string;
|
|
} else {
|
|
this.customizeBtn.innerHTML =
|
|
customizeBtnBannerDataset.closeModal as string;
|
|
this.customizeBtn.title = customizeBtnBannerDataset.closeModal as string;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the actual page: banner or modal (with cookie choices)
|
|
*/
|
|
isOnBannerPage(): boolean {
|
|
return this.modalContainer.classList.contains("d-none");
|
|
}
|
|
}
|
|
|
|
enum CookieStatus {
|
|
true = "true",
|
|
false = "false",
|
|
wait = "wait",
|
|
}
|
|
|
|
type CookieStatusList = Record<string, keyof typeof CookieStatus>;
|
|
|
|
type CookiePolicy = {
|
|
sameSite: CookiePolicySameSite;
|
|
secure: string;
|
|
};
|
|
|
|
enum CookiePolicySameSite {
|
|
lax = "lax",
|
|
strict = "strict",
|
|
none = "none",
|
|
}
|
|
|
|
export type CookieOptions = {
|
|
name: string;
|
|
description: string;
|
|
link: string;
|
|
labelLink: string;
|
|
cookies: string[];
|
|
/**
|
|
* Fonction qui sert à initialiser la fonctionnalité
|
|
* conduisant à la génération du cookie
|
|
* @return void
|
|
*/
|
|
script: () => void;
|
|
/**
|
|
* Fonction qui sert à informer l'utilisateur
|
|
* sur chaque élèment que le cookie
|
|
* associé à la fonctionnalité
|
|
* est désactivé et propose un lien sur
|
|
* cookie notice pour l'activer
|
|
* @return void
|
|
*/
|
|
fallback: () => void | null;
|
|
};
|