This repository has been archived on 2021-01-24. You can view files and clone it, but cannot push or open issues or pull requests.
grav-plugin-eucookiepolicy/assets/js/cookiechoices.js

121 lines
3.7 KiB
JavaScript
Executable File

(function(window) {
if (!!window.cookieChoices) {
return window.cookieChoices;
}
var document = window.document;
// IE8 does not support textContent, so we should fallback to innerText.
var supportsTextContent;
var cookieChoices = (function() {
var cookieName = 'displayCookieConsent';
var cookieConsentId = 'cookieChoiceInfo';
var dismissLinkId = 'cookieChoiceDismiss';
function _createHeaderElement(cookieText, dismissText, linkText, linkHref) {
_checkForTextContentSupport();
//var butterBarStyles = '';
var cookieConsentElement = document.createElement('div');
cookieConsentElement.id = cookieConsentId;
//cookieConsentElement.style.cssText = butterBarStyles;
cookieConsentElement.appendChild(_createConsentText(cookieText));
if (!!linkText && !!linkHref) {
cookieConsentElement.appendChild(_createInformationLink(linkText, linkHref));
}
cookieConsentElement.appendChild(_createDismissLink(dismissText));
return cookieConsentElement;
}
function _setElementText(element, text) {
if (supportsTextContent) {
element.textContent = text;
} else {
element.innerText = text;
}
}
function _createConsentText(cookieText) {
var consentText = document.createElement('span');
_setElementText(consentText, cookieText);
return consentText;
}
function _createDismissLink(dismissText) {
var dismissLink = document.createElement('a');
_setElementText(dismissLink, dismissText);
dismissLink.id = dismissLinkId;
dismissLink.href = '#';
return dismissLink;
}
function _createInformationLink(linkText, linkHref) {
var infoLink = document.createElement('a');
_setElementText(infoLink, linkText);
infoLink.href = linkHref;
infoLink.target = '_blank';
return infoLink;
}
function _dismissLinkClick() {
_saveUserPreference();
_removeCookieConsent();
return false;
}
function _showCookieConsent(cookieText, dismissText, linkText, linkHref) {
if (_shouldDisplayConsent()) {
_removeCookieConsent();
var consentElement = _createHeaderElement(cookieText, dismissText, linkText, linkHref);
var fragment = document.createDocumentFragment();
fragment.appendChild(consentElement);
document.body.appendChild(fragment.cloneNode(true));
document.getElementById(dismissLinkId).onclick = _dismissLinkClick;
}
}
function showCookieConsentBar(cookieText, dismissText, linkText, linkHref) {
_showCookieConsent(cookieText, dismissText, linkText, linkHref, false);
}
function _removeCookieConsent() {
var cookieChoiceElement = document.getElementById(cookieConsentId);
if (cookieChoiceElement != null) {
cookieChoiceElement.parentNode.removeChild(cookieChoiceElement);
}
}
function _saveUserPreference() {
// Set the cookie expiry to one year after today.
var expiryDate = new Date();
expiryDate.setFullYear(expiryDate.getFullYear() + 1);
document.cookie = cookieName + '=y; expires=' + expiryDate.toGMTString();
}
function _shouldDisplayConsent() {
// Display the header only if the cookie has not been set.
return !document.cookie.match(new RegExp(cookieName + '=([^;]+)'));
}
function _checkForTextContentSupport() {
try{
supportsTextContent = 'textContent' in document.body;
} catch(e){
supportsTextContent = false;
}
}
var exports = {};
exports.showCookieConsentBar = showCookieConsentBar;
return exports;
})();
window.cookieChoices = cookieChoices;
return cookieChoices;
})(this);