@nationalarchives/frontend 1.1.0 → 1.2.0
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/nationalarchives/all+analytics.js +1 -1
- package/nationalarchives/all+analytics.js.map +1 -1
- package/nationalarchives/all+analytics.mjs +0 -2
- package/nationalarchives/all.css +1 -1
- package/nationalarchives/all.css.map +1 -1
- package/nationalarchives/all.js +1 -1
- package/nationalarchives/all.js.map +1 -1
- package/nationalarchives/all.mjs +0 -2
- package/nationalarchives/analytics.js +1 -1
- package/nationalarchives/analytics.js.map +1 -1
- package/nationalarchives/analytics.mjs +4 -3
- package/nationalarchives/assets/fonts/fa-brands-400.woff2 +0 -0
- package/nationalarchives/assets/fonts/fa-solid-900.woff2 +0 -0
- package/nationalarchives/components/accordion/accordion.js +1 -1
- package/nationalarchives/components/accordion/accordion.js.map +1 -1
- package/nationalarchives/components/breadcrumbs/breadcrumbs.js +1 -1
- package/nationalarchives/components/breadcrumbs/breadcrumbs.js.map +1 -1
- package/nationalarchives/components/code-block/code-block.js +1 -1
- package/nationalarchives/components/code-block/code-block.js.map +1 -1
- package/nationalarchives/components/cookie-banner/cookie-banner.js +1 -1
- package/nationalarchives/components/cookie-banner/cookie-banner.js.map +1 -1
- package/nationalarchives/components/cookie-banner/cookie-banner.mjs +5 -5
- package/nationalarchives/components/date-input/date-input.js +1 -1
- package/nationalarchives/components/date-input/date-input.js.map +1 -1
- package/nationalarchives/components/date-input/date-input.mjs +0 -4
- package/nationalarchives/components/error-summary/error-summary.js +1 -1
- package/nationalarchives/components/error-summary/error-summary.js.map +1 -1
- package/nationalarchives/components/file-input/file-input.js +1 -1
- package/nationalarchives/components/file-input/file-input.js.map +1 -1
- package/nationalarchives/components/file-input/file-input.mjs +3 -13
- package/nationalarchives/components/files-list/macro-options.json +1 -1
- package/nationalarchives/components/footer/fixtures.json +2 -2
- package/nationalarchives/components/footer/footer.css +1 -1
- package/nationalarchives/components/footer/footer.css.map +1 -1
- package/nationalarchives/components/footer/footer.js +1 -1
- package/nationalarchives/components/footer/footer.js.map +1 -1
- package/nationalarchives/components/footer/footer.mjs +5 -5
- package/nationalarchives/components/footer/template.njk +1 -1
- package/nationalarchives/components/gallery/gallery.js +1 -1
- package/nationalarchives/components/gallery/gallery.js.map +1 -1
- package/nationalarchives/components/global-header/global-header.css +1 -1
- package/nationalarchives/components/global-header/global-header.css.map +1 -1
- package/nationalarchives/components/global-header/global-header.js +1 -1
- package/nationalarchives/components/global-header/global-header.js.map +1 -1
- package/nationalarchives/components/header/header.css +1 -1
- package/nationalarchives/components/header/header.css.map +1 -1
- package/nationalarchives/components/header/header.js +1 -1
- package/nationalarchives/components/header/header.js.map +1 -1
- package/nationalarchives/components/hero/hero.css +1 -1
- package/nationalarchives/components/hero/hero.css.map +1 -1
- package/nationalarchives/components/hero/hero.scss +1 -1
- package/nationalarchives/components/picture/picture.js +1 -1
- package/nationalarchives/components/picture/picture.js.map +1 -1
- package/nationalarchives/components/sidebar/fixtures.json +14 -6
- package/nationalarchives/components/sidebar/macro-options.json +56 -0
- package/nationalarchives/components/sidebar/sidebar.js +1 -1
- package/nationalarchives/components/sidebar/sidebar.js.map +1 -1
- package/nationalarchives/components/sidebar/template.njk +3 -3
- package/nationalarchives/components/skip-link/skip-link.js +1 -1
- package/nationalarchives/components/skip-link/skip-link.js.map +1 -1
- package/nationalarchives/components/tabs/tabs.js +1 -1
- package/nationalarchives/components/tabs/tabs.js.map +1 -1
- package/nationalarchives/components/text-input/text-input.js +1 -1
- package/nationalarchives/components/text-input/text-input.js.map +1 -1
- package/nationalarchives/components/textarea/textarea.js +1 -1
- package/nationalarchives/components/textarea/textarea.js.map +1 -1
- package/nationalarchives/error-page.css +1 -1
- package/nationalarchives/error-page.css.map +1 -1
- package/nationalarchives/font-awesome.css +5 -5
- package/nationalarchives/font-awesome.css.map +1 -1
- package/nationalarchives/ie.css +1 -1
- package/nationalarchives/ie.css.map +1 -1
- package/nationalarchives/print.css +1 -1
- package/nationalarchives/print.css.map +1 -1
- package/nationalarchives/print.scss +17 -1
- package/nationalarchives/prototype-kit.css +1 -1
- package/nationalarchives/prototype-kit.css.map +1 -1
- package/nationalarchives/utilities/overrides/_index.scss +0 -6
- package/package.json +17 -19
- package/nationalarchives/lib/cookie-events.mjs +0 -70
- package/nationalarchives/lib/cookies.mjs +0 -398
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nationalarchives/frontend",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "The National Archives frontend styles",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"start": "storybook dev -p 6006",
|
|
@@ -10,14 +10,13 @@
|
|
|
10
10
|
"build:errorpages": "npm run compile:sass && npm run compile:scripts && node tasks/static-error-pages/generate.js",
|
|
11
11
|
"compile:sass": "sass --style=compressed --load-path=node_modules --embed-sources src/nationalarchives:package/nationalarchives",
|
|
12
12
|
"compile:scripts": "webpack",
|
|
13
|
+
"lint:check": "prettier --check '{src,.storybook,tasks,.}/**/*.{js,mjs,scss,json,html}' && stylelint 'src/**/*.scss' && eslint 'src/**/*.{js,mjs}'",
|
|
13
14
|
"lint:fix": "prettier --write --list-different '{src,.storybook,tasks,.}/**/*.{js,mjs,scss,json,html}' && stylelint --fix 'src/**/*.scss' && eslint --fix 'src/**/*.{js,mjs}'",
|
|
14
|
-
"test:all": "npm run
|
|
15
|
+
"test:all": "npm run lint:check && npm run test:statichtml && npm run test:fixtures && npm run test:vitest && npm run build:package && npm run test:package",
|
|
15
16
|
"test:fixtures": "node tasks/test-fixtures.js",
|
|
16
17
|
"test:statichtml": "node tasks/generate-fixture-html.js && html-validate fixtures-html",
|
|
17
|
-
"test:lint": "prettier --check '{src,.storybook,tasks,.}/**/*.{js,mjs,scss,json,html}' && stylelint 'src/**/*.scss' && eslint 'src/**/*.{js,mjs}'",
|
|
18
18
|
"test:package": "node tasks/test-package.js",
|
|
19
|
-
"test:
|
|
20
|
-
"test:unit": "jest --verbose",
|
|
19
|
+
"test:vitest": "vitest run",
|
|
21
20
|
"update:fixtures": "node tasks/update-fixtures.js"
|
|
22
21
|
},
|
|
23
22
|
"repository": {
|
|
@@ -45,35 +44,34 @@
|
|
|
45
44
|
"sass": "nationalarchives/all.scss",
|
|
46
45
|
"homepage": "https://github.com/nationalarchives/tna-frontend#readme",
|
|
47
46
|
"dependencies": {
|
|
48
|
-
"@fortawesome/fontawesome-free": "^7.2.0"
|
|
47
|
+
"@fortawesome/fontawesome-free": "^7.2.0",
|
|
48
|
+
"@nationalarchives/cookies": "^0.1.5"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@babel/core": "^8.0.0",
|
|
52
52
|
"@babel/preset-env": "^8.0.0",
|
|
53
53
|
"@mdx-js/react": "^3.0.1",
|
|
54
|
-
"@nationalarchives/stylelint-config": "^0.1.1",
|
|
55
54
|
"@nationalarchives/eslint-config": "^0.1.4",
|
|
56
|
-
"@
|
|
57
|
-
"@storybook/addon-
|
|
58
|
-
"@storybook/addon-
|
|
55
|
+
"@nationalarchives/stylelint-config": "^0.1.1",
|
|
56
|
+
"@storybook/addon-a11y": "^10.5.5",
|
|
57
|
+
"@storybook/addon-docs": "^10.5.5",
|
|
58
|
+
"@storybook/addon-links": "^10.5.5",
|
|
59
59
|
"@storybook/addon-styling-webpack": "^3.0.0",
|
|
60
|
-
"@storybook/addon-vitest": "^10.
|
|
61
|
-
"@storybook/html-vite": "^10.
|
|
62
|
-
"@storybook/web-components-vite": "^10.
|
|
60
|
+
"@storybook/addon-vitest": "^10.5.5",
|
|
61
|
+
"@storybook/html-vite": "^10.5.5",
|
|
62
|
+
"@storybook/web-components-vite": "^10.5.5",
|
|
63
63
|
"@vitest/browser-playwright": "^4.0.6",
|
|
64
64
|
"@vitest/coverage-v8": "^4.0.6",
|
|
65
65
|
"axe-playwright": "^2.0.3",
|
|
66
|
-
"babel-jest": "^30.1.1",
|
|
67
66
|
"babel-loader": "^10.0.0",
|
|
68
|
-
"chromatic": "^
|
|
67
|
+
"chromatic": "^18.0.0",
|
|
69
68
|
"copy-webpack-plugin": "^14.0.0",
|
|
70
69
|
"css-loader": "^7.1.2",
|
|
71
70
|
"diff": "^9.0.0",
|
|
72
|
-
"eslint-plugin-storybook": "^10.
|
|
71
|
+
"eslint-plugin-storybook": "^10.5.5",
|
|
73
72
|
"glob": "^13.0.0",
|
|
74
73
|
"html-validate": "^11.0.0",
|
|
75
|
-
"
|
|
76
|
-
"jest-environment-jsdom": "^30.1.1",
|
|
74
|
+
"jsdom": "^30.0.0",
|
|
77
75
|
"node-self": "^1.0.2",
|
|
78
76
|
"nunjucks": "^3.2.4",
|
|
79
77
|
"playwright": "^1.56.1",
|
|
@@ -83,7 +81,7 @@
|
|
|
83
81
|
"sass-embedded": "^1.91.0",
|
|
84
82
|
"sass-loader": "^17.0.0",
|
|
85
83
|
"simple-nunjucks-loader": "^3.2.0",
|
|
86
|
-
"storybook": "^10.
|
|
84
|
+
"storybook": "^10.5.5",
|
|
87
85
|
"style-loader": "^4.0.0",
|
|
88
86
|
"vite-plugin-static-copy": "^4.0.0",
|
|
89
87
|
"vitest": "^4.0.6",
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
window.TNAFrontendCookieEvents ||= null;
|
|
2
|
-
|
|
3
|
-
export class CookieEventHandler {
|
|
4
|
-
events = {};
|
|
5
|
-
oneTimeEvents = {};
|
|
6
|
-
debug = false;
|
|
7
|
-
|
|
8
|
-
constructor(debug = false) {
|
|
9
|
-
this.debug = debug;
|
|
10
|
-
if (window.TNAFrontendCookieEvents) {
|
|
11
|
-
this.log("Using existing TNAFrontendCookieEvents instance");
|
|
12
|
-
/* eslint-disable-next-line no-constructor-return */
|
|
13
|
-
return window.TNAFrontendCookieEvents;
|
|
14
|
-
}
|
|
15
|
-
window.TNAFrontendCookieEvents = this;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
log(...args) {
|
|
19
|
-
if (this.debug) {
|
|
20
|
-
/* eslint-disable-next-line no-console */
|
|
21
|
-
console.log("[TNA Frontend Cookie Events]", ...args);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Add an event listener.
|
|
27
|
-
* @param {String} event - The event to add a listener for.
|
|
28
|
-
* @param {Function} callback - The callback function to call when the event is triggered.
|
|
29
|
-
*/
|
|
30
|
-
on(event, callback) {
|
|
31
|
-
if (!Object.hasOwn(this.events, event)) {
|
|
32
|
-
this.events[event] = [];
|
|
33
|
-
}
|
|
34
|
-
this.events[event] = [...this.events[event], callback];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
once(event, callback) {
|
|
38
|
-
if (!Object.hasOwn(this.oneTimeEvents, event)) {
|
|
39
|
-
this.oneTimeEvents[event] = [];
|
|
40
|
-
}
|
|
41
|
-
this.oneTimeEvents[event] = [...this.oneTimeEvents[event], callback];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/** @protected */
|
|
45
|
-
trigger(event, data = {}) {
|
|
46
|
-
if (Object.hasOwn(this.events, event)) {
|
|
47
|
-
this.log(`Triggering event: ${event}`, data);
|
|
48
|
-
this.events[event].forEach((eventToTrigger) =>
|
|
49
|
-
eventToTrigger.call(this, data),
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
if (Object.hasOwn(this.oneTimeEvents, event)) {
|
|
53
|
-
this.log(`Triggering one-time event: ${event}`, data);
|
|
54
|
-
|
|
55
|
-
for (
|
|
56
|
-
/* eslint-disable-next-line no-magic-numbers */
|
|
57
|
-
let eventIndex = this.oneTimeEvents[event].length - 1;
|
|
58
|
-
/* eslint-disable-next-line no-magic-numbers */
|
|
59
|
-
eventIndex >= 0;
|
|
60
|
-
/* eslint-disable-next-line no-magic-numbers */
|
|
61
|
-
eventIndex -= 1
|
|
62
|
-
) {
|
|
63
|
-
const eventToTrigger = this.oneTimeEvents[event][eventIndex];
|
|
64
|
-
eventToTrigger.call(this, data);
|
|
65
|
-
/* eslint-disable-next-line no-magic-numbers */
|
|
66
|
-
this.oneTimeEvents[event].splice(eventIndex, 1);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
@@ -1,398 +0,0 @@
|
|
|
1
|
-
/* eslint-disable max-lines */
|
|
2
|
-
/* eslint-disable no-console */
|
|
3
|
-
|
|
4
|
-
import { CookieEventHandler } from "./cookie-events.mjs";
|
|
5
|
-
|
|
6
|
-
window.TNAFrontendCookies ||= null;
|
|
7
|
-
|
|
8
|
-
const tnaCookiePolicies = ["usage", "settings", "marketing", "essential"];
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Class to handle cookies.
|
|
12
|
-
* @class Cookies
|
|
13
|
-
* @constructor
|
|
14
|
-
* @public
|
|
15
|
-
*/
|
|
16
|
-
export default class Cookies {
|
|
17
|
-
/** @protected */
|
|
18
|
-
defaultDomain = "";
|
|
19
|
-
/** @protected */
|
|
20
|
-
defaultPath = "";
|
|
21
|
-
/** @protected */
|
|
22
|
-
secure = true;
|
|
23
|
-
/** @protected */
|
|
24
|
-
policiesKey = "";
|
|
25
|
-
/** @protected */
|
|
26
|
-
events = null;
|
|
27
|
-
/** @protected */
|
|
28
|
-
defaultAge = null;
|
|
29
|
-
/** @protected */
|
|
30
|
-
policiesCorrectOnInit = false;
|
|
31
|
-
/** @protected */
|
|
32
|
-
debug = document.documentElement.dataset.tnaFrontendDebug === "true";
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Create a cookie handler.
|
|
36
|
-
* @param {String} [options.defaultDomain] - The domain to register the cookie with.
|
|
37
|
-
* @param {String} [options.path] - The domain to register the cookie with.
|
|
38
|
-
* @param {Boolean} [options.secure] - Only set cookie in HTTPS environments.
|
|
39
|
-
* @param {String} [options.policiesKey] - The name of the cookie.
|
|
40
|
-
* @param {Number} [options.defaultAge] - The default age of non-session cookies.
|
|
41
|
-
* @param {String} [options.newInstance=false] - Create a fresh instance of the cookie class.
|
|
42
|
-
* @param {Boolean} [options.noInit=false] - Don't initialise a blank cookie policy.
|
|
43
|
-
*/
|
|
44
|
-
constructor(options = {}) {
|
|
45
|
-
const {
|
|
46
|
-
defaultDomain,
|
|
47
|
-
defaultPath,
|
|
48
|
-
secure,
|
|
49
|
-
policiesKey,
|
|
50
|
-
defaultAge,
|
|
51
|
-
newInstance = false,
|
|
52
|
-
noInit = false,
|
|
53
|
-
} = options;
|
|
54
|
-
if (!newInstance && window.TNAFrontendCookies) {
|
|
55
|
-
/* eslint-disable-next-line no-constructor-return */
|
|
56
|
-
return window.TNAFrontendCookies;
|
|
57
|
-
}
|
|
58
|
-
if (defaultDomain) {
|
|
59
|
-
this.defaultDomain = defaultDomain;
|
|
60
|
-
} else {
|
|
61
|
-
this.defaultDomain =
|
|
62
|
-
document.documentElement.dataset.tnaCookiesDomain ||
|
|
63
|
-
window.location.hostname;
|
|
64
|
-
}
|
|
65
|
-
if (defaultPath) {
|
|
66
|
-
this.defaultPath = defaultPath;
|
|
67
|
-
} else {
|
|
68
|
-
this.defaultPath = document.documentElement.dataset.tnaCookiesPath || "/";
|
|
69
|
-
}
|
|
70
|
-
if (secure) {
|
|
71
|
-
this.secure = secure;
|
|
72
|
-
} else {
|
|
73
|
-
this.secure =
|
|
74
|
-
document.documentElement.dataset.tnaCookiesInsecure !== "true";
|
|
75
|
-
}
|
|
76
|
-
if (policiesKey) {
|
|
77
|
-
this.policiesKey = policiesKey;
|
|
78
|
-
} else {
|
|
79
|
-
this.policiesKey =
|
|
80
|
-
document.documentElement.dataset.tnaCookiesPoliciesKey ||
|
|
81
|
-
"cookies_policy";
|
|
82
|
-
}
|
|
83
|
-
if (defaultAge) {
|
|
84
|
-
this.defaultAge = defaultAge;
|
|
85
|
-
} else {
|
|
86
|
-
/* eslint-disable-next-line no-magic-numbers */
|
|
87
|
-
const secondsInAYear = 365 * 24 * 60 * 60;
|
|
88
|
-
this.defaultAge =
|
|
89
|
-
parseInt(document.documentElement.dataset.tnaCookiesDefaultAge, 10) ||
|
|
90
|
-
secondsInAYear;
|
|
91
|
-
}
|
|
92
|
-
this.events = new CookieEventHandler(this.debug);
|
|
93
|
-
this.policiesCorrectOnInit =
|
|
94
|
-
Object.keys(this.policies).length === tnaCookiePolicies.length &&
|
|
95
|
-
tnaCookiePolicies.every(
|
|
96
|
-
(policy) =>
|
|
97
|
-
Object.keys(this.policies).includes(policy) &&
|
|
98
|
-
typeof this.policies[policy] === "boolean",
|
|
99
|
-
);
|
|
100
|
-
if (!this.policiesCorrectOnInit && !noInit) {
|
|
101
|
-
this.init();
|
|
102
|
-
}
|
|
103
|
-
this.log({
|
|
104
|
-
defaultDomain: this.defaultDomain,
|
|
105
|
-
defaultPath: this.defaultPath,
|
|
106
|
-
secure: this.secure,
|
|
107
|
-
policiesKey: this.policiesKey,
|
|
108
|
-
defaultAge: this.defaultAge,
|
|
109
|
-
policiesCorrectOnInit: this.policiesCorrectOnInit,
|
|
110
|
-
});
|
|
111
|
-
if (!newInstance && !window.TNAFrontendCookies) {
|
|
112
|
-
window.TNAFrontendCookies = this;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/** @protected */
|
|
117
|
-
init() {
|
|
118
|
-
const existingPolicies = this.policies;
|
|
119
|
-
this.log("Existing policies on init:", existingPolicies);
|
|
120
|
-
const filteredExistingPolicies = Object.fromEntries(
|
|
121
|
-
Object.keys(existingPolicies)
|
|
122
|
-
.filter((policy) => tnaCookiePolicies.includes(policy))
|
|
123
|
-
.map((policy) => [policy, existingPolicies[policy]]),
|
|
124
|
-
);
|
|
125
|
-
if (Object.keys(filteredExistingPolicies).length) {
|
|
126
|
-
this.log("Filtered existing policies:", filteredExistingPolicies);
|
|
127
|
-
}
|
|
128
|
-
this.savePolicies({
|
|
129
|
-
usage: false,
|
|
130
|
-
settings: false,
|
|
131
|
-
marketing: false,
|
|
132
|
-
...filteredExistingPolicies,
|
|
133
|
-
essential: true,
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
log(...args) {
|
|
138
|
-
if (this.debug) {
|
|
139
|
-
console.log("[TNA Frontend Cookies]", ...args, this.all);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
destroyInstance() {
|
|
144
|
-
this.log("Destroying TNAFrontendCookies instance");
|
|
145
|
-
window.TNAFrontendCookies = null;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/** @protected */
|
|
149
|
-
/* eslint-disable-next-line class-methods-use-this */
|
|
150
|
-
get all() {
|
|
151
|
-
const deserialised = {};
|
|
152
|
-
document.cookie
|
|
153
|
-
.split("; ")
|
|
154
|
-
.filter((cookie) => cookie.trim() !== "")
|
|
155
|
-
.forEach((cookie) => {
|
|
156
|
-
const parts = cookie.trim().split("=");
|
|
157
|
-
const [key, value] = parts;
|
|
158
|
-
if (key) {
|
|
159
|
-
deserialised[key] = decodeURIComponent(value || "");
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
return deserialised;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/** @protected */
|
|
166
|
-
get policies() {
|
|
167
|
-
try {
|
|
168
|
-
return JSON.parse(this.get(this.policiesKey) || "{}");
|
|
169
|
-
// eslint-disable-next-line no-unused-vars
|
|
170
|
-
} catch (error) {
|
|
171
|
-
return {};
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Check to see whether a cookie exists or not.
|
|
177
|
-
* @param {String} key - The cookie name.
|
|
178
|
-
* @returns {Boolean}
|
|
179
|
-
*/
|
|
180
|
-
exists(key) {
|
|
181
|
-
return Object.hasOwn(this.all, key);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Check to see whether a cookie has a particular value.
|
|
186
|
-
* @param {String} key - The cookie name.
|
|
187
|
-
* @param {String|Number|Boolean} value - The value to check against.
|
|
188
|
-
* @returns
|
|
189
|
-
*/
|
|
190
|
-
hasValue(key, value) {
|
|
191
|
-
return this.get(key) === value;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Get a cookie.
|
|
196
|
-
* @param {String} key - The cookie name.
|
|
197
|
-
* @returns {String|Number|Boolean|null}
|
|
198
|
-
*/
|
|
199
|
-
get(key) {
|
|
200
|
-
if (this.exists(key)) {
|
|
201
|
-
return decodeURIComponent(this.all[key]);
|
|
202
|
-
}
|
|
203
|
-
return null;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Set a cookie.
|
|
208
|
-
* @param {String} key - The cookie name.
|
|
209
|
-
* @param {String|Number|Boolean} value - The cookie value.
|
|
210
|
-
* @param {Object} options
|
|
211
|
-
* @param {Number} [options.maxAge=this.defaultAge] - The maximum age of the cookie in seconds.
|
|
212
|
-
* @param {String} [options.path=/] - The path to register the cookie for.
|
|
213
|
-
* @param {String} [options.sameSite=Lax] - The sameSite attribute.
|
|
214
|
-
* @param {String} [options.domain=this.defaultDomain] - The domain to register the cookie with.
|
|
215
|
-
* @param {String} [options.path=this.defaultPath] - The path to register the cookie with.
|
|
216
|
-
* @param {String} [options.secure=this.secure] - Only set cookie in HTTPS environments.
|
|
217
|
-
* @param {String} [options.session=false] - Set a session cookie.
|
|
218
|
-
*/
|
|
219
|
-
set(key, value, options = {}) {
|
|
220
|
-
const {
|
|
221
|
-
maxAge = this.defaultAge,
|
|
222
|
-
sameSite = "Lax",
|
|
223
|
-
domain = this.defaultDomain,
|
|
224
|
-
path = this.defaultPath,
|
|
225
|
-
secure = this.secure,
|
|
226
|
-
session = false,
|
|
227
|
-
} = options;
|
|
228
|
-
if (!key) {
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
let secureString = "";
|
|
232
|
-
if (secure) {
|
|
233
|
-
secureString = "; secure";
|
|
234
|
-
}
|
|
235
|
-
let sessionString = "";
|
|
236
|
-
if (!session) {
|
|
237
|
-
sessionString = `; max-age=${maxAge}`;
|
|
238
|
-
}
|
|
239
|
-
const cookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)}; domain=${domain}; samesite=${sameSite}; path=${path}${sessionString}${secureString}`;
|
|
240
|
-
document.cookie = cookie;
|
|
241
|
-
this.log("Set cookie:", {
|
|
242
|
-
key,
|
|
243
|
-
value,
|
|
244
|
-
maxAge,
|
|
245
|
-
path,
|
|
246
|
-
sameSite,
|
|
247
|
-
domain,
|
|
248
|
-
secure,
|
|
249
|
-
session,
|
|
250
|
-
cookie,
|
|
251
|
-
});
|
|
252
|
-
this.events.trigger("setCookie", {
|
|
253
|
-
key,
|
|
254
|
-
value,
|
|
255
|
-
maxAge,
|
|
256
|
-
path,
|
|
257
|
-
sameSite,
|
|
258
|
-
domain,
|
|
259
|
-
secure,
|
|
260
|
-
session,
|
|
261
|
-
cookie,
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Delete a cookie.
|
|
267
|
-
* @param {String} key - The cookie name.
|
|
268
|
-
* @param {String} [path=/] - The path to the cookie is registered on.
|
|
269
|
-
*/
|
|
270
|
-
delete(key, path = "/", domain = this.defaultDomain) {
|
|
271
|
-
const options = { maxAge: -1, path, domain: domain || this.defaultDomain };
|
|
272
|
-
this.set(key, "", options);
|
|
273
|
-
this.log("Deleted cookie:", { key, path, domain, ...options });
|
|
274
|
-
this.events.trigger("deleteCookie", { key, ...options });
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* Delete all cookies.
|
|
279
|
-
*/
|
|
280
|
-
deleteAll(path = "/", domain = this.defaultDomain) {
|
|
281
|
-
Object.keys(this.all).forEach((cookie) => {
|
|
282
|
-
this.delete(cookie, path, domain);
|
|
283
|
-
});
|
|
284
|
-
this.log("Deleted all cookies", { path, domain });
|
|
285
|
-
this.events.trigger("deleteAllCookies", { path, domain });
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* Accept a policy.
|
|
290
|
-
* @param {String} policy - The name of the policy.
|
|
291
|
-
*/
|
|
292
|
-
acceptPolicy(policy) {
|
|
293
|
-
this.log("Accepting policy:", policy);
|
|
294
|
-
this.setPolicy(policy, true);
|
|
295
|
-
this.events.trigger("acceptPolicy", policy);
|
|
296
|
-
this.events.trigger("changePolicy", { [policy]: true });
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* Reject a policy.
|
|
301
|
-
* @param {String} policy - The name of the policy.
|
|
302
|
-
*/
|
|
303
|
-
rejectPolicy(policy) {
|
|
304
|
-
this.log("Rejecting policy:", policy);
|
|
305
|
-
this.setPolicy(policy, false);
|
|
306
|
-
this.events.trigger("rejectPolicy", policy);
|
|
307
|
-
this.events.trigger("changePolicy", { [policy]: false });
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* Set a policy.
|
|
312
|
-
* @param {String} policy - The name of the policy.
|
|
313
|
-
* @param {Boolean} accepted - Whether the policy is accepted or not.
|
|
314
|
-
*/
|
|
315
|
-
setPolicy(policy, accepted) {
|
|
316
|
-
if (policy === "essential") {
|
|
317
|
-
this.log("Cannot change essential policy, it is always accepted.");
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
320
|
-
this.log(`Setting policy ${policy} to ${accepted}`);
|
|
321
|
-
this.savePolicies({
|
|
322
|
-
...this.policies,
|
|
323
|
-
[policy]: accepted,
|
|
324
|
-
essential: true,
|
|
325
|
-
});
|
|
326
|
-
this.events.trigger("changePolicy", { [policy]: accepted });
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Accept all the cookie policies.
|
|
331
|
-
*/
|
|
332
|
-
acceptAllPolicies() {
|
|
333
|
-
this.log("Accepting all policies");
|
|
334
|
-
const allPolicies = Object.fromEntries(
|
|
335
|
-
Object.keys(this.policies).map((key) => [key.toLowerCase(), true]),
|
|
336
|
-
);
|
|
337
|
-
this.savePolicies(allPolicies);
|
|
338
|
-
this.events.trigger("acceptAllPolicies");
|
|
339
|
-
this.events.trigger("changePolicy", allPolicies);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* Reject all the cookie policies.
|
|
344
|
-
*/
|
|
345
|
-
rejectAllPolicies() {
|
|
346
|
-
this.log("Rejecting all policies");
|
|
347
|
-
const allPolicies = {
|
|
348
|
-
...Object.fromEntries(
|
|
349
|
-
Object.keys(this.policies).map((key) => [key.toLowerCase(), false]),
|
|
350
|
-
),
|
|
351
|
-
essential: true,
|
|
352
|
-
};
|
|
353
|
-
this.savePolicies(allPolicies);
|
|
354
|
-
this.events.trigger("rejectAllPolicies");
|
|
355
|
-
this.events.trigger("changePolicy", allPolicies);
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
/**
|
|
359
|
-
* Commit policy preferences to the browser.
|
|
360
|
-
* @param {object} policies - The policies to commit.
|
|
361
|
-
*/
|
|
362
|
-
savePolicies(policies) {
|
|
363
|
-
this.log("Saving policies:", policies);
|
|
364
|
-
this.set(this.policiesKey, JSON.stringify(policies));
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* Get the acceptance status of a policy.
|
|
369
|
-
* @param {String} policy - The name of the policy.
|
|
370
|
-
* @returns {Boolean}
|
|
371
|
-
*/
|
|
372
|
-
isPolicyAccepted(policy) {
|
|
373
|
-
if (Object.hasOwn(this.policies, policy)) {
|
|
374
|
-
return this.policies[policy] === true;
|
|
375
|
-
}
|
|
376
|
-
return null;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
/**
|
|
380
|
-
* Add an event listener.
|
|
381
|
-
* @param {String} event - The event to add a listener for.
|
|
382
|
-
* @param {Function} callback - The callback function to call when the event is triggered.
|
|
383
|
-
*/
|
|
384
|
-
on(event, callback) {
|
|
385
|
-
this.log(`Adding event listener for: ${event}`);
|
|
386
|
-
this.events.on(event, callback);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
/**
|
|
390
|
-
* Add a one-time event listener.
|
|
391
|
-
* @param {String} event - The event to add a listener for.
|
|
392
|
-
* @param {Function} callback - The callback function to call when the event is triggered.
|
|
393
|
-
*/
|
|
394
|
-
once(event, callback) {
|
|
395
|
-
this.log(`Adding one-time event listener for: ${event}`);
|
|
396
|
-
this.events.once(event, callback);
|
|
397
|
-
}
|
|
398
|
-
}
|