@dargmuesli/nuxt-cookie-control 3.0.0-beta.1 → 3.0.0-beta.2
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 +160 -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.2";
|
|
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,10 @@
|
|
|
85
84
|
:id="resolveTranslatable(cookie.name)"
|
|
86
85
|
type="checkbox"
|
|
87
86
|
:checked="
|
|
88
|
-
|
|
89
|
-
(
|
|
87
|
+
cookiesEnabled?.includes(cookie) ||
|
|
88
|
+
(Cookies.get(
|
|
89
|
+
moduleOptions.cookieNameIsConsentGiven
|
|
90
|
+
) !== 'true' &&
|
|
90
91
|
typeof moduleOptions.isIframeBlocked === 'object' &&
|
|
91
92
|
moduleOptions.isIframeBlocked.initialState)
|
|
92
93
|
"
|
|
@@ -118,16 +119,29 @@
|
|
|
118
119
|
</div>
|
|
119
120
|
<div class="cookieControl__ModalButtons">
|
|
120
121
|
<button
|
|
121
|
-
@click="
|
|
122
|
+
@click="
|
|
123
|
+
() => {
|
|
124
|
+
acceptPartial()
|
|
125
|
+
isModalActive = false
|
|
126
|
+
}
|
|
127
|
+
"
|
|
122
128
|
v-text="localeStrings?.save"
|
|
123
129
|
/>
|
|
124
130
|
<button
|
|
125
|
-
@click="
|
|
131
|
+
@click="
|
|
132
|
+
() => {
|
|
133
|
+
acceptAll()
|
|
134
|
+
isModalActive = false
|
|
135
|
+
}
|
|
136
|
+
"
|
|
126
137
|
v-text="localeStrings?.acceptAll"
|
|
127
138
|
/>
|
|
128
139
|
<button
|
|
129
140
|
@click="
|
|
130
|
-
|
|
141
|
+
() => {
|
|
142
|
+
declineAll()
|
|
143
|
+
isModalActive = false
|
|
144
|
+
}
|
|
131
145
|
"
|
|
132
146
|
v-text="localeStrings?.declineAll"
|
|
133
147
|
/>
|
|
@@ -141,17 +155,11 @@
|
|
|
141
155
|
</template>
|
|
142
156
|
|
|
143
157
|
<script setup lang="ts">
|
|
144
|
-
import
|
|
158
|
+
import Cookies from 'js-cookie'
|
|
159
|
+
import { ref, computed, onBeforeMount, watch } from 'vue'
|
|
145
160
|
|
|
146
161
|
import { Cookie, CookieType, Locale, Translatable } from '../types'
|
|
147
|
-
import {
|
|
148
|
-
getCookieControlConsent,
|
|
149
|
-
getCookieId,
|
|
150
|
-
useAcceptNecessary,
|
|
151
|
-
useSetConsent,
|
|
152
|
-
useResolveTranslatable,
|
|
153
|
-
setCookies,
|
|
154
|
-
} from '../methods'
|
|
162
|
+
import { getCookieIds, useResolveTranslatable } from '../methods'
|
|
155
163
|
|
|
156
164
|
import { useCookieControl } from '#imports'
|
|
157
165
|
|
|
@@ -162,59 +170,63 @@ const props = withDefaults(defineProps<Props>(), {
|
|
|
162
170
|
locale: 'en',
|
|
163
171
|
})
|
|
164
172
|
|
|
165
|
-
const {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
173
|
+
const {
|
|
174
|
+
cookiesEnabled,
|
|
175
|
+
cookiesEnabledIds,
|
|
176
|
+
isConsentGiven,
|
|
177
|
+
isModalActive,
|
|
178
|
+
moduleOptions,
|
|
179
|
+
} = useCookieControl()
|
|
169
180
|
const resolveTranslatable = useResolveTranslatable(props.locale)
|
|
170
181
|
|
|
171
182
|
// data
|
|
172
|
-
const
|
|
183
|
+
const expires = new Date()
|
|
173
184
|
const isColorsSet = ref(false)
|
|
185
|
+
const localCookiesEnabled = ref([...(cookiesEnabled.value || [])])
|
|
174
186
|
|
|
175
187
|
// computations
|
|
188
|
+
const isSaved = computed(
|
|
189
|
+
() =>
|
|
190
|
+
getCookieIds(cookiesEnabled.value || [])
|
|
191
|
+
.sort()
|
|
192
|
+
.join(',') !== getCookieIds(localCookiesEnabled.value).sort().join(',')
|
|
193
|
+
)
|
|
176
194
|
const localeStrings = computed(() => moduleOptions.localeTexts[props.locale])
|
|
177
195
|
|
|
178
196
|
// 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
|
-
}
|
|
197
|
+
const acceptAll = () => {
|
|
198
|
+
setCookies({
|
|
199
|
+
isConsentGiven: true,
|
|
200
|
+
cookiesOptionalEnabled: moduleOptions.cookies.optional,
|
|
201
|
+
})
|
|
189
202
|
}
|
|
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
|
-
|
|
203
|
+
const acceptNecessary = () => {
|
|
210
204
|
setCookies({
|
|
211
|
-
isConsentGiven,
|
|
212
|
-
|
|
213
|
-
expires: expirationDate,
|
|
205
|
+
isConsentGiven: true,
|
|
206
|
+
cookiesOptionalEnabled: moduleOptions.cookies?.necessary,
|
|
214
207
|
})
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
208
|
+
}
|
|
209
|
+
const acceptPartial = () => {
|
|
210
|
+
setCookies({
|
|
211
|
+
isConsentGiven: true,
|
|
212
|
+
cookiesOptionalEnabled: localCookiesEnabled.value,
|
|
213
|
+
})
|
|
214
|
+
}
|
|
215
|
+
const declineAll = () => {
|
|
216
|
+
setCookies({
|
|
217
|
+
isConsentGiven: false,
|
|
218
|
+
cookiesOptionalEnabled: [],
|
|
219
|
+
})
|
|
220
|
+
}
|
|
221
|
+
const toogleCookie = (cookie: Cookie) => {
|
|
222
|
+
if (!localCookiesEnabled.value.includes(cookie)) {
|
|
223
|
+
localCookiesEnabled.value.push(cookie)
|
|
224
|
+
} else {
|
|
225
|
+
localCookiesEnabled.value.splice(
|
|
226
|
+
localCookiesEnabled.value.indexOf(cookie),
|
|
227
|
+
1
|
|
228
|
+
)
|
|
229
|
+
}
|
|
218
230
|
}
|
|
219
231
|
const getDescription = (description: Translatable) =>
|
|
220
232
|
`${
|
|
@@ -227,6 +239,29 @@ const getName = (name: Translatable) => {
|
|
|
227
239
|
? name
|
|
228
240
|
: name[props.locale]
|
|
229
241
|
}
|
|
242
|
+
const init = () => {
|
|
243
|
+
expires.setTime(expires.getTime() + moduleOptions.cookieExpiryOffsetMs)
|
|
244
|
+
}
|
|
245
|
+
const setCookies = ({
|
|
246
|
+
cookiesOptionalEnabled: cookiesOptionalEnabledNew,
|
|
247
|
+
isConsentGiven: isConsentGivenNew,
|
|
248
|
+
}: {
|
|
249
|
+
cookiesOptionalEnabled: Cookie[]
|
|
250
|
+
isConsentGiven: boolean
|
|
251
|
+
}) => {
|
|
252
|
+
isConsentGiven.value = isConsentGivenNew // must come before an update to `cookiesEnabled`
|
|
253
|
+
cookiesEnabled.value = isConsentGivenNew
|
|
254
|
+
? [
|
|
255
|
+
...moduleOptions.cookies.necessary,
|
|
256
|
+
...moduleOptions.cookies.optional.filter((cookieOptional: Cookie) =>
|
|
257
|
+
cookiesOptionalEnabledNew?.includes(cookieOptional)
|
|
258
|
+
),
|
|
259
|
+
]
|
|
260
|
+
: []
|
|
261
|
+
cookiesEnabledIds.value = isConsentGivenNew
|
|
262
|
+
? getCookieIds(cookiesEnabled.value)
|
|
263
|
+
: []
|
|
264
|
+
}
|
|
230
265
|
|
|
231
266
|
// lifecycle
|
|
232
267
|
onBeforeMount(async () => {
|
|
@@ -253,18 +288,76 @@ onBeforeMount(async () => {
|
|
|
253
288
|
isColorsSet.value = true
|
|
254
289
|
}
|
|
255
290
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
if (!cookieControlConsent || !cookieControlConsent.length) {
|
|
291
|
+
if (Cookies.get(moduleOptions.cookieNameIsConsentGiven) === 'true') {
|
|
259
292
|
for (const cookieOptional of moduleOptions.cookies.optional) {
|
|
260
293
|
if (
|
|
261
294
|
typeof moduleOptions.isIframeBlocked === 'boolean'
|
|
262
295
|
? moduleOptions.isIframeBlocked === true
|
|
263
296
|
: moduleOptions.isIframeBlocked?.initialState === true
|
|
264
297
|
) {
|
|
265
|
-
|
|
298
|
+
localCookiesEnabled.value.push(cookieOptional)
|
|
266
299
|
}
|
|
267
300
|
}
|
|
268
301
|
}
|
|
269
302
|
})
|
|
303
|
+
watch(
|
|
304
|
+
() => cookiesEnabled.value,
|
|
305
|
+
(current, _previous) => {
|
|
306
|
+
if (isConsentGiven.value) {
|
|
307
|
+
Cookies.set(
|
|
308
|
+
moduleOptions.cookieNameCookiesEnabledIds,
|
|
309
|
+
getCookieIds(current || []).join(','),
|
|
310
|
+
{
|
|
311
|
+
expires,
|
|
312
|
+
}
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
for (const cookieEnabled of current || []) {
|
|
316
|
+
if (!cookieEnabled.src) continue
|
|
317
|
+
|
|
318
|
+
const script = document.createElement('script')
|
|
319
|
+
script.src = cookieEnabled.src
|
|
320
|
+
document.getElementsByTagName('head')[0].appendChild(script)
|
|
321
|
+
}
|
|
322
|
+
} else {
|
|
323
|
+
Cookies.remove(moduleOptions.cookieNameCookiesEnabledIds)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// delete formerly enabled cookies that are now disabled
|
|
327
|
+
const cookiesOptionalDisabled = moduleOptions.cookies.optional.filter(
|
|
328
|
+
(cookieOptional) => !(current || []).includes(cookieOptional)
|
|
329
|
+
)
|
|
330
|
+
|
|
331
|
+
for (const cookieOptionalDisabled of cookiesOptionalDisabled) {
|
|
332
|
+
if (!cookieOptionalDisabled.targetCookieIds) continue
|
|
333
|
+
|
|
334
|
+
for (const cookieOptionalDisabledId of cookieOptionalDisabled.targetCookieIds) {
|
|
335
|
+
Cookies.remove(cookieOptionalDisabledId)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (cookieOptionalDisabled.src) {
|
|
339
|
+
for (const script of [
|
|
340
|
+
...document.head.querySelectorAll(
|
|
341
|
+
`script[src="${cookieOptionalDisabled.src}"]`
|
|
342
|
+
),
|
|
343
|
+
]) {
|
|
344
|
+
script.parentNode?.removeChild(script)
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
{ deep: true }
|
|
350
|
+
)
|
|
351
|
+
watch(isConsentGiven, (current, _previous) => {
|
|
352
|
+
if (current === undefined) {
|
|
353
|
+
Cookies.remove(moduleOptions.cookieNameIsConsentGiven)
|
|
354
|
+
} else {
|
|
355
|
+
Cookies.set(moduleOptions.cookieNameIsConsentGiven, current.toString(), {
|
|
356
|
+
expires,
|
|
357
|
+
})
|
|
358
|
+
}
|
|
359
|
+
})
|
|
360
|
+
|
|
361
|
+
// initialization
|
|
362
|
+
init()
|
|
270
363
|
</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,
|