|
1 | | -window.addEventListener('DOMContentLoaded', function(){ |
| 1 | +window.addEventListener('DOMContentLoaded', () => { |
2 | 2 |
|
3 | | - // Detect if user is on IE browser |
4 | | - var isIE = !!window.MSInputMethodContext && !!document.documentMode; |
5 | | - |
6 | | - // ie requires promise + fetch polyfills |
7 | | - if (isIE) { |
8 | | - var promiseScript = document.createElement("script"); |
9 | | - promiseScript.type = "text/javascript"; |
10 | | - promiseScript.src = |
11 | | - "https://cdn.jsdelivr.net/npm/promise-polyfill@8.1.3/dist/polyfill.min.js"; |
| 3 | + const ConsentPanel = class { |
| 4 | + constructor(el) { |
| 5 | + if (! el) { |
| 6 | + console.error('Consent panel element not found'); |
| 7 | + return; |
| 8 | + } |
12 | 9 |
|
13 | | - var fetchScript = document.createElement("script"); |
14 | | - fetchScript.type = "text/javascript"; |
15 | | - fetchScript.src = |
16 | | - "https://cdn.jsdelivr.net/npm/whatwg-fetch@3.4.0/dist/fetch.umd.min.js"; |
| 10 | + this.panelElement = el; |
17 | 11 |
|
18 | | - document.head.appendChild(promiseScript); |
19 | | - document.head.appendChild(fetchScript); |
20 | | - } |
| 12 | + this.panelElement.addEventListener('click', this.clickAndInputHandler.bind(this)); |
21 | 13 |
|
22 | | - var cookiePanel; |
23 | | - if (cookiePanel = document.querySelector('.thoughtco-cookiepanel')) { |
| 14 | + let selectedCategories = this.getConsentSettings(); |
24 | 15 |
|
25 | | - var clickAndInputHandler = function(event) { |
| 16 | + [].forEach.call(this.panelElement.querySelectorAll('input[type="checkbox"]'), (el) => { |
| 17 | + if (selectedCategories.includes(el.value)) { |
| 18 | + el.checked = true; |
| 19 | + } |
| 20 | + }); |
26 | 21 |
|
27 | | - var target = event.target; |
| 22 | + if (! selectedCategories.length) { |
| 23 | + this.panelElement.classList.add('open'); |
| 24 | + } |
28 | 25 |
|
29 | | - // if we are a checkbox |
30 | | - if (target.closest('.toggler')) { |
31 | | - return; |
| 26 | + if (! selectedCategories.includes('functional')) { |
| 27 | + selectedCategories.push('functional'); |
32 | 28 | } |
33 | 29 |
|
34 | | - var categoryEls = cookiePanel.querySelectorAll('input[type="checkbox"]'); |
| 30 | + this.updateScriptConsent(selectedCategories); |
| 31 | + } |
35 | 32 |
|
36 | | - var attr = target.getAttribute('data-cookiepanel'); |
| 33 | + clickAndInputHandler (event) { |
| 34 | + let categoryEls = this.panelElement.querySelectorAll('input[type="checkbox"]'); |
| 35 | + |
| 36 | + let attr = event.target.getAttribute('data-consentpanel'); |
37 | 37 | if (!attr) { |
38 | 38 | return; |
39 | 39 | } |
40 | 40 |
|
41 | | - var autoClose = false; |
| 41 | + if (event.target.tagName.toLowerCase() == 'input') { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + let autoClose = false; |
42 | 46 | switch (attr) { |
43 | 47 | case 'open': |
44 | | - cookiePanel.classList.add('open'); |
| 48 | + this.panelElement.classList.add('open'); |
45 | 49 | return; |
46 | 50 | break; |
47 | 51 |
|
48 | 52 | case 'close': |
49 | | - cookiePanel.classList.remove('open'); |
| 53 | + this.panelElement.classList.remove('open'); |
| 54 | + return; |
50 | 55 | break; |
51 | 56 |
|
52 | 57 | case 'reject': |
53 | | - for (var i=0; i<categoryEls.length; i++) |
| 58 | + for (let i=0; i<categoryEls.length; i++) { |
54 | 59 | categoryEls[i].checked = false; |
| 60 | + } |
55 | 61 |
|
56 | 62 | autoClose = true; |
57 | 63 | break; |
58 | 64 |
|
59 | 65 | case 'accept': |
60 | | - for (var i=0; i<categoryEls.length; i++) |
| 66 | + for (let i=0; i<categoryEls.length; i++) { |
61 | 67 | categoryEls[i].checked = true; |
| 68 | + } |
| 69 | + |
| 70 | + autoClose = true; |
| 71 | + break; |
62 | 72 |
|
| 73 | + case 'select': |
63 | 74 | autoClose = true; |
64 | 75 | break; |
65 | 76 | } |
66 | 77 |
|
67 | | - var selectedCategories = []; |
68 | | - for (var i=0; i<categoryEls.length; i++) { |
| 78 | + let selectedCategories = []; |
| 79 | + for (let i=0; i<categoryEls.length; i++) { |
69 | 80 | if (categoryEls[i].checked) { |
70 | 81 | selectedCategories.push(categoryEls[i].value); |
71 | 82 | } |
72 | 83 | } |
73 | 84 |
|
74 | | - var data = new FormData(); |
75 | | - data.append('_token', cookiePanel.querySelector('[name="_token"]').value); |
76 | | - data.append('categories', selectedCategories.join(',')); |
77 | | - |
78 | | - fetch('/!/statamic-cookiepanel', { |
79 | | - method: 'POST', |
80 | | - body: data, |
81 | | - headers: { |
82 | | - 'X-Requested-With': 'XMLHttpRequest' |
83 | | - }, |
84 | | - }); |
| 85 | + localStorage.setItem('consent_settings', selectedCategories.join(',')); |
85 | 86 |
|
86 | 87 | if (autoClose) { |
87 | | - cookiePanel.classList.remove('open'); |
| 88 | + this.panelElement.classList.remove('open'); |
88 | 89 | } |
89 | 90 |
|
90 | | - window.dispatchEvent(new CustomEvent('statamic-cookiepanel:consent-changed', { |
| 91 | + this.updateScriptConsent(selectedCategories); |
| 92 | + |
| 93 | + window.dispatchEvent(new CustomEvent('statamic-consentpanel:consent-changed', { |
91 | 94 | detail: { |
92 | 95 | categories: selectedCategories, |
93 | 96 | } |
94 | 97 | })); |
95 | | - }; |
| 98 | + } |
| 99 | + |
| 100 | + getConsentSettings() { |
| 101 | + return localStorage.getItem('consent_settings')?.split(',') ?? []; |
| 102 | + } |
| 103 | + |
| 104 | + hasConsentedTo(category) { |
| 105 | + return this.getConsentSettings().includes(category); |
| 106 | + } |
| 107 | + |
| 108 | + updateScriptConsent(categories) { |
| 109 | + [].forEach.call(document.querySelectorAll('[data-consentpanel-type]'), (el) => { |
| 110 | + let id = el.getAttribute('data-consentpanel-id'); |
| 111 | + |
| 112 | + if (! id) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + // consented |
| 117 | + if (categories.includes(el.getAttribute('data-consentpanel-type'))) { |
| 118 | + if (! document.querySelector('[data-consentpanel-output="' + id + '"]')) { |
| 119 | + let div = document.createElement('div'); |
| 120 | + div.innerHTML = el.innerHTML; |
| 121 | + |
| 122 | + let fragment = document.createDocumentFragment(); |
96 | 123 |
|
97 | | - cookiePanel.addEventListener('click', clickAndInputHandler); |
98 | | - } |
| 124 | + for (const child of div.children) { |
| 125 | + let newChild = document.createRange().createContextualFragment(child.outerHTML); |
| 126 | + fragment.appendChild(newChild); |
| 127 | + } |
| 128 | + |
| 129 | + for (const child of fragment.children) { |
| 130 | + child.setAttribute('data-consentpanel-output', id); |
| 131 | + } |
| 132 | + |
| 133 | + el.parentNode.appendChild(fragment); |
| 134 | + } |
| 135 | + |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + // not consented |
| 140 | + if (document.querySelector('[data-consentpanel-output="' + id + '"]')) { |
| 141 | + [].forEach.call(document.querySelectorAll('[data-consentpanel-output="' + id + '"]'), (el) => { |
| 142 | + el.parentNode.removeChild(el); |
| 143 | + }); |
| 144 | + } |
| 145 | + }); |
| 146 | + } |
| 147 | + }; |
99 | 148 |
|
| 149 | + window.ConsentPanel = new ConsentPanel(document.querySelector('.thoughtco-cookiepanel')); |
100 | 150 | }); |
0 commit comments