@dargmuesli/nuxt-cookie-control 3.0.0-beta.1 → 3.0.0-beta.3
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 +7 -0
- package/dist/module.d.ts +3 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +6 -34
- package/dist/runtime/components/CookieControl.vue +162 -67
- package/dist/runtime/components/CookieIframe.vue +1 -1
- package/dist/runtime/locale/en.mjs +1 -1
- package/dist/runtime/methods.d.ts +3 -21
- package/dist/runtime/methods.mjs +2 -110
- package/dist/runtime/plugin.mjs +22 -11
- package/dist/runtime/types.d.ts +6 -3
- package/dist/runtime/types.mjs +3 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -141,6 +141,13 @@ cookies: {
|
|
|
141
141
|
optional: [],
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
+
// The milliseconds from now until expiry of the cookies that are being set by this module.
|
|
145
|
+
cookieExpiryOffsetMs: 1000 * 60 * 60 * 24 * 365, // one year
|
|
146
|
+
|
|
147
|
+
// Names for the cookies that are being set by this module.
|
|
148
|
+
cookieNameIsConsentGiven: 'cookie_control_is_consent_given',
|
|
149
|
+
cookieNameCookiesEnabledIds: 'cookie_control_cookies_enabled_ids',
|
|
150
|
+
|
|
144
151
|
// Switch to toggle the "accept necessary" button.
|
|
145
152
|
isAcceptNecessaryButtonEnabled: true
|
|
146
153
|
|
package/dist/module.d.ts
CHANGED
|
@@ -34,6 +34,9 @@ interface ModuleOptions {
|
|
|
34
34
|
necessary: Cookie[];
|
|
35
35
|
optional: Cookie[];
|
|
36
36
|
};
|
|
37
|
+
cookieExpiryOffsetMs: number;
|
|
38
|
+
cookieNameIsConsentGiven: string;
|
|
39
|
+
cookieNameCookiesEnabledIds: string;
|
|
37
40
|
isAcceptNecessaryButtonEnabled?: boolean;
|
|
38
41
|
isControlButtonEnabled?: boolean;
|
|
39
42
|
isCssEnabled?: boolean;
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,17 +1,8 @@
|
|
|
1
1
|
import { resolve } from 'node:path';
|
|
2
|
-
import { createResolver, defineNuxtModule, addPlugin, addImports, addTemplate,
|
|
2
|
+
import { createResolver, defineNuxtModule, addPlugin, addImports, addTemplate, extendWebpackConfig, resolvePath } from '@nuxt/kit';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
// -- Unbuild CommonJS Shims --
|
|
7
|
-
import __cjs_url__ from 'url';
|
|
8
|
-
import __cjs_path__ from 'path';
|
|
9
|
-
import __cjs_mod__ from 'module';
|
|
10
|
-
const __filename = __cjs_url__.fileURLToPath(import.meta.url);
|
|
11
|
-
const __dirname = __cjs_path__.dirname(__filename);
|
|
12
|
-
const require = __cjs_mod__.createRequire(import.meta.url);
|
|
13
4
|
const name = "@dargmuesli/nuxt-cookie-control";
|
|
14
|
-
const version = "3.0.0-beta.
|
|
5
|
+
const version = "3.0.0-beta.3";
|
|
15
6
|
|
|
16
7
|
const en = {
|
|
17
8
|
acceptAll: "Accept all",
|
|
@@ -20,7 +11,7 @@ const en = {
|
|
|
20
11
|
barTitle: "Cookies",
|
|
21
12
|
blockedIframe: "To see this, please enable functional cookies",
|
|
22
13
|
close: "Close",
|
|
23
|
-
declineAll: "
|
|
14
|
+
declineAll: "Decline all",
|
|
24
15
|
functional: "Functional cookies",
|
|
25
16
|
here: "here",
|
|
26
17
|
manageCookies: "Manage cookies",
|
|
@@ -64,6 +55,9 @@ const DEFAULTS = {
|
|
|
64
55
|
necessary: [],
|
|
65
56
|
optional: []
|
|
66
57
|
},
|
|
58
|
+
cookieExpiryOffsetMs: 1e3 * 60 * 60 * 24 * 365,
|
|
59
|
+
cookieNameIsConsentGiven: "cookie_control_is_consent_given",
|
|
60
|
+
cookieNameCookiesEnabledIds: "cookie_control_cookies_enabled_ids",
|
|
67
61
|
isAcceptNecessaryButtonEnabled: true,
|
|
68
62
|
isControlButtonEnabled: true,
|
|
69
63
|
isCssEnabled: true,
|
|
@@ -116,28 +110,6 @@ export default ${JSON.stringify(
|
|
|
116
110
|
2
|
|
117
111
|
)} as ModuleOptions`
|
|
118
112
|
});
|
|
119
|
-
if (isNuxt2()) {
|
|
120
|
-
try {
|
|
121
|
-
if (require.resolve("nuxt-purgecss").length > 0) {
|
|
122
|
-
if (nuxt.options.purgeCSS) {
|
|
123
|
-
if (nuxt.options.purgeCSS.whitelistPatternsChildren) {
|
|
124
|
-
nuxt.options.purgeCSS.whitelistPatternsChildren.push(
|
|
125
|
-
/cookieControl/
|
|
126
|
-
);
|
|
127
|
-
} else {
|
|
128
|
-
nuxt.options.purgeCSS.whitelistPatternsChildren = [
|
|
129
|
-
/cookieControl/
|
|
130
|
-
];
|
|
131
|
-
}
|
|
132
|
-
} else {
|
|
133
|
-
nuxt.options.purgeCSS = {
|
|
134
|
-
whitelistPatternsChildren: [/cookieControl/]
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
} catch (e) {
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
113
|
}
|
|
142
114
|
});
|
|
143
115
|
const blockIframes = (moduleOptions) => {
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
<section class="cookieControl">
|
|
4
4
|
<transition :name="`cookieControl__Bar--${moduleOptions.barPosition}`">
|
|
5
5
|
<div
|
|
6
|
-
v-if="isColorsSet &&
|
|
6
|
+
v-if="isColorsSet && isConsentGiven === undefined"
|
|
7
7
|
:class="`cookieControl__Bar cookieControl__Bar--${moduleOptions.barPosition}`"
|
|
8
8
|
>
|
|
9
9
|
<div class="cookieControl__BarContainer">
|
|
@@ -14,13 +14,10 @@
|
|
|
14
14
|
</slot>
|
|
15
15
|
</div>
|
|
16
16
|
<div class="cookieControl__BarButtons">
|
|
17
|
-
<button
|
|
18
|
-
@click="setConsent({})"
|
|
19
|
-
v-text="localeStrings?.acceptAll"
|
|
20
|
-
/>
|
|
17
|
+
<button @click="acceptAll()" v-text="localeStrings?.acceptAll" />
|
|
21
18
|
<button
|
|
22
19
|
v-if="moduleOptions.isAcceptNecessaryButtonEnabled"
|
|
23
|
-
@click="acceptNecessary"
|
|
20
|
+
@click="acceptNecessary()"
|
|
24
21
|
v-text="localeStrings?.acceptNecessary"
|
|
25
22
|
/>
|
|
26
23
|
<button
|
|
@@ -33,7 +30,9 @@
|
|
|
33
30
|
</transition>
|
|
34
31
|
<button
|
|
35
32
|
v-if="
|
|
36
|
-
moduleOptions.isControlButtonEnabled &&
|
|
33
|
+
moduleOptions.isControlButtonEnabled &&
|
|
34
|
+
isColorsSet &&
|
|
35
|
+
isConsentGiven !== undefined
|
|
37
36
|
"
|
|
38
37
|
aria-label="Cookie control"
|
|
39
38
|
class="cookieControl__ControlButton"
|
|
@@ -50,7 +49,7 @@
|
|
|
50
49
|
<transition name="cookieControl__Modal">
|
|
51
50
|
<div v-if="isModalActive" class="cookieControl__Modal">
|
|
52
51
|
<p
|
|
53
|
-
v-if="
|
|
52
|
+
v-if="isSaved"
|
|
54
53
|
class="cookieControl__ModalUnsaved"
|
|
55
54
|
v-text="localeStrings?.unsaved"
|
|
56
55
|
/>
|
|
@@ -85,8 +84,12 @@
|
|
|
85
84
|
:id="resolveTranslatable(cookie.name)"
|
|
86
85
|
type="checkbox"
|
|
87
86
|
:checked="
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
getCookieIds(localCookiesEnabled)?.includes(
|
|
88
|
+
getCookieId(cookie)
|
|
89
|
+
) ||
|
|
90
|
+
(Cookies.get(
|
|
91
|
+
moduleOptions.cookieNameIsConsentGiven
|
|
92
|
+
) !== 'true' &&
|
|
90
93
|
typeof moduleOptions.isIframeBlocked === 'object' &&
|
|
91
94
|
moduleOptions.isIframeBlocked.initialState)
|
|
92
95
|
"
|
|
@@ -118,16 +121,29 @@
|
|
|
118
121
|
</div>
|
|
119
122
|
<div class="cookieControl__ModalButtons">
|
|
120
123
|
<button
|
|
121
|
-
@click="
|
|
124
|
+
@click="
|
|
125
|
+
() => {
|
|
126
|
+
acceptPartial()
|
|
127
|
+
isModalActive = false
|
|
128
|
+
}
|
|
129
|
+
"
|
|
122
130
|
v-text="localeStrings?.save"
|
|
123
131
|
/>
|
|
124
132
|
<button
|
|
125
|
-
@click="
|
|
133
|
+
@click="
|
|
134
|
+
() => {
|
|
135
|
+
acceptAll()
|
|
136
|
+
isModalActive = false
|
|
137
|
+
}
|
|
138
|
+
"
|
|
126
139
|
v-text="localeStrings?.acceptAll"
|
|
127
140
|
/>
|
|
128
141
|
<button
|
|
129
142
|
@click="
|
|
130
|
-
|
|
143
|
+
() => {
|
|
144
|
+
declineAll()
|
|
145
|
+
isModalActive = false
|
|
146
|
+
}
|
|
131
147
|
"
|
|
132
148
|
v-text="localeStrings?.declineAll"
|
|
133
149
|
/>
|
|
@@ -141,17 +157,11 @@
|
|
|
141
157
|
</template>
|
|
142
158
|
|
|
143
159
|
<script setup lang="ts">
|
|
144
|
-
import
|
|
160
|
+
import Cookies from 'js-cookie'
|
|
161
|
+
import { ref, computed, onBeforeMount, watch } from 'vue'
|
|
145
162
|
|
|
146
163
|
import { Cookie, CookieType, Locale, Translatable } from '../types'
|
|
147
|
-
import {
|
|
148
|
-
getCookieControlConsent,
|
|
149
|
-
getCookieId,
|
|
150
|
-
useAcceptNecessary,
|
|
151
|
-
useSetConsent,
|
|
152
|
-
useResolveTranslatable,
|
|
153
|
-
setCookies,
|
|
154
|
-
} from '../methods'
|
|
164
|
+
import { getCookieId, getCookieIds, useResolveTranslatable } from '../methods'
|
|
155
165
|
|
|
156
166
|
import { useCookieControl } from '#imports'
|
|
157
167
|
|
|
@@ -162,59 +172,63 @@ const props = withDefaults(defineProps<Props>(), {
|
|
|
162
172
|
locale: 'en',
|
|
163
173
|
})
|
|
164
174
|
|
|
165
|
-
const {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
175
|
+
const {
|
|
176
|
+
cookiesEnabled,
|
|
177
|
+
cookiesEnabledIds,
|
|
178
|
+
isConsentGiven,
|
|
179
|
+
isModalActive,
|
|
180
|
+
moduleOptions,
|
|
181
|
+
} = useCookieControl()
|
|
169
182
|
const resolveTranslatable = useResolveTranslatable(props.locale)
|
|
170
183
|
|
|
171
184
|
// data
|
|
172
|
-
const
|
|
185
|
+
const expires = new Date()
|
|
173
186
|
const isColorsSet = ref(false)
|
|
187
|
+
const localCookiesEnabled = ref([...(cookiesEnabled.value || [])])
|
|
174
188
|
|
|
175
189
|
// computations
|
|
190
|
+
const isSaved = computed(
|
|
191
|
+
() =>
|
|
192
|
+
getCookieIds(cookiesEnabled.value || [])
|
|
193
|
+
.sort()
|
|
194
|
+
.join(',') !== getCookieIds(localCookiesEnabled.value).sort().join(',')
|
|
195
|
+
)
|
|
176
196
|
const localeStrings = computed(() => moduleOptions.localeTexts[props.locale])
|
|
177
197
|
|
|
178
198
|
// methods
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
if (!cookiesEnabledIds.value.includes(cookieId)) {
|
|
185
|
-
cookiesEnabledIds.value.push(cookieId)
|
|
186
|
-
} else {
|
|
187
|
-
cookiesEnabledIds.value.splice(cookiesEnabledIds.value.indexOf(cookieId), 1)
|
|
188
|
-
}
|
|
199
|
+
const acceptAll = () => {
|
|
200
|
+
setCookies({
|
|
201
|
+
isConsentGiven: true,
|
|
202
|
+
cookiesOptionalEnabled: moduleOptions.cookies.optional,
|
|
203
|
+
})
|
|
189
204
|
}
|
|
190
|
-
const
|
|
191
|
-
type = undefined,
|
|
192
|
-
isConsentGiven = true,
|
|
193
|
-
declineAll = false,
|
|
194
|
-
}: {
|
|
195
|
-
type?: 'partial'
|
|
196
|
-
isConsentGiven?: boolean
|
|
197
|
-
declineAll?: boolean
|
|
198
|
-
}) => {
|
|
199
|
-
const cookieIds = declineAll
|
|
200
|
-
? []
|
|
201
|
-
: type === 'partial' && isConsentGiven
|
|
202
|
-
? cookiesEnabledIds.value
|
|
203
|
-
: moduleOptions.cookies.optional.map((cookie: Cookie) =>
|
|
204
|
-
getCookieId(cookie)
|
|
205
|
-
)
|
|
206
|
-
|
|
207
|
-
const expirationDate = new Date()
|
|
208
|
-
expirationDate.setFullYear(expirationDate.getFullYear() + 1)
|
|
209
|
-
|
|
205
|
+
const acceptNecessary = () => {
|
|
210
206
|
setCookies({
|
|
211
|
-
isConsentGiven,
|
|
212
|
-
|
|
213
|
-
expires: expirationDate,
|
|
207
|
+
isConsentGiven: true,
|
|
208
|
+
cookiesOptionalEnabled: moduleOptions.cookies?.necessary,
|
|
214
209
|
})
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
210
|
+
}
|
|
211
|
+
const acceptPartial = () => {
|
|
212
|
+
setCookies({
|
|
213
|
+
isConsentGiven: true,
|
|
214
|
+
cookiesOptionalEnabled: localCookiesEnabled.value,
|
|
215
|
+
})
|
|
216
|
+
}
|
|
217
|
+
const declineAll = () => {
|
|
218
|
+
setCookies({
|
|
219
|
+
isConsentGiven: false,
|
|
220
|
+
cookiesOptionalEnabled: [],
|
|
221
|
+
})
|
|
222
|
+
}
|
|
223
|
+
const toogleCookie = (cookie: Cookie) => {
|
|
224
|
+
if (!localCookiesEnabled.value.includes(cookie)) {
|
|
225
|
+
localCookiesEnabled.value.push(cookie)
|
|
226
|
+
} else {
|
|
227
|
+
localCookiesEnabled.value.splice(
|
|
228
|
+
localCookiesEnabled.value.indexOf(cookie),
|
|
229
|
+
1
|
|
230
|
+
)
|
|
231
|
+
}
|
|
218
232
|
}
|
|
219
233
|
const getDescription = (description: Translatable) =>
|
|
220
234
|
`${
|
|
@@ -227,6 +241,29 @@ const getName = (name: Translatable) => {
|
|
|
227
241
|
? name
|
|
228
242
|
: name[props.locale]
|
|
229
243
|
}
|
|
244
|
+
const init = () => {
|
|
245
|
+
expires.setTime(expires.getTime() + moduleOptions.cookieExpiryOffsetMs)
|
|
246
|
+
}
|
|
247
|
+
const setCookies = ({
|
|
248
|
+
cookiesOptionalEnabled: cookiesOptionalEnabledNew,
|
|
249
|
+
isConsentGiven: isConsentGivenNew,
|
|
250
|
+
}: {
|
|
251
|
+
cookiesOptionalEnabled: Cookie[]
|
|
252
|
+
isConsentGiven: boolean
|
|
253
|
+
}) => {
|
|
254
|
+
isConsentGiven.value = isConsentGivenNew // must come before an update to `cookiesEnabled`
|
|
255
|
+
cookiesEnabled.value = isConsentGivenNew
|
|
256
|
+
? [
|
|
257
|
+
...moduleOptions.cookies.necessary,
|
|
258
|
+
...moduleOptions.cookies.optional.filter((cookieOptional: Cookie) =>
|
|
259
|
+
cookiesOptionalEnabledNew?.includes(cookieOptional)
|
|
260
|
+
),
|
|
261
|
+
]
|
|
262
|
+
: []
|
|
263
|
+
cookiesEnabledIds.value = isConsentGivenNew
|
|
264
|
+
? getCookieIds(cookiesEnabled.value)
|
|
265
|
+
: []
|
|
266
|
+
}
|
|
230
267
|
|
|
231
268
|
// lifecycle
|
|
232
269
|
onBeforeMount(async () => {
|
|
@@ -253,18 +290,76 @@ onBeforeMount(async () => {
|
|
|
253
290
|
isColorsSet.value = true
|
|
254
291
|
}
|
|
255
292
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
if (!cookieControlConsent || !cookieControlConsent.length) {
|
|
293
|
+
if (Cookies.get(moduleOptions.cookieNameIsConsentGiven) === 'true') {
|
|
259
294
|
for (const cookieOptional of moduleOptions.cookies.optional) {
|
|
260
295
|
if (
|
|
261
296
|
typeof moduleOptions.isIframeBlocked === 'boolean'
|
|
262
297
|
? moduleOptions.isIframeBlocked === true
|
|
263
298
|
: moduleOptions.isIframeBlocked?.initialState === true
|
|
264
299
|
) {
|
|
265
|
-
|
|
300
|
+
localCookiesEnabled.value.push(cookieOptional)
|
|
266
301
|
}
|
|
267
302
|
}
|
|
268
303
|
}
|
|
269
304
|
})
|
|
305
|
+
watch(
|
|
306
|
+
() => cookiesEnabled.value,
|
|
307
|
+
(current, _previous) => {
|
|
308
|
+
if (isConsentGiven.value) {
|
|
309
|
+
Cookies.set(
|
|
310
|
+
moduleOptions.cookieNameCookiesEnabledIds,
|
|
311
|
+
getCookieIds(current || []).join(','),
|
|
312
|
+
{
|
|
313
|
+
expires,
|
|
314
|
+
}
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
for (const cookieEnabled of current || []) {
|
|
318
|
+
if (!cookieEnabled.src) continue
|
|
319
|
+
|
|
320
|
+
const script = document.createElement('script')
|
|
321
|
+
script.src = cookieEnabled.src
|
|
322
|
+
document.getElementsByTagName('head')[0].appendChild(script)
|
|
323
|
+
}
|
|
324
|
+
} else {
|
|
325
|
+
Cookies.remove(moduleOptions.cookieNameCookiesEnabledIds)
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// delete formerly enabled cookies that are now disabled
|
|
329
|
+
const cookiesOptionalDisabled = moduleOptions.cookies.optional.filter(
|
|
330
|
+
(cookieOptional) => !(current || []).includes(cookieOptional)
|
|
331
|
+
)
|
|
332
|
+
|
|
333
|
+
for (const cookieOptionalDisabled of cookiesOptionalDisabled) {
|
|
334
|
+
if (!cookieOptionalDisabled.targetCookieIds) continue
|
|
335
|
+
|
|
336
|
+
for (const cookieOptionalDisabledId of cookieOptionalDisabled.targetCookieIds) {
|
|
337
|
+
Cookies.remove(cookieOptionalDisabledId)
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (cookieOptionalDisabled.src) {
|
|
341
|
+
for (const script of [
|
|
342
|
+
...document.head.querySelectorAll(
|
|
343
|
+
`script[src="${cookieOptionalDisabled.src}"]`
|
|
344
|
+
),
|
|
345
|
+
]) {
|
|
346
|
+
script.parentNode?.removeChild(script)
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
{ deep: true }
|
|
352
|
+
)
|
|
353
|
+
watch(isConsentGiven, (current, _previous) => {
|
|
354
|
+
if (current === undefined) {
|
|
355
|
+
Cookies.remove(moduleOptions.cookieNameIsConsentGiven)
|
|
356
|
+
} else {
|
|
357
|
+
Cookies.set(moduleOptions.cookieNameIsConsentGiven, current.toString(), {
|
|
358
|
+
expires,
|
|
359
|
+
})
|
|
360
|
+
}
|
|
361
|
+
})
|
|
362
|
+
|
|
363
|
+
// initialization
|
|
364
|
+
init()
|
|
270
365
|
</script>
|
|
@@ -34,7 +34,7 @@ const { cookiesEnabled, isModalActive, moduleOptions } = useCookieControl()
|
|
|
34
34
|
// computations
|
|
35
35
|
const isCookieFunctionalEnabled = computed(
|
|
36
36
|
() =>
|
|
37
|
-
cookiesEnabled.value.filter(
|
|
37
|
+
(cookiesEnabled.value || []).filter(
|
|
38
38
|
(cookieEnabled: Cookie) => cookieEnabled.name === 'functional'
|
|
39
39
|
).length > 0
|
|
40
40
|
)
|
|
@@ -5,7 +5,7 @@ export default {
|
|
|
5
5
|
barTitle: "Cookies",
|
|
6
6
|
blockedIframe: "To see this, please enable functional cookies",
|
|
7
7
|
close: "Close",
|
|
8
|
-
declineAll: "
|
|
8
|
+
declineAll: "Decline all",
|
|
9
9
|
functional: "Functional cookies",
|
|
10
10
|
here: "here",
|
|
11
11
|
manageCookies: "Manage cookies",
|
|
@@ -1,22 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Cookie, ModuleOptions, Translatable } from './types';
|
|
3
|
-
export declare const useAcceptNecessary: () => () => void;
|
|
4
|
-
export declare const acceptNecessary: (cookiesEnabledRef: Ref<Cookie[]>, cookiesNecessary?: Cookie[]) => void;
|
|
5
|
-
export declare const useResolveTranslatable: (locale?: import("./types").Locale) => (translatable: Translatable) => string;
|
|
6
|
-
export declare const useSetConsent: () => () => void;
|
|
7
|
-
export declare const setConsent: ({ isInit, isConsentGiven, moduleOptions, cookiesEnabled, cookiesEnabledIds, }: {
|
|
8
|
-
isInit: boolean;
|
|
9
|
-
isConsentGiven: Ref<boolean | undefined>;
|
|
10
|
-
moduleOptions: ModuleOptions;
|
|
11
|
-
cookiesEnabled: Ref<Cookie[]>;
|
|
12
|
-
cookiesEnabledIds: Ref<string[]>;
|
|
13
|
-
}) => void;
|
|
14
|
-
export declare const getCookieControlConsent: () => any;
|
|
1
|
+
import { Cookie, Translatable } from './types';
|
|
15
2
|
export declare const getCookieId: (cookie: Cookie) => string;
|
|
16
|
-
export declare const
|
|
17
|
-
export declare const
|
|
18
|
-
export declare const setCookies: ({ isConsentGiven, cookieIds, expires, }: {
|
|
19
|
-
isConsentGiven: boolean;
|
|
20
|
-
cookieIds: string[];
|
|
21
|
-
expires: Date;
|
|
22
|
-
}) => void;
|
|
3
|
+
export declare const getCookieIds: (cookies: Cookie[]) => string[];
|
|
4
|
+
export declare const useResolveTranslatable: (locale?: import("./types").Locale) => (translatable: Translatable) => string;
|
package/dist/runtime/methods.mjs
CHANGED
|
@@ -1,24 +1,7 @@
|
|
|
1
|
-
import Cookies from "js-cookie";
|
|
2
1
|
import slugify from "@sindresorhus/slugify";
|
|
3
|
-
import { useCookieControl } from "./composables.mjs";
|
|
4
2
|
import { LOCALE_DEFAULT } from "./constants.mjs";
|
|
5
|
-
export const
|
|
6
|
-
|
|
7
|
-
return () => acceptNecessary(cookiesEnabled, moduleOptions.cookies?.necessary);
|
|
8
|
-
};
|
|
9
|
-
export const acceptNecessary = (cookiesEnabledRef, cookiesNecessary = []) => {
|
|
10
|
-
const expires = new Date();
|
|
11
|
-
expires.setFullYear(expires.getFullYear() + 1);
|
|
12
|
-
const necessaryCookieIds = cookiesNecessary.map(
|
|
13
|
-
(necessaryCookie) => getCookieId(necessaryCookie)
|
|
14
|
-
);
|
|
15
|
-
setCookies({
|
|
16
|
-
isConsentGiven: true,
|
|
17
|
-
cookieIds: necessaryCookieIds,
|
|
18
|
-
expires
|
|
19
|
-
});
|
|
20
|
-
setHead(cookiesEnabledRef.value);
|
|
21
|
-
};
|
|
3
|
+
export const getCookieId = (cookie) => cookie.id || slugify(resolveTranslatable(cookie.name));
|
|
4
|
+
export const getCookieIds = (cookies) => cookies.map((cookie) => getCookieId(cookie));
|
|
22
5
|
export const useResolveTranslatable = (locale = LOCALE_DEFAULT) => {
|
|
23
6
|
return (translatable) => resolveTranslatable(translatable, locale);
|
|
24
7
|
};
|
|
@@ -32,94 +15,3 @@ const resolveTranslatable = (translatable, locale = LOCALE_DEFAULT) => {
|
|
|
32
15
|
throw new Error(`Could not get translation for locale ${locale}.`);
|
|
33
16
|
return result;
|
|
34
17
|
};
|
|
35
|
-
export const useSetConsent = () => {
|
|
36
|
-
const { isConsentGiven, moduleOptions, cookiesEnabled, cookiesEnabledIds } = useCookieControl();
|
|
37
|
-
return () => setConsent({
|
|
38
|
-
isInit: false,
|
|
39
|
-
isConsentGiven,
|
|
40
|
-
moduleOptions,
|
|
41
|
-
cookiesEnabled,
|
|
42
|
-
cookiesEnabledIds
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
|
-
export const setConsent = ({
|
|
46
|
-
isInit = false,
|
|
47
|
-
isConsentGiven,
|
|
48
|
-
moduleOptions,
|
|
49
|
-
cookiesEnabled,
|
|
50
|
-
cookiesEnabledIds
|
|
51
|
-
}) => {
|
|
52
|
-
isConsentGiven.value = Cookies.get("cookie_control_consent") === "true";
|
|
53
|
-
cookiesEnabled.value = [];
|
|
54
|
-
cookiesEnabledIds.value = [];
|
|
55
|
-
if (isConsentGiven.value) {
|
|
56
|
-
const enabledFromCookie = Cookies.get("cookie_control_enabled_cookies");
|
|
57
|
-
cookiesEnabled.value.push(
|
|
58
|
-
...moduleOptions.cookies.optional.filter(
|
|
59
|
-
(cookieOptional) => enabledFromCookie?.includes(getCookieId(cookieOptional))
|
|
60
|
-
)
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
if (moduleOptions.cookies?.necessary)
|
|
64
|
-
cookiesEnabled.value.push(
|
|
65
|
-
...moduleOptions.cookies.necessary.filter(
|
|
66
|
-
(cookieNecessary) => cookieNecessary.src
|
|
67
|
-
)
|
|
68
|
-
);
|
|
69
|
-
cookiesEnabledIds.value = cookiesEnabled.value.map(
|
|
70
|
-
(cookieEnabled) => getCookieId(cookieEnabled)
|
|
71
|
-
);
|
|
72
|
-
if (process.client && !isInit) {
|
|
73
|
-
setHead(cookiesEnabled.value);
|
|
74
|
-
clearCookies(cookiesEnabledIds.value, moduleOptions.cookies.optional);
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
export const getCookieControlConsent = () => Cookies.get("cookie_control_consent");
|
|
78
|
-
export const getCookieId = (cookie) => cookie.id || slugify(resolveTranslatable(cookie.name));
|
|
79
|
-
export const clearCookies = (cookiesEnabledIds, cookiesOptional) => {
|
|
80
|
-
const cookiesDisabled = cookiesOptional.filter(
|
|
81
|
-
(cookieOptional) => !cookiesEnabledIds.includes(getCookieId(cookieOptional))
|
|
82
|
-
);
|
|
83
|
-
for (const cookieDisabled of cookiesDisabled) {
|
|
84
|
-
if (!cookieDisabled.targetCookieIds)
|
|
85
|
-
continue;
|
|
86
|
-
for (const cookieDisabledId of cookieDisabled.targetCookieIds) {
|
|
87
|
-
Cookies.remove(cookieDisabledId);
|
|
88
|
-
}
|
|
89
|
-
if (cookieDisabled.src) {
|
|
90
|
-
for (const s of [
|
|
91
|
-
...document.head.querySelectorAll(
|
|
92
|
-
`script[src="${cookieDisabled.src}"]`
|
|
93
|
-
)
|
|
94
|
-
]) {
|
|
95
|
-
s.parentNode?.removeChild(s);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
export const setHead = (enabledCookies) => {
|
|
101
|
-
const head = document.getElementsByTagName("head")[0];
|
|
102
|
-
for (const cookieEnabled of enabledCookies) {
|
|
103
|
-
if (!cookieEnabled.src)
|
|
104
|
-
continue;
|
|
105
|
-
const script = document.createElement("script");
|
|
106
|
-
script.src = cookieEnabled.src;
|
|
107
|
-
head.appendChild(script);
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
export const setCookies = ({
|
|
111
|
-
isConsentGiven,
|
|
112
|
-
cookieIds,
|
|
113
|
-
expires
|
|
114
|
-
}) => {
|
|
115
|
-
Cookies.set(
|
|
116
|
-
"cookie_control_enabled_cookies",
|
|
117
|
-
isConsentGiven ? cookieIds.join(",") : "",
|
|
118
|
-
{
|
|
119
|
-
expires
|
|
120
|
-
}
|
|
121
|
-
);
|
|
122
|
-
Cookies.set("cookie_control_consent", isConsentGiven.toString(), {
|
|
123
|
-
expires
|
|
124
|
-
});
|
|
125
|
-
};
|
package/dist/runtime/plugin.mjs
CHANGED
|
@@ -1,11 +1,29 @@
|
|
|
1
|
+
import Cookies from "js-cookie";
|
|
1
2
|
import { ref } from "vue";
|
|
2
|
-
import {
|
|
3
|
+
import { getCookieId } from "./methods.mjs";
|
|
3
4
|
import { defineNuxtPlugin } from "#imports";
|
|
4
5
|
import moduleOptions from "#build/cookie-control-options";
|
|
5
6
|
export default defineNuxtPlugin((_nuxtApp) => {
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
const cookieIsConsentGiven = Cookies.get(
|
|
8
|
+
moduleOptions.cookieNameIsConsentGiven
|
|
9
|
+
);
|
|
10
|
+
const cookieCookiesEnabledIds = Cookies.get(
|
|
11
|
+
moduleOptions.cookieNameCookiesEnabledIds
|
|
12
|
+
)?.split(",");
|
|
13
|
+
const isConsentGiven = ref(
|
|
14
|
+
cookieIsConsentGiven === void 0 ? void 0 : cookieIsConsentGiven === "true"
|
|
15
|
+
);
|
|
16
|
+
const cookiesEnabled = ref(
|
|
17
|
+
cookieCookiesEnabledIds === void 0 ? void 0 : [
|
|
18
|
+
...moduleOptions.cookies.necessary.filter(
|
|
19
|
+
(cookieNecessary) => cookieCookiesEnabledIds.includes(getCookieId(cookieNecessary))
|
|
20
|
+
),
|
|
21
|
+
...moduleOptions.cookies.optional.filter(
|
|
22
|
+
(cookieOptional) => cookieCookiesEnabledIds.includes(getCookieId(cookieOptional))
|
|
23
|
+
)
|
|
24
|
+
]
|
|
25
|
+
);
|
|
26
|
+
const cookiesEnabledIds = ref(cookieCookiesEnabledIds);
|
|
9
27
|
const isModalActive = ref();
|
|
10
28
|
const state = {
|
|
11
29
|
isConsentGiven,
|
|
@@ -14,13 +32,6 @@ export default defineNuxtPlugin((_nuxtApp) => {
|
|
|
14
32
|
isModalActive,
|
|
15
33
|
moduleOptions
|
|
16
34
|
};
|
|
17
|
-
setConsent({
|
|
18
|
-
isInit: !process.client,
|
|
19
|
-
isConsentGiven,
|
|
20
|
-
moduleOptions,
|
|
21
|
-
cookiesEnabled,
|
|
22
|
-
cookiesEnabledIds
|
|
23
|
-
});
|
|
24
35
|
return {
|
|
25
36
|
provide: {
|
|
26
37
|
cookies: state
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -37,6 +37,9 @@ export interface ModuleOptions {
|
|
|
37
37
|
necessary: Cookie[];
|
|
38
38
|
optional: Cookie[];
|
|
39
39
|
};
|
|
40
|
+
cookieExpiryOffsetMs: number;
|
|
41
|
+
cookieNameIsConsentGiven: string;
|
|
42
|
+
cookieNameCookiesEnabledIds: string;
|
|
40
43
|
isAcceptNecessaryButtonEnabled?: boolean;
|
|
41
44
|
isControlButtonEnabled?: boolean;
|
|
42
45
|
isCssEnabled?: boolean;
|
|
@@ -51,9 +54,9 @@ export interface ModuleOptions {
|
|
|
51
54
|
}
|
|
52
55
|
export declare const DEFAULTS: Required<ModuleOptions>;
|
|
53
56
|
export interface State {
|
|
54
|
-
cookiesEnabled: Ref<Cookie[]>;
|
|
55
|
-
cookiesEnabledIds: Ref<string[]>;
|
|
56
|
-
isConsentGiven: Ref<boolean>;
|
|
57
|
+
cookiesEnabled: Ref<Cookie[] | undefined>;
|
|
58
|
+
cookiesEnabledIds: Ref<string[] | undefined>;
|
|
59
|
+
isConsentGiven: Ref<boolean | undefined>;
|
|
57
60
|
isModalActive: Ref<boolean>;
|
|
58
61
|
moduleOptions: ModuleOptions;
|
|
59
62
|
}
|
package/dist/runtime/types.mjs
CHANGED
|
@@ -37,6 +37,9 @@ export const DEFAULTS = {
|
|
|
37
37
|
necessary: [],
|
|
38
38
|
optional: []
|
|
39
39
|
},
|
|
40
|
+
cookieExpiryOffsetMs: 1e3 * 60 * 60 * 24 * 365,
|
|
41
|
+
cookieNameIsConsentGiven: "cookie_control_is_consent_given",
|
|
42
|
+
cookieNameCookiesEnabledIds: "cookie_control_cookies_enabled_ids",
|
|
40
43
|
isAcceptNecessaryButtonEnabled: true,
|
|
41
44
|
isControlButtonEnabled: true,
|
|
42
45
|
isCssEnabled: true,
|