-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcookie-consent.js
103 lines (91 loc) · 2.99 KB
/
cookie-consent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Requires a simple cookie lib, like the bundled cookie.js
/**
* Init cookie consent bar (no cookie written yet)
*/
function initCookieConsentBar() {
// Only show a cookie bar if cookies are allowed at all, otherwise keep
// »null« as consent level and retry on the next request
if($.areCookiesEnabled() == false) {
return;
}
// Set level & duration due to continued usage
let consentBar = document.getElementsByClassName('cookie-consent')[0];
let level = consentBar.dataset.level;
// Set fallback level for continued usage and restrict the max level
// (don't allow automatic opt-ins)
if (level === undefined || level >= 50) {
level = 1;
}
// Don't store cookie for more than 1 year
let duration = consentBar.dataset.duration;
if (duration === undefined || duration > 8760) {
duration = 8;
}
$.cookie('cookie-consent', level, duration*60*60*1000)
showCookieConsentBar();
}
/**
* Show cookie consent bar until a confirmation button is clicked
*/
function showCookieConsentBar() {
// Autowire events to opt-in button, set default level & duration if missing
let consentBar = document.getElementsByClassName('cookie-consent')[0];
let buttons = document.getElementsByClassName('cookie-accept');
for (var i = 0; i < buttons.length; i++) {
let button = buttons[i];
let level = button.dataset.level;
// Set fallback level for opt-in button
if (level === undefined) {
level = 50;
}
// Don't store consent cookie for more than 1 year
let duration = button.dataset.duration;
if (duration === undefined || duration > 8760) {
duration = 8;
}
button.addEventListener('click', function () {
$.cookie('cookie-consent', level, duration * 60 * 60 * 1000)
fadeOut(consentBar, 250);
});
}
// Setup done, show the consent bar
fadeIn(consentBar, 250);
}
/**
* FadeIn effect
* @param element - DOM Element
* @param speed - effect duration
* @return null
*/
function fadeIn(element, speed) {
var elementStyle = element.style;
elementStyle.opacity = 0;
elementStyle.display = 'block';
(function fade() {
(elementStyle.opacity -= -0.1) > 0.9 ? null : setTimeout(fade, (speed / 10));
})();
}
/**
* FadeOut effect
* @param element - DOM Element
* @param speed - effect duration
* @return null
*/
function fadeOut(element, speed) {
var elementStyle = element.style;
elementStyle.opacity = 1;
(function fade() {
(elementStyle.opacity -= 0.1) < 0.1 ? elementStyle.display = 'none' : setTimeout(fade, (speed / 10));
})();
}
document.addEventListener('DOMContentLoaded', function() {
let currentCookieSelection = $.cookie('cookie-consent');
// First request ever - show cookie bar and set a default level
if(currentCookieSelection === null) {
initCookieConsentBar();
} else if (currentCookieSelection > 0 && currentCookieSelection < 50) {
// Keep showing the cookie bar as long as no explicit agreement (Opt-In)
// was given yet (level lower than 50)
showCookieConsentBar();
}
});