bv-ui-core 2.5.0 → 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/README.md CHANGED
@@ -40,6 +40,7 @@ module's directory.
40
40
  - [body](./lib/body)
41
41
  - [checkHighContrast](./lib/checkHighContrast)
42
42
  - [cookie](./lib/cookie)
43
+ - [cookieConsent](./lib/cookieConsent)
43
44
  - [cssLoadChecker](./lib/cssLoadChecker)
44
45
  - [date.now](./lib/date.now)
45
46
  - [domainPolice](./lib/domainPolice)
@@ -5,12 +5,12 @@ 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.write('RememberMe', '1', 365);
8
+ cookie.create('RememberMe', '1', 365);
9
9
  console.log(cookie.read('RememberMe')); // '1'
10
10
  cookie.remove('RememberMe');
11
11
  ```
12
12
 
13
- The following methods are provided:
13
+ ## The following methods are provided:
14
14
 
15
15
  - `read(name)`: Read the cookie with the given name.
16
16
  - `create(name, value, [days, domain, secure])`: Write a cookie with the given
@@ -19,3 +19,11 @@ The following methods are provided:
19
19
  by passing `true` as the `secure` argument. Note that if one optional
20
20
  argument is provided, all previous arguments must be provided as well.
21
21
  - `remove(name)`: Delete the cookie with the given name.
22
+
23
+ ## Usage with `cookieConsent`:
24
+
25
+ - Call `cookieConsent.initConsent` before calling `cookie.create`.
26
+ - Cookies will get created only if the consent for that cookie is true.
27
+ - Each cookie will automatically subscribe to `enable` and `disable` consent events for that cookie.
28
+ - Cookie will get set when consent is `true`.
29
+ - Cookie will get removed when consent is `false`.
@@ -55,16 +55,21 @@ function readCookie (name) {
55
55
  }
56
56
  }
57
57
 
58
- return store[name] || null;
58
+ return null;
59
59
  }
60
60
  /**
61
61
  * Remove a cookie.
62
62
  *
63
63
  * @param {String} name The cookie name.
64
64
  */
65
- function removeCookie (name) {
65
+ function removeCookie (name, domain) {
66
66
  delete store[name];
67
- createCookie(name, '', -1);
67
+ if (domain) {
68
+ createCookie(name, null, -1, domain);
69
+ }
70
+ else {
71
+ createCookie(name, '', -1);
72
+ }
68
73
  }
69
74
 
70
75
  module.exports = {
@@ -74,13 +79,21 @@ module.exports = {
74
79
  if (consentPresent) {
75
80
  createCookie(name, value, days, domain, secure);
76
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
+ })
77
90
 
78
91
  cookieConsent.subscribe(name, 'enable', function () {
79
92
  createCookie(name, value, days, domain, secure);
80
93
  });
81
94
 
82
95
  cookieConsent.subscribe(name, 'disable', function () {
83
- removeCookie(name);
96
+ removeCookie(name, domain);
84
97
  });
85
98
  },
86
99
  read: readCookie,
@@ -5,10 +5,14 @@ The cookieConsent module provides methods for controlling cookie consents.
5
5
  ```js
6
6
  var cookieConsent = require('bv-ui-core/lib/cookieConsent');
7
7
  // Initialize
8
+ /**
9
+ * 2nd parameter is to disable/enable consent mechanism.
10
+ * Passing true will disable the consent mechanism.
11
+ */
8
12
  cookieConsent.initConsent({
9
13
  cookie1: false,
10
14
  cookie2: true
11
- });
15
+ }, false);
12
16
 
13
17
  // Set cookie consent
14
18
  cookieConsent.setConsent({ cookie1: true });
@@ -22,9 +26,16 @@ cookieConsent.subscribe('cookie3', 'add', function (data) {});
22
26
  // Subscribe to consent 'enable' event. Triggers when a cookie consent is set to true
23
27
  cookieConsent.subscribe('cookie3', 'enable', function (data) {});
24
28
 
29
+ // Valid events that can be subscribed to are 'add', 'enable', 'disable', and 'change'
25
30
  // Subscribe to consent 'disable' event. Triggers when a cookie consent is set to false
26
31
  var event = cookieConsent.subscribe('cookie3', 'disable', function (data) {});
27
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
+
28
36
  // Unsubscribe events
29
37
  event.unsubscribe();
