-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathindex.js
227 lines (199 loc) · 6.17 KB
/
index.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var cookieConsent = (function () {
var init = false;
var consentDisabled = true;
var store = {};
var subscribers = {};
var events = ['add', 'enable', 'disable', 'change'];
var storeCallbacks = {};
/**
* _publish: Calls subscriber callbacks
* @param {String} consentKey Consent key
* @param {String} eventName Event name
* @param {Boolean} data Consent value
*/
function _publish (consentKey, eventName, data) {
if (subscribers[consentKey] && subscribers[consentKey][eventName]) {
Object.values(subscribers[consentKey][eventName]).forEach(function (callback) {
callback(data);
})
}
}
/**
* _publishStore: calls Callbacks with the store object passed
*/
function _publishStore () {
if (Object.values(storeCallbacks).length > 0) {
Object.values(storeCallbacks).forEach(function (callback) {
callback(Object.assign({}, store));
})
}
}
/**
* _set: Set store data
* @param {String} consentKey Consent key
* @param {String} consentValue Consent value
*/
function _set (consentKey, consentValue) {
if (typeof consentValue !== 'boolean') {
throw new TypeError('cookieConsent (setConsent): consent values should be of type boolean. Check value for ' + consentKey + '.');
}
var oldValue = store[consentKey];
if (oldValue !== consentValue) {
store[consentKey] = consentValue;
if (oldValue === undefined) {
_publish(consentKey, 'add', consentValue);
}
if (oldValue === true) {
_publish(consentKey, 'disable', consentValue);
_publish(consentKey, 'change', consentValue);
}
else if (oldValue === false) {
_publish(consentKey, 'enable', consentValue);
_publish(consentKey, 'change', consentValue);
}
else {
_publish(consentKey, 'change', consentValue);
}
}
}
/**
* _unsubscribe: Unsubscribe subscribers
*/
function _unsubscribe () {
if (subscribers[this.consentKey] && subscribers[this.consentKey][this.eventName]) {
delete subscribers[this.consentKey][this.eventName][this.key];
}
}
/**
* _unsubscribeStore: Unsubscribes subscribers from the consent store
*/
function _unsubscribeStore () {
if (storeCallbacks[this.key]) {
delete storeCallbacks[this.key];
}
}
/**
* Get consent disabled
* @returns Boolean
*/
function getConsentDisabled () {
return consentDisabled
}
/**
* initConsent: Initialize the consent store
* @param {Object} consent Consent object
* @param {Boolean} disable Disable consent
* @returns Boolean
*/
function initConsent (consent, disable) {
if (!init) {
consentDisabled = !!disable;
if (consentDisabled) {
init = true;
return true;
}
if (!(consent && !Array.isArray(consent) && typeof consent === 'object')) {
throw new TypeError('cookieConsent (init): consent should be an object.');
}
var keys = Object.keys(consent);
for (var i = 0; i < keys.length; i++) {
if (typeof consent[keys[i]] !== 'boolean') {
throw new TypeError('cookieConsent (init): consent values for ' + keys[i] + ' should be of type boolean.');
}
}
store = Object.assign({}, consent);
init = true;
return true;
}
return false;
}
/**
* getConsent: Get consent value from store
* @param {String} consentKey Consent key
* @returns Boolean or Undefined
*/
function getConsent (consentKey) {
if (consentDisabled) {
return true;
}
return store[consentKey];
}
/**
* setConsent: Set consent values in the store
* @param {Object} consent Consent object
* @returns Boolean
*/
function setConsent (consent) {
if (init && !consentDisabled) {
if (!(consent && !Array.isArray(consent) && typeof consent === 'object')) {
throw new TypeError('cookieConsent (setConsent): consent should be an object.')
}
var store
var keys = Object.keys(consent);
for (var i = 0; i < keys.length; i++) {
_set(keys[i], consent[keys[i]]);
}
var storeCopy=Object.assign({},store)
if (JSON.stringify(storeCopy)!==JSON.stringify(store)) {
_publishStore()
}
return true;
}
return false;
}
/**
* subscribe: Subscribe to store events
* @param {String} consentKey Consent key
* @param {String} eventName Event name
* @param {Function} callback Event callback
* @returns Object: Contains unsubscribe method
*/
function subscribe (consentKey, eventName, callback) {
if (typeof consentKey !== 'string') {
throw new TypeError('cookieConsent (subscribe): consent key should be of type string.');
}
if (typeof eventName !== 'string') {
throw new TypeError('cookieConsent (subscribe): event name should be of type string.');
}
if (!events.includes(eventName)) {
throw new TypeError('cookieConsent (subscribe): event name should be one of ' + events.join(', ') + '.');
}
if (typeof callback !== 'function') {
throw new TypeError('cookieConsent (subscribe): callback should be a function.');
}
if (!subscribers[consentKey]) {
subscribers[consentKey] = {};
}
if (!subscribers[consentKey][eventName]) {
subscribers[consentKey][eventName] = {};
}
var key = Math.random().toString(36).substr(2, 5);
subscribers[consentKey][eventName][key] = callback
return {
unsubscribe: _unsubscribe.bind({
consentKey: consentKey,
eventName: eventName,
key: key
})
};
}
function subscribeToConsentStore (callback) {
if (typeof callback !== 'function') {
throw new TypeError('cookieConsent (subscribeToConsentStore): callback should be a function.');
}
var key = Math.random().toString(36).substr(2, 5);
storeCallbacks[key] = callback;
return {
unsubscribe: _unsubscribeStore.bind({ key: key })
}
}
return {
initConsent: initConsent,
getConsent: getConsent,
getConsentDisabled: getConsentDisabled,
setConsent: setConsent,
subscribe: subscribe,
subscribeToConsentStore: subscribeToConsentStore
};
})();
module.exports = cookieConsent;