Skip to content

Commit 3dc9d8a

Browse files
committed
Refactor to 2 js classes that will be load separately
1 parent 99f7d58 commit 3dc9d8a

File tree

8 files changed

+74
-68
lines changed

8 files changed

+74
-68
lines changed

dist/cookies.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/mix-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"/script.js": "/script.js",
32
"/cookies.js": "/cookies.js",
3+
"/modal.js": "/modal.js",
44
"/style.css": "/style.css"
55
}

dist/modal.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/script.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

resources/js/Cookies.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from "axios";
22

3-
export class LaravelCookieConsent {
3+
class LaravelCookieConsent {
44
config;
55

66
constructor(config) {
@@ -82,4 +82,11 @@ export class LaravelCookieConsent {
8282
}
8383
});
8484
}
85-
}
85+
}
86+
87+
/*
88+
Config is set to 1 here, but it is modified by ScriptController depending on the different actions.
89+
*/
90+
91+
window.LaravelCookieConsent = new LaravelCookieConsent({config:1});
92+
window.dispatchEvent(new Event('LARAVEL_COOKIE_CONSENT_SCRIPT_LOAD'));

resources/js/Modal.js

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
1-
export const LaravelCookieModal = {
2-
getValues(cookieConsentNotice, text) {
1+
class LaravelCookieModal {
2+
element;
3+
4+
constructor() {
5+
this.element = document.querySelector('#cookies-policy');
6+
if (!window.LaravelCookieConsent || window.LaravelCookieModal) {
7+
return;
8+
}
9+
10+
if (!this.element) {
11+
return;
12+
}
13+
14+
this.getValues();
15+
this.addEventListeners();
16+
17+
this.element.removeAttribute('data-text');
18+
19+
setTimeout(() => {
20+
this.element.classList.remove('cookies--pre-init');
21+
this.element.classList.remove('cookies--closing');
22+
}, 60);
23+
}
24+
25+
getValues() {
326
this.reset = document.querySelector('.cookiereset');
4-
this.customize = cookieConsentNotice.querySelector('.cookies__btn--customize');
5-
this.details = cookieConsentNotice.querySelectorAll('.cookies__details');
6-
this.acceptAll = cookieConsentNotice.querySelector('.cookiesBtn--accept');
7-
this.acceptEssentials = cookieConsentNotice.querySelector('.cookiesBtn--essentials');
8-
this.configure = cookieConsentNotice.querySelector('.cookies__customize');
9-
this.text = text;
10-
this.cookieConsentNotice = cookieConsentNotice;
11-
},
27+
this.customize = this.element.querySelector('.cookies__btn--customize');
28+
this.details = this.element.querySelectorAll('.cookies__details');
29+
this.acceptAll = this.element.querySelector('.cookiesBtn--accept');
30+
this.acceptEssentials = this.element.querySelector('.cookiesBtn--essentials');
31+
this.configure = this.element.querySelector('.cookies__customize');
32+
this.text = JSON.parse(this.element.getAttribute('data-text'));
33+
}
1234

1335
addEventListeners() {
1436
if (this.reset) {
@@ -22,39 +44,39 @@ export const LaravelCookieModal = {
2244
this.acceptEssentials.addEventListener('submit', (event) => this.acceptEssentialsCookies(event));
2345
this.configure.addEventListener('submit', (event) => this.configureCookies(event));
2446
window.addEventListener('resize', (event) => this.resize(event));
25-
},
47+
}
2648

2749
configureCookies(event) {
2850
event.preventDefault();
2951
window.LaravelCookieConsent.configure(new FormData(event.target));
3052
this.close();
31-
},
53+
}
3254

3355
acceptAllCookies(event) {
3456
event.preventDefault();
3557
window.LaravelCookieConsent.acceptAll()
3658
this.close();
37-
},
59+
}
3860

3961
acceptEssentialsCookies(event) {
4062
event.preventDefault();
4163
window.LaravelCookieConsent.acceptEssentials()
4264
this.close();
43-
},
65+
}
4466

