bv-ui-core 2.5.2 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cookie/README.md
CHANGED
@@ -5,7 +5,7 @@ The cookie module provides methods for interacting with browser cookies.
|
|
5
5
|
```js
|
6
6
|
var cookie = require('bv-ui-core/lib/cookie');
|
7
7
|
|
8
|
-
cookie.
|
8
|
+
cookie.create('RememberMe', '1', 365);
|
9
9
|
console.log(cookie.read('RememberMe')); // '1'
|
10
10
|
cookie.remove('RememberMe');
|
11
11
|
```
|
package/lib/cookie/index.js
CHANGED
@@ -79,6 +79,14 @@ module.exports = {
|
|
79
79
|
if (consentPresent) {
|
80
80
|
createCookie(name, value, days, domain, secure);
|
81
81
|
}
|
82
|
+
cookieConsent.subscribe(name,'add',function (consent) {
|
83
|
+
if (consent) {
|
84
|
+
createCookie(name,value,days,domain,secure);
|
85
|
+
}
|
86
|
+
else {
|
87
|
+
removeCookie(name,domain)
|
88
|
+
}
|
89
|
+
})
|
82
90
|
|
83
91
|
cookieConsent.subscribe(name, 'enable', function () {
|
84
92
|
createCookie(name, value, days, domain, secure);
|
@@ -30,6 +30,9 @@ cookieConsent.subscribe('cookie3', 'enable', function (data) {});
|
|
30
30
|
// Subscribe to consent 'disable' event. Triggers when a cookie consent is set to false
|
31
31
|
var event = cookieConsent.subscribe('cookie3', 'disable', function (data) {});
|
32
32
|
|
33
|
+
// Subscribe to the store change event. The latest consent store object is passed as parameter to the callback function
|
34
|
+
var event = subscribeToConsentStore(function (store){});
|
35
|
+
|
33
36
|
// Unsubscribe events
|
34
37
|
event.unsubscribe();
|
35
38
|
|
@@ -4,6 +4,9 @@ var cookieConsent = (function () {
|
|
4
4
|
var store = {};
|
5
5
|
var subscribers = {};
|
6
6
|
var events = ['add', 'enable', 'disable', 'change'];
|
7
|
+
var storeCallbacks = {};
|
8
|
+
|
9
|
+
|
7
10
|
/**
|
8
11
|
* _publish: Calls subscriber callbacks
|
9
12
|
* @param {String} consentKey Consent key
|
@@ -17,6 +20,18 @@ var cookieConsent = (function () {
|
|
17
20
|
})
|
18
21
|
}
|
19
22
|
}
|
23
|
+
|
24
|
+
/**
|
25
|
+
* _publishStore: calls Callbacks with the store object passed
|
26
|
+
*/
|
27
|
+
|
28
|
+
function _publishStore () {
|
29
|
+
if (Object.values(storeCallbacks).length > 0) {
|
30
|
+
Object.values(storeCallbacks).forEach(function (callback) {
|
31
|
+
callback(Object.assign({}, store));
|
32
|
+
})
|
33
|
+
}
|
34
|
+
}
|
20
35
|
/**
|
21
36
|
* _set: Set store data
|
22
37
|
* @param {String} consentKey Consent key
|
@@ -56,6 +71,15 @@ var cookieConsent = (function () {
|
|
56
71
|
delete subscribers[this.consentKey][this.eventName][this.key];
|
57
72
|
}
|
58
73
|
}
|
74
|
+
|
75
|
+
/**
|
76
|
+
* _unsubscribeStore: Unsubscribes subscribers from the consent store
|
77
|
+
*/
|
78
|
+
function _unsubscribeStore () {
|
79
|
+
if (storeCallbacks[this.key]) {
|
80
|
+
delete storeCallbacks[this.key];
|
81
|
+
}
|
82
|
+
}
|
59
83
|
/**
|
60
84
|
* Get consent disabled
|
61
85
|
* @returns Boolean
|
@@ -119,10 +143,15 @@ var cookieConsent = (function () {
|
|
119
143
|
if (!(consent && !Array.isArray(consent) && typeof consent === 'object')) {
|
120
144
|
throw new TypeError('cookieConsent (setConsent): consent should be an object.')
|
121
145
|
}
|
146
|
+
var store
|
122
147
|
var keys = Object.keys(consent);
|
123
148
|
for (var i = 0; i < keys.length; i++) {
|
124
149
|
_set(keys[i], consent[keys[i]]);
|
125
150
|
}
|
151
|
+
var storeCopy=Object.assign({},store)
|
152
|
+
if (JSON.stringify(storeCopy)!==JSON.stringify(store)) {
|
153
|
+
_publishStore()
|
154
|
+
}
|
126
155
|
return true;
|
127
156
|
}
|
128
157
|
|
@@ -172,12 +201,26 @@ var cookieConsent = (function () {
|
|
172
201
|
};
|
173
202
|
}
|
174
203
|
|
204
|
+
function subscribeToConsentStore (callback) {
|
205
|
+
if (typeof callback !== 'function') {
|
206
|
+
throw new TypeError('cookieConsent (subscribeToConsentStore): callback should be a function.');
|
207
|
+
}
|
208
|
+
|
209
|
+
var key = Math.random().toString(36).substr(2, 5);
|
210
|
+
storeCallbacks[key] = callback;
|
211
|
+
|
212
|
+
return {
|
213
|
+
unsubscribe: _unsubscribeStore.bind({ key: key })
|
214
|
+
}
|
215
|
+
}
|
216
|
+
|
175
217
|
return {
|
176
218
|
initConsent: initConsent,
|
177
219
|
getConsent: getConsent,
|
178
220
|
getConsentDisabled: getConsentDisabled,
|
179
221
|
setConsent: setConsent,
|
180
|
-
subscribe: subscribe
|
222
|
+
subscribe: subscribe,
|
223
|
+
subscribeToConsentStore: subscribeToConsentStore
|
181
224
|
};
|
182
225
|
})();
|
183
226
|
|
package/package.json
CHANGED
@@ -84,7 +84,36 @@ describe('lib/cookieConsent', function () {
|
|
84
84
|
function test5 () {
|
85
85
|
return cookieConsent.subscribe('key1', 'enable', function () {});
|
86
86
|
}
|
87
|
-
|
88
87
|
expect(test5()).to.be.an('object');
|
89
88
|
});
|
89
|
+
it('cookieConsent.subscribeToConsentStore', function () {
|
90
|
+
// Error checks - Correct errors are thrown
|
91
|
+
function test6 () {
|
92
|
+
cookieConsent.subscribeToConsentStore('Callback')
|
93
|
+
}
|
94
|
+
expect(test6).to.throw(TypeError,'cookieConsent (subscribeToConsentStore): callback should be a function.');
|
95
|
+
// Subscriber creation test - The subscription gets created correctly
|
96
|
+
function test7 () {
|
97
|
+
return cookieConsent.subscribeToConsentStore(function () {});
|
98
|
+
}
|
99
|
+
expect(test7()).to.be.an('object');
|
100
|
+
|
101
|
+
// Event listener test - The subscriber callback fires on store change
|
102
|
+
var fn = sinon.spy()
|
103
|
+
function test8 () {
|
104
|
+
cookieConsent.subscribeToConsentStore(fn)
|
105
|
+
cookieConsent.setConsent({ cookie4: true })
|
106
|
+
}
|
107
|
+
test8 ()
|
108
|
+
sinon.assert.calledOnce(fn)
|
109
|
+
// Unsubscribe test - Should be able to unsubscribe successfully (Should not fire the callback after unsubscription)
|
110
|
+
var fn2 = sinon.spy()
|
111
|
+
function test9 () {
|
112
|
+
var event = cookieConsent.subscribeToConsentStore(fn2)
|
113
|
+
event.unsubscribe()
|
114
|
+
cookieConsent.setConsent({ cookie5: true })
|
115
|
+
}
|
116
|
+
test9()
|
117
|
+
sinon.assert.notCalled(fn2)
|
118
|
+
});
|
90
119
|
});
|