@dargmuesli/nuxt-cookie-control 2.2.4 → 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 +15 -7
- 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 +158 -71
- 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 -111
- 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 +3 -3
package/README.md
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# Nuxt Cookie Control
|
|
2
|
+
[![npm version][npm-version-src]][npm-version-href] [![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
3
|
+
|
|
4
|
+

|
|
5
|
+
|
|
2
6
|
|
|
3
7
|
Continuing Dario Ferderber's work on [gitlab.com/broj42/nuxt-cookie-control](https://gitlab.com/broj42/nuxt-cookie-control).
|
|
4
8
|
|
|
5
|
-

|
|
6
|
-
####
|
|
7
9
|
Try it out here:
|
|
8
10
|
[Nuxt.js Cookie Control](https://codesandbox.io/s/vkwqmm577)
|
|
9
11
|
|
|
@@ -13,8 +15,7 @@ npm i -D @dargmuesli/nuxt-cookie-control
|
|
|
13
15
|
yarn add -D @dargmuesli/nuxt-cookie-control
|
|
14
16
|
pnpm i -D @dargmuesli/nuxt-cookie-control
|
|
15
17
|
```
|
|
16
|
-
|
|
17
|
-
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
18
|
+
|
|
18
19
|
|
|
19
20
|
```javascript
|
|
20
21
|
// nuxt.config.js
|
|
@@ -49,7 +50,7 @@ const {
|
|
|
49
50
|
} = useCookieControl()
|
|
50
51
|
</script>
|
|
51
52
|
```
|
|
52
|
-
##
|
|
53
|
+
## Component Slots
|
|
53
54
|
### Bar
|
|
54
55
|
```html
|
|
55
56
|
<CookieControl>
|
|
@@ -140,6 +141,13 @@ cookies: {
|
|
|
140
141
|
optional: [],
|
|
141
142
|
}
|
|
142
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
|
+
|
|
143
151
|
// Switch to toggle the "accept necessary" button.
|
|
144
152
|
isAcceptNecessaryButtonEnabled: true
|
|
145
153
|
|
|
@@ -188,9 +196,9 @@ Every property the includes a `{ en: ... }` value is a translatable property tha
|
|
|
188
196
|
description: {
|
|
189
197
|
en: 'Used for cookie control.'
|
|
190
198
|
},
|
|
191
|
-
id: 'ga', // if unset, the slugified name
|
|
199
|
+
id: 'ga', // if unset, `getCookieId(cookie)` returns the cookie's slugified name instead, which e.g. is used to fill the state's `enabledCookieIds` list
|
|
192
200
|
name: {
|
|
193
|
-
en: 'Google Analytics'
|
|
201
|
+
en: 'Google Analytics' // you always have to specify a cookie name (in English)
|
|
194
202
|
},
|
|
195
203
|
src: 'https://www.googletagmanager.com/gtag/js?id=<API-KEY>',
|
|
196
204
|
targetCookieIds: ['cookie_control_consent', 'cookie_control_enabled_cookies']
|
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 = "
|
|
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({ reload: false })"
|
|
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,64 +170,62 @@ 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
|
-
reload = true,
|
|
194
|
-
declineAll = false,
|
|
195
|
-
}: {
|
|
196
|
-
type?: 'partial'
|
|
197
|
-
isConsentGiven?: boolean
|
|
198
|
-
reload?: boolean
|
|
199
|
-
declineAll?: boolean
|
|
200
|
-
}) => {
|
|
201
|
-
const cookieIds = declineAll
|
|
202
|
-
? []
|
|
203
|
-
: type === 'partial' && isConsentGiven
|
|
204
|
-
? cookiesEnabledIds.value
|
|
205
|
-
: moduleOptions.cookies.optional.map((cookie: Cookie) =>
|
|
206
|
-
getCookieId(cookie)
|
|
207
|
-
)
|
|
208
|
-
|
|
209
|
-
const expirationDate = new Date()
|
|
210
|
-
expirationDate.setFullYear(expirationDate.getFullYear() + 1)
|
|
211
|
-
|
|
203
|
+
const acceptNecessary = () => {
|
|
212
204
|
setCookies({
|
|
213
|
-
isConsentGiven,
|
|
214
|
-
|
|
215
|
-
expires: expirationDate,
|
|
205
|
+
isConsentGiven: true,
|
|
206
|
+
cookiesOptionalEnabled: moduleOptions.cookies?.necessary,
|
|
216
207
|
})
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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)
|
|
220
224
|
} else {
|
|
221
|
-
|
|
222
|
-
|
|
225
|
+
localCookiesEnabled.value.splice(
|
|
226
|
+
localCookiesEnabled.value.indexOf(cookie),
|
|
227
|
+
1
|
|
228
|
+
)
|
|
223
229
|
}
|
|
224
230
|
}
|
|
225
231
|
const getDescription = (description: Translatable) =>
|
|
@@ -233,6 +239,29 @@ const getName = (name: Translatable) => {
|
|
|
233
239
|
? name
|
|
234
240
|
: name[props.locale]
|
|
235
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
|
+
}
|
|
236
265
|
|
|
237
266
|
// lifecycle
|
|
238
267
|
onBeforeMount(async () => {
|
|
@@ -259,18 +288,76 @@ onBeforeMount(async () => {
|
|
|
259
288
|
isColorsSet.value = true
|
|
260
289
|
}
|
|
261
290
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
if (!cookieControlConsent || !cookieControlConsent.length) {
|
|
291
|
+
if (Cookies.get(moduleOptions.cookieNameIsConsentGiven) === 'true') {
|
|
265
292
|
for (const cookieOptional of moduleOptions.cookies.optional) {
|
|
266
293
|
if (
|
|
267
294
|
typeof moduleOptions.isIframeBlocked === 'boolean'
|
|
268
295
|
? moduleOptions.isIframeBlocked === true
|
|
269
296
|
: moduleOptions.isIframeBlocked?.initialState === true
|
|
270
297
|
) {
|
|
271
|
-
|
|
298
|
+
localCookiesEnabled.value.push(cookieOptional)
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
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
|
+
}
|
|
272
346
|
}
|
|
273
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
|
+
})
|
|
274
358
|
}
|
|
275
359
|
})
|
|
360
|
+
|
|
361
|
+
// initialization
|
|
362
|
+
init()
|
|
276
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,25 +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
|
-
window.location.reload();
|
|
22
|
-
};
|
|
3
|
+
export const getCookieId = (cookie) => cookie.id || slugify(resolveTranslatable(cookie.name));
|
|
4
|
+
export const getCookieIds = (cookies) => cookies.map((cookie) => getCookieId(cookie));
|
|
23
5
|
export const useResolveTranslatable = (locale = LOCALE_DEFAULT) => {
|
|
24
6
|
return (translatable) => resolveTranslatable(translatable, locale);
|
|
25
7
|
};
|
|
@@ -33,94 +15,3 @@ const resolveTranslatable = (translatable, locale = LOCALE_DEFAULT) => {
|
|
|
33
15
|
throw new Error(`Could not get translation for locale ${locale}.`);
|
|
34
16
|
return result;
|
|
35
17
|
};
|
|
36
|
-
export const useSetConsent = () => {
|
|
37
|
-
const { isConsentGiven, moduleOptions, cookiesEnabled, cookiesEnabledIds } = useCookieControl();
|
|
38
|
-
return () => setConsent({
|
|
39
|
-
isInit: false,
|
|
40
|
-
isConsentGiven,
|
|
41
|
-
moduleOptions,
|
|
42
|
-
cookiesEnabled,
|
|
43
|
-
cookiesEnabledIds
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
export const setConsent = ({
|
|
47
|
-
isInit = false,
|
|
48
|
-
isConsentGiven,
|
|
49
|
-
moduleOptions,
|
|
50
|
-
cookiesEnabled,
|
|
51
|
-
cookiesEnabledIds
|
|
52
|
-
}) => {
|
|
53
|
-
isConsentGiven.value = Cookies.get("cookie_control_consent") === "true";
|
|
54
|
-
cookiesEnabled.value = [];
|
|
55
|
-
cookiesEnabledIds.value = [];
|
|
56
|
-
if (isConsentGiven.value) {
|
|
57
|
-
const enabledFromCookie = Cookies.get("cookie_control_enabled_cookies");
|
|
58
|
-
cookiesEnabled.value.push(
|
|
59
|
-
...moduleOptions.cookies.optional.filter(
|
|
60
|
-
(cookieOptional) => enabledFromCookie?.includes(getCookieId(cookieOptional))
|
|
61
|
-
)
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
if (moduleOptions.cookies?.necessary)
|
|
65
|
-
cookiesEnabled.value.push(
|
|
66
|
-
...moduleOptions.cookies.necessary.filter(
|
|
67
|
-
(cookieNecessary) => cookieNecessary.src
|
|
68
|
-
)
|
|
69
|
-
);
|
|
70
|
-
cookiesEnabledIds.value = cookiesEnabled.value.map(
|
|
71
|
-
(cookieEnabled) => getCookieId(cookieEnabled)
|
|
72
|
-
);
|
|
73
|
-
if (process.client && !isInit) {
|
|
74
|
-
setHead(cookiesEnabled.value);
|
|
75
|
-
clearCookies(cookiesEnabledIds.value, moduleOptions.cookies.optional);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
export const getCookieControlConsent = () => Cookies.get("cookie_control_consent");
|
|
79
|
-
export const getCookieId = (cookie) => cookie.id || slugify(resolveTranslatable(cookie.name));
|
|
80
|
-
export const clearCookies = (cookiesEnabledIds, cookiesOptional) => {
|
|
81
|
-
const cookiesDisabled = cookiesOptional.filter(
|
|
82
|
-
(cookieOptional) => !cookiesEnabledIds.includes(getCookieId(cookieOptional))
|
|
83
|
-
);
|
|
84
|
-
for (const cookieDisabled of cookiesDisabled) {
|
|
85
|
-
if (!cookieDisabled.targetCookieIds)
|
|
86
|
-
continue;
|
|
87
|
-
for (const cookieDisabledId of cookieDisabled.targetCookieIds) {
|
|
88
|
-
Cookies.remove(cookieDisabledId);
|
|
89
|
-
}
|
|
90
|
-
if (cookieDisabled.src) {
|
|
91
|
-
for (const s of [
|
|
92
|
-
...document.head.querySelectorAll(
|
|
93
|
-
`script[src="${cookieDisabled.src}"]`
|
|
94
|
-
)
|
|
95
|
-
]) {
|
|
96
|
-
s.parentNode?.removeChild(s);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
export const setHead = (enabledCookies) => {
|
|
102
|
-
const head = document.getElementsByTagName("head")[0];
|
|
103
|
-
for (const cookieEnabled of enabledCookies) {
|
|
104
|
-
if (!cookieEnabled.src)
|
|
105
|
-
continue;
|
|
106
|
-
const script = document.createElement("script");
|
|
107
|
-
script.src = cookieEnabled.src;
|
|
108
|
-
head.appendChild(script);
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
export const setCookies = ({
|
|
112
|
-
isConsentGiven,
|
|
113
|
-
cookieIds,
|
|
114
|
-
expires
|
|
115
|
-
}) => {
|
|
116
|
-
Cookies.set(
|
|
117
|
-
"cookie_control_enabled_cookies",
|
|
118
|
-
isConsentGiven ? cookieIds.join(",") : "",
|
|
119
|
-
{
|
|
120
|
-
expires
|
|
121
|
-
}
|
|
122
|
-
);
|
|
123
|
-
Cookies.set("cookie_control_consent", isConsentGiven.toString(), {
|
|
124
|
-
expires
|
|
125
|
-
});
|
|
126
|
-
};
|
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,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dargmuesli/nuxt-cookie-control",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-beta.2",
|
|
4
4
|
"description": "Nuxt Cookies Control Module",
|
|
5
5
|
"author": "Dario Ferderber <dario.ferderber@broj42.com>",
|
|
6
6
|
"maintainers": [
|
|
@@ -52,10 +52,10 @@
|
|
|
52
52
|
"husky": "8.0.3",
|
|
53
53
|
"lint-staged": "13.1.0",
|
|
54
54
|
"nuxt": "3.0.0",
|
|
55
|
-
"prettier": "2.8.
|
|
55
|
+
"prettier": "2.8.2",
|
|
56
56
|
"typescript": "4.9.4",
|
|
57
57
|
"vue": "3.2.45",
|
|
58
|
-
"vue-tsc": "1.0.
|
|
58
|
+
"vue-tsc": "1.0.24",
|
|
59
59
|
"webpack": "5.75.0"
|
|
60
60
|
},
|
|
61
61
|
"resolutions": {
|