4567
resize() {
46-
if (window.innerHeight <= this.cookieConsentNotice.offsetHeight) {
47-
this.cookieConsentNotice.querySelector('.cookies__sections').style.maxHeight = '50vh';
68+
if (window.innerHeight <= this.element.offsetHeight) {
69+
this.element.querySelector('.cookies__sections').style.maxHeight = '50vh';
4870
} else {
49-
this.cookieConsentNotice.querySelector('.cookies__sections').removeAttribute('style')
71+
this.element.querySelector('.cookies__sections').removeAttribute('style')
5072
}
51-
},
73+
}
5274

5375
toggleExpand(event, el, hide = true) {
5476
event.preventDefault();
5577
event.target.blur();
5678

57-
const element = this.cookieConsentNotice.querySelector(el.getAttribute('href')),
79+
const element = this.element.querySelector(el.getAttribute('href')),
5880
content = element.firstElementChild,
5981
height = content.offsetHeight,
6082
isOpen = element.classList.contains('cookies__expandable--open');
@@ -71,23 +93,23 @@ export const LaravelCookieModal = {
7193
element.removeAttribute('style');
7294
}, 310);
7395

74-
})(this.cookieConsentNotice), 10);
96+
})(this.element), 10);
7597

7698
this.hideNotice(hide, isOpen)
77-
},
99+
}
78100

79101
changeText(hide, isOpen, event) {
80102
if (hide) return;
81103

82104
event.target.textContent = isOpen
83105
? this.text.more
84106
: this.text.less
85-
},
107+
}
86108

87109
hideNotice(hide, isOpen) {
88110
if (!hide) return;
89111

90-
const container = this.cookieConsentNotice.querySelector('.cookies__container'),
112+
const container = this.element.querySelector('.cookies__container'),
91113
containerHeight = container.firstElementChild.offsetHeight;
92114

93115
container.setAttribute('style', 'height:' + (!isOpen ? containerHeight : 0) + 'px');
@@ -101,30 +123,36 @@ export const LaravelCookieModal = {
101123
container.removeAttribute('style');
102124
}, 320);
103125

104-
})(this.cookieConsentNotice), 10);
105-
},
126+
})(this.element), 10);
127+
}
106128

107129
close() {
108-
this.cookieConsentNotice.classList.add('cookies--closing');
130+
this.element.classList.add('cookies--closing');
109131

110132
setTimeout(((cookie) => {
111133
return () => {
112134
if (!cookie.parentNode) return;
113135

114-
let scripts = this.cookieConsentNotice.parentNode.querySelectorAll('[data-cookie-consent]');
136+
let scripts = this.element.parentNode.querySelectorAll('[data-cookie-consent]');
115137

116138
scripts.forEach(script => {
117139
script.parentNode.removeChild(script);
118140
});
119141

120-
this.cookieConsentNotice.parentNode.removeChild(cookie);
142+
this.element.parentNode.removeChild(cookie);
121143
}
122-
})(this.cookieConsentNotice), 210);
123-
},
144+
})(this.element), 210);
145+
}
124146

125147
resetCookies(event) {
126148
event.preventDefault();
127149
if (document.querySelector('#cookies-policy')) return;
128150
window.LaravelCookieConsent.reset()
129-
},
130-
}
151+
}
152+
}
153+
154+
window.LaravelCookieModal = new LaravelCookieModal();
155+
window.LaravelCookieModal.init();
156+
window.addEventListener('LARAVEL_COOKIE_CONSENT_SCRIPT_LOAD', () => {
157+
window.LaravelCookieModal.init();
158+
});

resources/js/script.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

webpack.mix.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
let mix = require('laravel-mix');
22

33
mix.setPublicPath('dist')
4-
.js('resources/js/script.js', 'dist')
54
.js('resources/js/cookies.js', 'dist')
5+
.js('resources/js/modal.js', 'dist')
66
.sass('resources/scss/style.scss', 'dist');

0 commit comments

Comments
 (0)