diff --git a/src/lib/cmp.js b/src/lib/cmp.js index b429565..62774ea 100644 --- a/src/lib/cmp.js +++ b/src/lib/cmp.js @@ -122,7 +122,7 @@ export default class Cmp { return this.store.getFullVendorConsentsObject(vendorIds) .then(consent => { consent.gdprApplies = this.gdprApplies; - callback(consent); + callback(consent, true); return consent; }); }, @@ -131,12 +131,16 @@ export default class Cmp { * Get the encoded vendor consent data value. */ getConsentData: (_, callback = () => {}) => { - const consentData = { - gdprApplies: this.gdprApplies, - hasGlobalScope: this.config.storeConsentGlobally, - consentData: this.generateConsentString() - }; - callback(consentData, true); + return this.store.getFullVendorConsentsObject() + .then(consent => { + const output = { + gdprApplies: this.gdprApplies, + hasGlobalScope: this.config.storeConsentGlobally, + consentData: consent.consentString + }; + callback(output, true); + return output; + }); }, /** @@ -224,8 +228,9 @@ export default class Cmp { * Trigger the consent tool UI to be shown */ showConsentTool: (_, callback = () => {}) => { + const self = this; let _command; - switch (this.config.layout) { + switch (self.config.layout) { case 'footer': _command = 'toggleFooterConsentToolShowing'; break; @@ -236,8 +241,9 @@ export default class Cmp { _command = 'toggleConsentToolShowing'; } - this.cmpShown = true; - this.store[_command](true); + self.cmpShown = true; + self.store[_command](true); + self.notify("consentToolDisplayed"); callback(true); }, @@ -254,21 +260,43 @@ export default class Cmp { customPurposeList } = this.store; - const { purposes: customPurposes = [] } = customPurposeList; - const { purposes = [] } = vendorList || {}; + let customPurposes = []; + customPurposes = customPurposeList && customPurposeList.purposes; + const { purposes = [] } = vendorList || {}; const { selectedPurposeIds = new Set() } = persistedVendorConsentData || {}; const { selectedCustomPurposeIds = new Set() } = persistedPublisherConsentData || {}; - const allowedPurposeIds = new Set(purposes.map(({id}) => id)); - const allowedCustomPurposeIds = new Set(customPurposes.map(({id}) => id)); + const allowedPurposeIds = new Set(); + for (let i in purposes) { + allowedPurposeIds.add(purposes[i].id); + } + + const allowedCustomPurposeIds = new Set(); + if (customPurposeList) { + for (let j in customPurposes) { + allowedCustomPurposeIds.add(customPurposes[j].id); + } + } + + const selectedAllowedPurposeIds = new Set(); + Array.from(selectedPurposeIds).filter(id => allowedPurposeIds.has(id)).forEach((id) => { + selectedAllowedPurposeIds.add(id); + }); + + const selectedAllowedCustomPurposeIds = new Set(); + if (customPurposeList) { + Array.from(selectedCustomPurposeIds).filter(id => allowedCustomPurposeIds.has(id)).forEach((id) => { + selectedAllowedCustomPurposeIds.add(id); + }); + } // Encode the persisted data return persistedPublisherConsentData && encodePublisherConsentData({ ...persistedVendorConsentData, ...persistedPublisherConsentData, - selectedCustomPurposeIds: new Set(Array.from(selectedCustomPurposeIds).filter(id => allowedCustomPurposeIds.has(id))), - selectedPurposeIds: new Set(Array.from(selectedPurposeIds).filter(id => allowedPurposeIds.has(id))), + selectedPurposeIds: selectedAllowedPurposeIds, + selectedCustomPurposeIds: selectedAllowedCustomPurposeIds, customPurposeList, vendorList, }); @@ -307,14 +335,31 @@ export default class Cmp { } = persistedVendorConsentData || {}; // Filter consents by values that exist in the current vendorList - const allowedVendorIds = new Set(vendors.map(({id}) => id)); - const allowedPurposeIds = new Set(purposes.map(({id}) => id)); + const allowedVendorIds = new Set(); + for (let i in vendors) { + allowedVendorIds.add(vendors[i].id); + } + + const allowedPurposeIds = new Set(); + for (let j in purposes) { + allowedPurposeIds.add(purposes[j].id); + } + + const selectedAllowedVendorIds = new Set(); + Array.from(selectedVendorIds).filter(id => allowedVendorIds.has(id)).forEach((id) => { + selectedAllowedVendorIds.add(id); + }); + + const selectedAllowedPurposeIds = new Set(); + Array.from(selectedPurposeIds).filter(id => allowedPurposeIds.has(id)).forEach((id) => { + selectedAllowedPurposeIds.add(id); + }); // Encode the persisted data return persistedVendorConsentData && encodeVendorConsentData({ ...persistedVendorConsentData, - selectedVendorIds: new Set(Array.from(selectedVendorIds).filter(id => allowedVendorIds.has(id))), - selectedPurposeIds: new Set(Array.from(selectedPurposeIds).filter(id => allowedPurposeIds.has(id))), + selectedVendorIds: selectedAllowedVendorIds, + selectedPurposeIds: selectedAllowedPurposeIds, vendorList }); }; diff --git a/src/lib/cmp.test.js b/src/lib/cmp.test.js index 2f0c09d..d7a7962 100644 --- a/src/lib/cmp.test.js +++ b/src/lib/cmp.test.js @@ -189,18 +189,37 @@ describe('cmp', () => { }); }); - it('getConsentData executes', (done) => { - cmp.processCommand('getConsentData', null, data => { - expect(typeof data.consentData).to.equal('string'); - done(); + describe('getConsentData', () => { + beforeEach(() => { + cmp.store = { + consentString: "here's a consent string", + persistedVendorConsentData: {}, + persist: () => { + cmp.store.consentString = "here's a persisted consent string"; + }, + getFullVendorConsentsObject: () => { + return new Promise((resolve) => { + resolve({consentString: cmp.store.consentString}); + }); + } + }; }); - }); - it('getConsentData returns persisted data', (done) => { - cmp.store.persist(); - cmp.processCommand('getConsentData', null, data => { - expect(typeof data.consentData).to.equal('string'); - done(); + it('getConsentData executes', (done) => { + cmp.processCommand('getConsentData', null, data => { + expect(typeof data.consentData).to.equal("string"); + expect(data.consentData).to.equal("here's a consent string"); + done(); + }); + }); + + it('getConsentData returns persisted data', (done) => { + cmp.store.persist(); + cmp.processCommand('getConsentData', null, data => { + expect(typeof data.consentData).to.equal("string"); + expect(data.consentData).to.equal("here's a persisted consent string"); + done(); + }); }); }); diff --git a/src/lib/portal.js b/src/lib/portal.js index a31c119..bb73205 100644 --- a/src/lib/portal.js +++ b/src/lib/portal.js @@ -44,7 +44,7 @@ function openGlobalVendorPortal() { // Add listener for messages from iframe window.addEventListener('message', event => { // Only look at messages with the vendorConsent property - const data = event.data.vendorConsent; + const data = event && event.data && event.data.vendorConsent; if (data) { // The iframe has loaded if (data.command === 'isLoaded' && portalTimeout) { diff --git a/src/lib/store.js b/src/lib/store.js index dc4c0d7..3553d2f 100644 --- a/src/lib/store.js +++ b/src/lib/store.js @@ -23,7 +23,11 @@ function copyData(dataObject) { const copy = {...dataObject}; for (let key in copy) { if (copy.hasOwnProperty(key) && copy[key] instanceof Set) { - copy[key] = new Set(copy[key]); + let set = new Set(); + copy[key].forEach((val) => { + set.add(val); + }); + copy[key] = set; } } return copy; @@ -102,8 +106,14 @@ export default class Store { const {purposes = [], vendors = []} = vendorList; // No consent will be allowed for vendors or purposes not on the list - const allowedVendorIds = new Set(vendors.map(({id}) => id)); - const allowedPurposeIds = new Set(purposes.map(({id}) => id)); + const allowedVendorIds = new Set(); + for (let i in vendors) { + allowedVendorIds.add(vendors[i].id); + } + const allowedPurposeIds = new Set(); + for (let j in purposes) { + allowedPurposeIds.add(purposes[j].id); + } // Map requested vendorIds const vendorMap = {}; @@ -204,7 +214,10 @@ export default class Store { const {purposes: customPurposes = []} = customPurposeList; // No consent will be allowed for purposes not on the list - const allowedPurposeIds = new Set(purposes.map(({id}) => id)); + const allowedPurposeIds = new Set(); + for (let i in purposes) { + allowedPurposeIds.add(purposes[i].id); + } const lastCustomPurposeId = Math.max( ...customPurposes.map(({id}) => id), @@ -404,8 +417,14 @@ export default class Store { // If vendor consent data has never been persisted set default selected status if (!created) { - this.vendorConsentData.selectedPurposeIds = new Set(purposes.map(p => p.id)); - this.vendorConsentData.selectedVendorIds = new Set(vendors.map(v => v.id)); + this.vendorConsentData.selectedPurposeIds = new Set(); + for (let i in purposes) { + this.vendorConsentData.selectedPurposeIds.add(purposes[i].id); + } + this.vendorConsentData.selectedVendorIds = new Set(); + for (let j in vendors) { + this.vendorConsentData.selectedVendorIds.add(vendors[j].id); + } } const {selectedVendorIds = new Set()} = this.vendorConsentData; @@ -427,7 +446,10 @@ export default class Store { // If publisher consent has never been persisted set the default selected status if (!created) { const {purposes = [],} = customPurposeList || {}; - this.publisherConsentData.selectedCustomPurposeIds = new Set(purposes.map(p => p.id)); + this.publisherConsentData.selectedCustomPurposeIds = new Set(); + for (let i in purposes) { + this.publisherConsentData.selectedCustomPurposeIds.add(purposes[i].id); + } } const {version = 1} = customPurposeList || {}; diff --git a/src/lib/utils.js b/src/lib/utils.js index 97e9f59..65fa7de 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -33,7 +33,6 @@ function initConstants() { initConstants(); - function getConsentsCount(consentObject, vendorList) { let total = 0; let consented = 0;