/
cookie-permission.plugin.js
131 lines (105 loc) · 3.18 KB
/
cookie-permission.plugin.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import Plugin from 'src/plugin-system/plugin.class';
import CookieStorage from 'src/helper/storage/cookie-storage.helper';
import Debouncer from 'src/helper/debouncer.helper';
import DeviceDetection from 'src/helper/device-detection.helper';
export default class CookiePermissionPlugin extends Plugin {
static options = {
/**
* cookie expiration time
* the amount of days until the cookie bar will be displayed again
*/
cookieExpiration: 30,
/**
* cookie set to determine if cookies were accepted or denied
*/
cookieName: 'cookie-preference',
/**
* cookie dismiss button selector
*/
buttonSelector: '.js-cookie-permission-button',
/**
* resize debounce delay
*/
resizeDebounceTime: 200
};
init() {
this._button = this.el.querySelector(this.options.buttonSelector);
if (!this._isPreferenceSet()) {
this._setBodyPadding();
this._registerEvents();
}
}
/**
* Checks if a cookie preference is already set.
* If not, the cookie bar is displayed.
*/
_isPreferenceSet() {
const cookiePermission = CookieStorage.getItem(this.options.cookieName);
if (!cookiePermission) {
this._showCookieBar();
return false;
}
return true;
}
/**
* Shows cookie bar
*/
_showCookieBar() {
this.el.style.display = 'block';
this.$emitter.publish('showCookieBar');
}
/**
* Hides cookie bar
*/
_hideCookieBar() {
this.el.style.display = 'none';
this.$emitter.publish('hideCookieBar');
}
/**
* register all needed events
*
* @private
*/
_registerEvents() {
if (this._button) {
const submitEvent = (DeviceDetection.isTouchDevice()) ? 'touchstart' : 'click';
this._button.addEventListener(submitEvent, this._handleDenyButton.bind(this));
}
window.addEventListener('resize', Debouncer.debounce(this._setBodyPadding.bind(this), this.options.resizeDebounceTime), {
capture: true,
passive: true
});
}
/**
* Event handler for the cookie bar 'deny' button
* Sets the 'cookie-preference' cookie to hide the cookie bar
* @private
*/
_handleDenyButton() {
const { cookieExpiration, cookieName } = this.options;
this._hideCookieBar();
this._removeBodyPadding();
CookieStorage.setItem(cookieName, '1', cookieExpiration);
this.$emitter.publish('onClickDenyButton');
}
/**
* Calculates cookie bar height
*/
_calculateCookieBarHeight() {
return this.el.offsetHeight;
}
/**
* Adds cookie bar height as padding-bottom on body
*/
_setBodyPadding() {
document.body.style.paddingBottom = this._calculateCookieBarHeight() + 'px';
this.$emitter.publish('setBodyPadding');
}
/**
* Removes padding-bottom from body
*/
_removeBodyPadding() {
document.body.style.paddingBottom = '0';
this.$emitter.publish('removeBodyPadding');
}
}