@solvenza/cookies-compliance 1.0.0 → 1.2.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.
@@ -459,14 +459,34 @@ var CookieStore = class {
459
459
  const isHttps = typeof window !== "undefined" && window.location.protocol === "https:";
460
460
  const secure = (_b = options.secure) != null ? _b : isHttps;
461
461
  let cookieStr = `${encodeURIComponent(name)}=${encodeURIComponent(value)}; Path=${path}; Max-Age=${maxAgeSeconds}; SameSite=${sameSite}`;
462
+ if (options.domain) {
463
+ cookieStr += `; Domain=${options.domain}`;
464
+ }
462
465
  if (secure) {
463
466
  cookieStr += "; Secure";
464
467
  }
465
468
  document.cookie = cookieStr;
466
469
  }
467
- static remove(name, path = "/") {
470
+ static remove(name, path = "/", domain) {
468
471
  if (typeof document === "undefined") return;
469
- document.cookie = `${encodeURIComponent(name)}=; Path=${path}; Max-Age=0; SameSite=Lax`;
472
+ let cookieStr = `${encodeURIComponent(name)}=; Path=${path}; Max-Age=0; SameSite=Lax`;
473
+ if (domain) {
474
+ cookieStr += `; Domain=${domain}`;
475
+ }
476
+ document.cookie = cookieStr;
477
+ }
478
+ static clearServiceCookies(config, revokedCategory) {
479
+ var _a, _b;
480
+ if (typeof document === "undefined" || !config.services) return;
481
+ const path = ((_a = config.storage) == null ? void 0 : _a.path) || "/";
482
+ const domain = (_b = config.storage) == null ? void 0 : _b.domain;
483
+ for (const service of Object.values(config.services)) {
484
+ if (service.category === revokedCategory && service.cookies) {
485
+ for (const cookieItem of service.cookies) {
486
+ this.remove(cookieItem.name, path, domain || cookieItem.domain);
487
+ }
488
+ }
489
+ }
470
490
  }
471
491
  };
472
492
 
