370 lines
13 KiB
JavaScript
Vendored
370 lines
13 KiB
JavaScript
Vendored
/**
|
|
* LDC Service Web
|
|
* Version 1.3
|
|
*/
|
|
|
|
class LdcwebCn {
|
|
constructor(config) {
|
|
this.config = config;
|
|
this.cookieContainer = document.getElementById("ldcweb-cn");
|
|
this.bannerContainer = document.getElementById("ldcweb-banner");
|
|
this.modalContainer = document.getElementById("ldcweb-cn-modal");
|
|
this.modalCloseBtn = document.getElementById("close-link");
|
|
this.acceptAllBtnBanner = document.getElementById(
|
|
"ldcweb-cn-accept-banner"
|
|
);
|
|
this.denyAllBtnBanner = document.getElementById("ldcweb-cn-deny-banner");
|
|
this.acceptAllBtn = document.getElementById("ldcweb-cn-accept");
|
|
this.denyAllBtn = document.getElementById("ldcweb-cn-deny");
|
|
this.personalizeBtn = document.getElementsByClassName(
|
|
"ldcweb-cn-personalize"
|
|
);
|
|
this.changeBtn = document.getElementsByClassName("ldcweb-cn-change");
|
|
this.cookiesList = document.getElementById("ldcweb-cn-list");
|
|
this.cookieLink = document.getElementsByClassName("ldcweb-cn-cookie-link");
|
|
this.template = document.getElementById("template").cloneNode(true);
|
|
document.getElementById("template").remove();
|
|
|
|
let cookies = this.getLDCWebCookie("ldcwebcn-init");
|
|
if (cookies === undefined) {
|
|
let newCookie = [];
|
|
|
|
for (const [key, value] of Object.entries(this.config)) {
|
|
newCookie.push(key + "=wait");
|
|
}
|
|
|
|
this.saveLDCWebCookie(newCookie);
|
|
this.cookieContainer.classList.add("open-banner");
|
|
} else {
|
|
let test = cookies.split(",");
|
|
test.forEach((elm) => {
|
|
let test = elm.split("=");
|
|
if (test[1] === "wait") {
|
|
this.cookieContainer.classList.add("open-banner");
|
|
}
|
|
});
|
|
}
|
|
this.modalCloseBtn.addEventListener(
|
|
"click",
|
|
function (e) {
|
|
e.preventDefault();
|
|
let cookies = this.getLDCWebCookie("ldcwebcn-init");
|
|
let test = cookies.split(",");
|
|
let bIsCompleted = true;
|
|
for (let item of test) {
|
|
if (item.indexOf("=wait") !== -1) {
|
|
bIsCompleted = false;
|
|
break;
|
|
}
|
|
}
|
|
if (bIsCompleted) {
|
|
this.cookieContainer.classList.remove("open-modal");
|
|
this.cookieContainer.classList.remove("open-banner");
|
|
} else {
|
|
alert(
|
|
"Vous devez renseigner votre choix pour tous les cookies, svp."
|
|
);
|
|
}
|
|
}.bind(this)
|
|
);
|
|
let accept = function (e) {
|
|
e.preventDefault();
|
|
for (const [key, value] of Object.entries(this.config)) {
|
|
this.addCookie(key, value);
|
|
}
|
|
for (let acceptLabel of this.cookieContainer.querySelectorAll(
|
|
".accept > label"
|
|
)) {
|
|
acceptLabel.classList.add("active");
|
|
}
|
|
for (let denyLabel of this.cookieContainer.querySelectorAll(
|
|
".deny > label"
|
|
)) {
|
|
denyLabel.classList.remove("active");
|
|
}
|
|
this.updateAllCookiesButtonsActiveStatus();
|
|
this.cookieContainer.classList.remove("open-modal");
|
|
this.cookieContainer.classList.remove("open-banner");
|
|
}.bind(this);
|
|
this.acceptAllBtnBanner.addEventListener("click", accept);
|
|
this.acceptAllBtn.addEventListener("click", accept);
|
|
|
|
let deny = function (e) {
|
|
e.preventDefault();
|
|
for (const [key, value] of Object.entries(this.config)) {
|
|
this.removeCookie(key, value);
|
|
}
|
|
for (let acceptLabel of this.cookieContainer.querySelectorAll(
|
|
".accept > label"
|
|
)) {
|
|
acceptLabel.classList.remove("active");
|
|
}
|
|
for (let denyLabel of this.cookieContainer.querySelectorAll(
|
|
".deny > label"
|
|
)) {
|
|
denyLabel.classList.add("active");
|
|
}
|
|
this.updateAllCookiesButtonsActiveStatus();
|
|
this.cookieContainer.classList.remove("open-modal");
|
|
this.cookieContainer.classList.remove("open-banner");
|
|
}.bind(this);
|
|
this.denyAllBtnBanner.addEventListener("click", deny);
|
|
this.denyAllBtn.addEventListener("click", deny);
|
|
|
|
for (let item of this.cookieLink) {
|
|
this.addEvent(
|
|
item,
|
|
"click",
|
|
function (e) {
|
|
e.preventDefault();
|
|
this.openModal();
|
|
}.bind(this)
|
|
);
|
|
}
|
|
|
|
for (let item of this.personalizeBtn) {
|
|
this.addEvent(
|
|
item,
|
|
"click",
|
|
function (e) {
|
|
e.preventDefault();
|
|
this.setCookieList();
|
|
}.bind(this)
|
|
);
|
|
}
|
|
|
|
this.setListenerOnChange();
|
|
|
|
let cookie = this.getLDCWebCookie("ldcwebcn-init");
|
|
let test = cookie.split(",");
|
|
test.forEach((elm) => {
|
|
let test = elm.split("=");
|
|
if (test[1] === "true") {
|
|
this.config[test[0]].script();
|
|
} else {
|
|
if (this.config[test[0]].fallback !== undefined) {
|
|
this.config[test[0]].fallback();
|
|
}
|
|
}
|
|
});
|
|
|
|
this.updateAllCookiesButtonsActiveStatus();
|
|
}
|
|
|
|
setCookieList() {
|
|
this.cookiesList.innerHTML = "";
|
|
let cookiesSite = this.getLDCWebCookie("ldcwebcn-init");
|
|
let acceptInputEvent = function (acceptLabel, denyLabel, key, value) {
|
|
return function (e) {
|
|
acceptLabel.classList.add("active");
|
|
denyLabel.classList.remove("active");
|
|
this.updateAllCookiesButtonsActiveStatus();
|
|
this.addCookie(key, value);
|
|
}.bind(this, [acceptLabel, denyLabel, key, value]);
|
|
};
|
|
let denyInputEvent = function (acceptLabel, denyLabel, key, value) {
|
|
return function (e) {
|
|
acceptLabel.classList.remove("active");
|
|
denyLabel.classList.add("active");
|
|
this.updateAllCookiesButtonsActiveStatus();
|
|
this.removeCookie(key, value);
|
|
}.bind(this, [acceptLabel, denyLabel, key, value]);
|
|
};
|
|
|
|
for (const [key, value] of Object.entries(this.config)) {
|
|
let cookie = document.createElement("li");
|
|
cookie.append(this.template.children.item(0).cloneNode(true));
|
|
let name = cookie.querySelector("span.ldcweb-cn-name");
|
|
let description = cookie.querySelector("span.ldcweb-cn-description");
|
|
let link = cookie.querySelector("a.ldcweb-cn-link");
|
|
let acceptInput = cookie.querySelector(".accept > input");
|
|
let denyInput = cookie.querySelector(".deny > input");
|
|
let acceptLabel = cookie.querySelector(".accept > label");
|
|
let denyLabel = cookie.querySelector(".deny > label");
|
|
|
|
name.textContent = value.name;
|
|
description.textContent = value.description;
|
|
link.textContent = value.labelLink;
|
|
link.setAttribute("href", value.link);
|
|
acceptInput.id = key + "1";
|
|
acceptInput.name = key;
|
|
acceptInput.dataset.name = key;
|
|
acceptLabel.htmlFor = key + "1";
|
|
denyInput.id = key + "2";
|
|
denyInput.name = key;
|
|
denyInput.dataset.name = key;
|
|
denyLabel.htmlFor = key + "2";
|
|
|
|
acceptInput.addEventListener(
|
|
"click",
|
|
acceptInputEvent.apply(this, [acceptLabel, denyLabel, key, value])
|
|
);
|
|
denyInput.addEventListener(
|
|
"click",
|
|
denyInputEvent.apply(this, [acceptLabel, denyLabel, key, value])
|
|
);
|
|
|
|
this.cookiesList.append(cookie);
|
|
|
|
let cookiesArray = cookiesSite.split(",");
|
|
for (const cookieEl of cookiesArray) {
|
|
let name = cookieEl.slice(0, cookieEl.indexOf("="));
|
|
let status = cookieEl.slice(cookieEl.indexOf("=") + 1);
|
|
if (name === key) {
|
|
acceptLabel.classList.remove("active");
|
|
denyLabel.classList.remove("active");
|
|
switch (status) {
|
|
case "true":
|
|
acceptLabel.classList.add("active");
|
|
break;
|
|
case "false":
|
|
denyLabel.classList.add("active");
|
|
break;
|
|
}
|
|
}
|
|
if (status === "wait") {
|
|
this.cookieContainer.classList.add("open-banner");
|
|
}
|
|
}
|
|
}
|
|
|
|
this.cookieContainer.classList.add("open-modal");
|
|
this.setListenerOnChange();
|
|
}
|
|
|
|
updateAllCookiesButtonsActiveStatus() {
|
|
let acceptsButtons =
|
|
this.cookieContainer.querySelectorAll("div.accept > label");
|
|
let deniesButtons =
|
|
this.cookieContainer.querySelectorAll("div.deny > label");
|
|
let activeAcceptsButtons = this.cookieContainer.querySelectorAll(
|
|
"div.accept > label.active"
|
|
);
|
|
let activeDeniesButtons = this.cookieContainer.querySelectorAll(
|
|
"div.deny > label.active"
|
|
);
|
|
|
|
this.acceptAllBtn.classList.remove("active");
|
|
this.denyAllBtn.classList.remove("active");
|
|
if (
|
|
acceptsButtons.length &&
|
|
acceptsButtons.length === activeAcceptsButtons.length
|
|
) {
|
|
this.acceptAllBtn.classList.add("active");
|
|
}
|
|
if (
|
|
deniesButtons.length &&
|
|
deniesButtons.length === activeDeniesButtons.length
|
|
) {
|
|
this.denyAllBtn.classList.add("active");
|
|
}
|
|
}
|
|
|
|
addCookie(key, value) {
|
|
if (value.script) {
|
|
value.script();
|
|
}
|
|
this.changeStatus(key, "true");
|
|
}
|
|
|
|
removeCookie(key, value) {
|
|
for (let cookieName of value.cookies) {
|
|
this.deleteBrowserCookie(cookieName, "/", "." + location.host);
|
|
}
|
|
if (document.getElementById(key)) {
|
|
document.getElementById(key).remove();
|
|
}
|
|
this.changeStatus(key, "false");
|
|
}
|
|
setListenerOnChange() {
|
|
this.changeBtn = document.getElementsByClassName("ldcweb-cn-change");
|
|
let itemClick = function (item) {
|
|
return function () {
|
|
this.changeStatus(
|
|
item.getAttribute("data-name"),
|
|
item.getAttribute("data-status")
|
|
);
|
|
}.bind(this);
|
|
};
|
|
for (let item of this.changeBtn) {
|
|
this.addEvent(item, "click", itemClick.bind(this, [item]));
|
|
}
|
|
}
|
|
|
|
changeStatus(name, status) {
|
|
let newCookie = [];
|
|
|
|
let cookie = this.getLDCWebCookie("ldcwebcn-init");
|
|
let test = cookie.split(",");
|
|
test.forEach((elm) => {
|
|
let test = elm.split("=");
|
|
if (test[0] === name) {
|
|
newCookie.push(name + "=" + status);
|
|
if (status === "false") {
|
|
this.config[test[0]].cookies.forEach((elm) => {
|
|
let cookies = this.getLDCWebCookie(elm);
|
|
if (cookies !== undefined) {
|
|
document.cookie =
|
|
elm +
|
|
"=; expires=Thu, 01 Jan 1970 00:00:00 UTC; domain=" +
|
|
document.domain +
|
|
"; path=/";
|
|
document.cookie =
|
|
elm +
|
|
"=; expires=Thu, 01 Jan 1970 00:00:00 UTC; domain=." +
|
|
document.domain +
|
|
"; path=/";
|
|
}
|
|
});
|
|
}
|
|
} else {
|
|
newCookie.push(elm);
|
|
}
|
|
});
|
|
|
|
this.saveLDCWebCookie(newCookie);
|
|
}
|
|
|
|
deleteBrowserCookie(name, path, domain) {
|
|
if (this.getLDCWebCookie(name)) {
|
|
document.cookie =
|
|
name +
|
|
"=" +
|
|
(path ? ";path=" + path : "") +
|
|
(domain ? ";domain=" + domain : "") +
|
|
";expires=Thu, 01 Jan 1970 00:00:01 GMT";
|
|
}
|
|
}
|
|
|
|
getLDCWebCookie(name) {
|
|
let matches = document.cookie.match(
|
|
new RegExp(
|
|
"(?:^|; )" +
|
|
name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, "\\$1") +
|
|
"=([^;]*)"
|
|
)
|
|
);
|
|
return matches ? decodeURIComponent(matches[1]) : undefined;
|
|
}
|
|
|
|
addEvent(element, evnt, funct) {
|
|
if (element.attachEvent) return element.attachEvent("on" + evnt, funct);
|
|
else return element.addEventListener(evnt, funct, false);
|
|
}
|
|
|
|
saveLDCWebCookie(content) {
|
|
let date = new Date();
|
|
let newDate = new Date(date.setMonth(date.getMonth() + 13));
|
|
document.cookie =
|
|
"ldcwebcn-init=" +
|
|
content +
|
|
"; expires=" +
|
|
newDate.toUTCString() +
|
|
"; path=/;";
|
|
}
|
|
|
|
openModal() {
|
|
this.setCookieList();
|
|
this.updateAllCookiesButtonsActiveStatus();
|
|
}
|
|
}
|