150 lines
5.9 KiB
JavaScript
150 lines
5.9 KiB
JavaScript
/**
|
|
* Cookie handling mini framework &
|
|
* EU Cookie policy information layer
|
|
*
|
|
* created by binarity s.r.o
|
|
* www.binarity.cz
|
|
*
|
|
* @returns {*}
|
|
* @constructor
|
|
*/
|
|
var Cookies = {
|
|
read: function(sKey) {
|
|
return !sKey ? null : decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
|
|
},
|
|
set: function(sKey, sValue, vEnd, sPath, sDomain, bSecure) {
|
|
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
|
|
var sExpires = "";
|
|
if (vEnd) {
|
|
switch (vEnd.constructor) {
|
|
case Number:
|
|
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
|
|
break;
|
|
case String:
|
|
sExpires = "; expires=" + vEnd;
|
|
break;
|
|
case Date:
|
|
sExpires = "; expires=" + vEnd.toUTCString();
|
|
break;
|
|
}
|
|
}
|
|
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
|
|
return true;
|
|
},
|
|
remove: function (sKey, sPath, sDomain) {
|
|
if (!this.has(sKey)) { return false; }
|
|
document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
|
|
return true;
|
|
},
|
|
has: function (sKey) {
|
|
if (!sKey) { return false; }
|
|
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
|
|
},
|
|
keys: function () {
|
|
var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
|
|
for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
|
|
return aKeys;
|
|
}
|
|
};
|
|
|
|
function CookiePolicy(sPrefix, sText, sMore, sMoreURL, sUnderstand, sCookieName) {
|
|
'use strict';
|
|
|
|
var prefix = sPrefix || 'bineucookie_',
|
|
cookieName = sCookieName || 'allowed_eu',
|
|
text = sText || 'Tyto webové stránky používají k poskytování služeb, personalizaci reklam a analýze návštěvnosti soubory cookie. Informace o tom, jak tyto webové stránky používáte, jsou sdíleny se společností Google. Používáním těchto webových stránek souhlasíte s použitím souborů cookie.',
|
|
more = sMore || 'Další informace',
|
|
more_url = sMoreURL || 'https://www.google.com/policies/technologies/cookies/',
|
|
understand = sUnderstand || 'Rozumím',
|
|
// DOM
|
|
layerHolder = document.createElement('div'),
|
|
textElement = document.createElement('p'),
|
|
moreInfoButton = document.createElement('a'),
|
|
okButton = document.createElement('a');
|
|
|
|
/**
|
|
* This method checks whether cookie about cookies was set or not.
|
|
* If it was set, this method additionally checks for its validity.
|
|
*/
|
|
this.isValidCookie = function(sKey) {
|
|
return Cookies.has(prefix + sKey);
|
|
};
|
|
|
|
/**
|
|
* This method just adds custom styles to page header so it does not need to be all inline/
|
|
*/
|
|
this.addStyle = function() {
|
|
var styleElement = document.createElement('style');
|
|
|
|
styleElement.id = prefix + 'style';
|
|
styleElement.innerHTML += '#bin_eu_cookie_layer {opacity:1; position:fixed; padding:16px; left:0; top:0; width:100%; display:block; color:#f4f4f4; background-color:rgba(0,0,0,0.8); z-index:1000;}\r\n';
|
|
styleElement.innerHTML += '#bin_eu_cookie_layer #bin-cookie-text {width:80%; float:left; display:inline-block;}\r\n';
|
|
styleElement.innerHTML += '#bin_eu_cookie_layer .bin-button {padding: 8px; float:right;}\r\n';
|
|
|
|
// Responsive version
|
|
styleElement.innerHTML += '@media screen and (max-width:980px) {\r\n';
|
|
styleElement.innerHTML += '\t#bin_eu_cookie_layer #bin-cookie-text {width:100%;}';
|
|
styleElement.innerHTML += '}\r\n';
|
|
|
|
document.head.appendChild(styleElement);
|
|
};
|
|
|
|
/**
|
|
* This method accepts eu cookie policy by setting up a cookie that will inform the script next time that the policy
|
|
* was already accepted.
|
|
*/
|
|
this.acceptCookiePolicy = function() {
|
|
var date = new Date();
|
|
date.setFullYear(date.getFullYear() + 25);
|
|
Cookies.set(prefix + cookieName, 1, date.toGMTString(), '/');
|
|
|
|
layerHolder.parentNode.removeChild(layerHolder);
|
|
};
|
|
|
|
/**
|
|
* This method adds necessary elements to the DOM
|
|
*/
|
|
this.addElements = function() {
|
|
layerHolder.id = "bin_eu_cookie_layer";
|
|
|
|
textElement.id = "bin-cookie-text";
|
|
textElement.textContent = text;
|
|
|
|
moreInfoButton.className = 'bin-button';
|
|
moreInfoButton.textContent = more;
|
|
moreInfoButton.href = more_url;
|
|
moreInfoButton.target = '_blank';
|
|
|
|
okButton.id = 'bin-cookie-ok';
|
|
okButton.className = 'bin-button';
|
|
okButton.textContent = understand;
|
|
okButton.href = 'javascript:void(0);';
|
|
|
|
layerHolder.appendChild(textElement);
|
|
layerHolder.appendChild(moreInfoButton);
|
|
layerHolder.appendChild(okButton);
|
|
|
|
document.body.appendChild(layerHolder);
|
|
|
|
okButton.addEventListener('click', this.acceptCookiePolicy);
|
|
};
|
|
|
|
/**
|
|
* Method for adding complete DOM layer to the DOM.
|
|
*/
|
|
this.addDOMLayer = function() {
|
|
this.addStyle();
|
|
this.addElements();
|
|
};
|
|
|
|
/**
|
|
* This method initializes the whole process of drawing eu cookie policy layer.
|
|
*/
|
|
this.init = function() {
|
|
if(!this.isValidCookie(cookieName)) {
|
|
this.addDOMLayer();
|
|
}
|
|
};
|
|
|
|
return this.init(); // Auto-run on setup.
|
|
} |