@@ -1627,6 +1647,7 @@ var ConsentEngine = class {
1627
1647
  GoogleConsentAdapter.initDefault();
1628
1648
  if (evalResult.isValid) {
1629
1649
  GoogleConsentAdapter.update(evalResult.choices);
1650
+ this.dispatchDomEvent("solvenza:restored", { state: this.getConsent() });
1630
1651
  }
1631
1652
  this.blockerRegistry.init(
1632
1653
  (cat) => this.has(cat),
@@ -1713,7 +1734,7 @@ var ConsentEngine = class {
1713
1734
  this.preferencesModal.close();
1714
1735
  }
1715
1736
  withdraw() {
1716
- var _a, _b;
1737
+ var _a, _b, _c, _d;
1717
1738
  const previousChoices = this.stateManager.getChoices();
1718
1739
  const config = this.stateManager.getConfig();
1719
1740
  if (!config) return;
@@ -1721,11 +1742,17 @@ var ConsentEngine = class {
1721
1742
  if (((_b = config.storage) == null ? void 0 : _b.type) === "memory") {
1722
1743
  MemoryStore.remove(cookieName);
1723
1744
  } else {
1724
- CookieStore.remove(cookieName);
1745
+ CookieStore.remove(cookieName, ((_c = config.storage) == null ? void 0 : _c.path) || "/", (_d = config.storage) == null ? void 0 : _d.domain);
1746
+ }
1747
+ for (const catId of Object.keys(config.categories)) {
1748
+ if (catId !== "necessary") {
1749
+ CookieStore.clearServiceCookies(config, catId);
1750
+ }
1725
1751
  }
1726
1752
  this.stateManager.clearChoices();
1727
1753
  GoogleConsentAdapter.update(this.stateManager.getChoices());
1728
1754
  this.eventBus.emit("consent:withdrawn", { previousChoices });
1755
+ this.dispatchDomEvent("solvenza:updated", { choices: this.stateManager.getChoices() });
1729
1756
  this.showBanner();
1730
1757
  }
1731
1758
  when(categoryOrService, callback) {
@@ -1760,7 +1787,7 @@ var ConsentEngine = class {
1760
1787
  }
1761
1788
  }
1762
1789
  saveChoices(choices, source) {
1763
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1790
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1764
1791
  const config = this.stateManager.getConfig();
1765
1792
  if (!config) return;
1766
1793
  const receipt = createReceipt(
@@ -1771,6 +1798,11 @@ var ConsentEngine = class {
1771
1798
  (_a = config.security) == null ? void 0 : _a.secretKey
1772
1799
  );
1773
1800
  this.stateManager.updateChoices(receipt);
1801
+ for (const [catId, isAllowed] of Object.entries(choices)) {
1802
+ if (!isAllowed) {
1803
+ CookieStore.clearServiceCookies(config, catId);
1804
+ }
1805
+ }
1774
1806
  const cookieName = ((_b = config.storage) == null ? void 0 : _b.name) || "site_consent";
1775
1807
  const receiptJson = JSON.stringify(receipt);
1776
1808
  if (((_c = config.storage) == null ? void 0 : _c.type) === "memory") {
@@ -1780,7 +1812,8 @@ var ConsentEngine = class {
1780
1812
  path: ((_d = config.storage) == null ? void 0 : _d.path) || "/",
1781
1813
  maxAgeDays: (_f = (_e = config.consent) == null ? void 0 : _e.maxAgeDays) != null ? _f : 365,
1782
1814
  sameSite: ((_g = config.storage) == null ? void 0 : _g.sameSite) || "Lax",
1783
- secure: (_h = config.storage) == null ? void 0 : _h.secure
1815
+ secure: (_h = config.storage) == null ? void 0 : _h.secure,
1816
+ domain: (_i = config.storage) == null ? void 0 : _i.domain
1784
1817
  });
1785
1818
  }
1786
1819
  GoogleConsentAdapter.update(choices);
@@ -1788,7 +1821,7 @@ var ConsentEngine = class {
1788
1821
  (cat) => this.has(cat),
1789
1822
  (srv) => this.hasService(srv)
1790
1823
  );
1791
- if (((_i = config.logging) == null ? void 0 : _i.enabled) && config.logging.endpoint) {
1824
+ if (((_j = config.logging) == null ? void 0 : _j.enabled) && config.logging.endpoint) {
1792
1825
  try {
1793
1826
  fetch(config.logging.endpoint, {
1794
1827
  method: "POST",
@@ -1801,6 +1834,7 @@ var ConsentEngine = class {
1801
1834
  }
1802
1835
  this.banner.remove();
1803
1836
  this.eventBus.emit("consent:changed", { choices, receipt });
1837
+ this.dispatchDomEvent("solvenza:updated", { choices, receipt });
1804
1838
  }
1805
1839
  showBanner() {
1806
1840
  const config = this.stateManager.getConfig();
@@ -1811,6 +1845,7 @@ var ConsentEngine = class {
1811
1845
  onConfigure: () => this.openPreferences()
1812
1846
  });
1813
1847
  this.eventBus.emit("banner:shown", void 0);
1848
+ this.dispatchDomEvent("solvenza:show", void 0);
1814
1849
  }
1815
1850
  setupGlobalRevocationTrigger() {
1816
1851
  if (typeof document === "undefined") return;
@@ -1821,6 +1856,20 @@ var ConsentEngine = class {
1821
1856
  this.openPreferences();
1822
1857
  }
1823
1858
  });
1859
+ document.addEventListener("solvenza:show", () => {
1860
+ this.showBanner();
1861
+ });
1862
+ document.addEventListener("solvenza:preferences", () => {
1863
+ this.openPreferences();
1864
+ });
1865
+ }
1866
+ dispatchDomEvent(name, detail) {
1867
+ if (typeof document === "undefined") return;
1868
+ try {
1869
+ const customEvent = new CustomEvent(name, { detail, bubbles: true });
1870
+ document.dispatchEvent(customEvent);
1871
+ } catch (e) {
1872
+ }
1824
1873
  }
1825
1874
  };
1826
1875
  var globalScope = typeof window !== "undefined" ? window : globalThis;