1
0
Fork 0
mirror of https://github.com/infinum/cookies_eu synced 2023-03-27 23:21:16 -04:00
cookies_eu/app/assets/javascripts/cookies_eu.js

66 lines
1.8 KiB
JavaScript
Raw Normal View History

//= require js.cookie
'use strict';
2018-03-19 14:45:12 +01:00
var windowIsTurbolinked = 'Turbolinks' in window;
2016-10-03 22:06:23 +02:00
var cookiesEu = {
init: function() {
var cookiesEuOKButton = document.querySelector('.js-cookies-eu-ok');
if (cookiesEuOKButton) {
this.addListener(cookiesEuOKButton);
2018-03-19 14:45:12 +01:00
// clear turbolinks cache so cookie banner does not reappear
windowIsTurbolinked && window.Turbolinks.clearCache();
2016-10-03 22:06:23 +02:00
}
},
addListener: function(target) {
// Support for IE < 9
if (target.attachEvent) {
target.attachEvent('onclick', this.setCookie);
} else {
target.addEventListener('click', this.setCookie, false);
}
2016-10-03 22:06:23 +02:00
},
setCookie: function() {
2019-01-30 14:45:08 +01:00
var isSecure = location.protocol === 'https:';
Cookies.set('cookie_eu_consented', true, { path: '/', expires: 365, secure: isSecure });
2016-10-03 22:06:23 +02:00
var container = document.querySelector('.js-cookies-eu');
container.parentNode.removeChild(container);
document.dispatchEvent(new CustomEvent('cookies-eu-acknowledged'));
2016-10-03 22:06:23 +02:00
}
};
(function() {
2018-03-19 14:45:12 +01:00
function eventName(fallback) {
return windowIsTurbolinked ? 'turbolinks:load' : fallback
}
2016-10-03 22:06:23 +02:00
var isCalled = false;
function isReady() {
2018-03-19 14:45:12 +01:00
// return early when cookiesEu.init has been called AND Turbolinks is NOT included
// when Turbolinks is included cookiesEu.init has to be called on every page load
if (isCalled && !windowIsTurbolinked) {
return
}
2016-10-03 22:06:23 +02:00
isCalled = true;
cookiesEu.init();
}
2016-10-03 22:06:23 +02:00
if (document.addEventListener) {
2018-03-19 14:45:12 +01:00
return document.addEventListener(eventName('DOMContentLoaded'), isReady, false);
2016-10-03 22:06:23 +02:00
}
2016-10-03 22:06:23 +02:00
// Old browsers IE < 9
if (window.addEventListener) {
2018-03-19 14:45:12 +01:00
window.addEventListener(eventName('load'), isReady, false);
2016-10-03 22:06:23 +02:00
} else if (window.attachEvent) {
2018-03-19 14:45:12 +01:00
window.attachEvent(eventName('onload'), isReady);
2016-10-03 22:06:23 +02:00
}
})();