38
+
39
+ // Get cookie consent disabled/enabled value.
40
+ cookieConsent.getConsentDisabled();
30
41
  ```
@@ -1,8 +1,12 @@
1
1
  var cookieConsent = (function () {
2
2
  var init = false;
3
+ var consentDisabled = true;
3
4
  var store = {};
4
5
  var subscribers = {};
5
- var events = ['add', 'enable', 'disable'];
6
+ var events = ['add', 'enable', 'disable', 'change'];
7
+ var storeCallbacks = {};
8
+
9
+
6
10
  /**
7
11
  * _publish: Calls subscriber callbacks
8
12
  * @param {String} consentKey Consent key
@@ -16,6 +20,18 @@ var cookieConsent = (function () {
16
20
  })
17
21
  }
18
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
+ }
19
35
  /**
20
36
  * _set: Set store data
21
37
  * @param {String} consentKey Consent key
@@ -36,9 +52,14 @@ var cookieConsent = (function () {
36
52
  }
37
53
  if (oldValue === true) {
38
54
  _publish(consentKey, 'disable', consentValue);
55
+ _publish(consentKey, 'change', consentValue);
39
56
  }
40
- else {
57
+ else if (oldValue === false) {
41
58
  _publish(consentKey, 'enable', consentValue);
59
+ _publish(consentKey, 'change', consentValue);
60
+ }
61
+ else {
62
+ _publish(consentKey, 'change', consentValue);
42
63
  }
43
64
  }
44
65
  }
@@ -50,13 +71,37 @@ var cookieConsent = (function () {
50
71
  delete subscribers[this.consentKey][this.eventName][this.key];
51
72
  }
52
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
+ }
83
+ /**
84
+ * Get consent disabled
85
+ * @returns Boolean
86
+ */
87
+ function getConsentDisabled () {
88
+ return consentDisabled
89
+ }
53
90
  /**
54
91
  * initConsent: Initialize the consent store
55
92
  * @param {Object} consent Consent object
93
+ * @param {Boolean} disable Disable consent
56
94
  * @returns Boolean
57
95
  */
58
- function initConsent (consent) {
96
+ function initConsent (consent, disable) {
59
97
  if (!init) {
98
+ consentDisabled = !!disable;
99
+
100
+ if (consentDisabled) {
101
+ init = true;
102
+ return true;
103
+ }
104
+
60
105
  if (!(consent && !Array.isArray(consent) && typeof consent === 'object')) {
61
106
  throw new TypeError('cookieConsent (init): consent should be an object.');
62
107
  }
@@ -82,6 +127,10 @@ var cookieConsent = (function () {
82
127
  * @returns Boolean or Undefined
83
128
  */
84
129
  function getConsent (consentKey) {
130
+ if (consentDisabled) {
131
+ return true;
132
+ }
133
+
85
134
  return store[consentKey];
86
135
  }
87
136
  /**
@@ -90,14 +139,19 @@ var cookieConsent = (function () {
90
139
  * @returns Boolean
91
140
  */
92
141
  function setConsent (consent) {
93
- if (init) {
142
+ if (init && !consentDisabled) {
94
143
  if (!(consent && !Array.isArray(consent) && typeof consent === 'object')) {
95
144
  throw new TypeError('cookieConsent (setConsent): consent should be an object.')
96
145
  }
146
+ var store
97
147
  var keys = Object.keys(consent);
98
148
  for (var i = 0; i < keys.length; i++) {
99
149
  _set(keys[i], consent[keys[i]]);
100
150
  }
151
+ var storeCopy=Object.assign({},store)
152
+ if (JSON.stringify(storeCopy)!==JSON.stringify(store)) {
153
+ _publishStore()
154
+ }
101
155
  return true;
102
156
  }
103
157
 
@@ -147,11 +201,26 @@ var cookieConsent = (function () {
147
201
  };
148
202
  }
149
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
+
150
217
  return {
151
218
  initConsent: initConsent,
152
219
  getConsent: getConsent,
220
+ getConsentDisabled: getConsentDisabled,
153
221
  setConsent: setConsent,
154
- subscribe: subscribe
222
+ subscribe: subscribe,
223
+ subscribeToConsentStore: subscribeToConsentStore
155
224
  };
156
225
  })();
157
226
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bv-ui-core",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
4
4
  "license": "Apache 2.0",
5
5
  "description": "Bazaarvoice UI-related JavaScript",
6
6
  "repository": {
@@ -72,7 +72,7 @@ describe('lib/cookieConsent', function () {
72
72
  cookieConsent.subscribe('key1', 'delete', null);
73
73
  }
74
74
 
75
- var events = ['add', 'enable', 'disable'];
75
+ var events = ['add', 'enable', 'disable', 'change'];
76
76
  expect(test3).to.throw(TypeError, 'cookieConsent (subscribe): event name should be one of ' + events.join(', ') + '.');
77
77
 
78
78
  function test4 () {
@@ -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
  });