Some checks are pending
Module sub-split / Sub-split (push) Waiting to run
- Base: wintercms/winter branch 1.2 (full framework) - Theme vivespos: Canvas 7 + Bootstrap 5 CDN, custom CSS - Layout: deferred GTM/GA4 tracking, JSON-LD SoftwareApplication - Partials: hero (offline-first), features, modes (offline/nube toggle), screenshots, pricing (3 planes), comparison, FAQ, CTA - Plugin VivesPOS.Site with ContactForm - Dockerfile: PHP 8.2 Apache, port 80, healthcheck - Added winter/wn-pages, blog, sitemap, seo plugins - Active theme set to vivespos
135 lines
3.2 KiB
JavaScript
135 lines
3.2 KiB
JavaScript
import BaseCookie from 'js-cookie';
|
|
import Singleton from '../abstracts/Singleton';
|
|
|
|
/**
|
|
* Cookie utility.
|
|
*
|
|
* This utility is a thin wrapper around the "js-cookie" library.
|
|
*
|
|
* @see https://github.com/js-cookie/js-cookie
|
|
* @copyright 2021 Winter.
|
|
* @author Ben Thomson <git@alfreido.com>
|
|
*/
|
|
export default class Cookie extends Singleton {
|
|
construct() {
|
|
this.defaults = {
|
|
expires: null,
|
|
path: '/',
|
|
domain: null,
|
|
secure: false,
|
|
sameSite: 'Lax',
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Set the default cookie parameters for all subsequent "set" and "remove" calls.
|
|
*
|
|
* @param {Object} options
|
|
*/
|
|
setDefaults(options) {
|
|
if (typeof options !== 'object') {
|
|
throw new Error('Cookie defaults must be provided as an object');
|
|
}
|
|
|
|
Object.entries(options).forEach((entry) => {
|
|
const [key, value] = entry;
|
|
|
|
if (this.defaults[key] !== undefined) {
|
|
this.defaults[key] = value;
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the current default cookie parameters.
|
|
*
|
|
* @returns {Object}
|
|
*/
|
|
getDefaults() {
|
|
const defaults = {};
|
|
|
|
Object.entries(this.defaults).forEach((entry) => {
|
|
const [key, value] = entry;
|
|
|
|
if (this.defaults[key] !== null) {
|
|
defaults[key] = value;
|
|
}
|
|
});
|
|
|
|
return defaults;
|
|
}
|
|
|
|
/**
|
|
* Get a cookie by name.
|
|
*
|
|
* If `name` is undefined, returns all cookies as an Object.
|
|
*
|
|
* @param {String} name
|
|
* @returns {Object|String}
|
|
*/
|
|
get(name) {
|
|
if (name === undefined) {
|
|
const cookies = BaseCookie.get();
|
|
|
|
Object.entries(cookies).forEach((entry) => {
|
|
const [cookieName, cookieValue] = entry;
|
|
|
|
this.snowboard.globalEvent('cookie.get', cookieName, cookieValue, (newValue) => {
|
|
cookies[cookieName] = newValue;
|
|
});
|
|
});
|
|
|
|
return cookies;
|
|
}
|
|
|
|
let value = BaseCookie.get(name);
|
|
|
|
// Allow plugins to override the gotten value
|
|
this.snowboard.globalEvent('cookie.get', name, value, (newValue) => {
|
|
value = newValue;
|
|
});
|
|
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* Set a cookie by name.
|
|
*
|
|
* You can specify additional cookie parameters through the "options" parameter.
|
|
*
|
|
* @param {String} name
|
|
* @param {String} value
|
|
* @param {Object} options
|
|
* @returns {String}
|
|
*/
|
|
set(name, value, options) {
|
|
let saveValue = value;
|
|
|
|
// Allow plugins to override the value to save
|
|
this.snowboard.globalEvent('cookie.set', name, value, (newValue) => {
|
|
saveValue = newValue;
|
|
});
|
|
|
|
return BaseCookie.set(name, saveValue, {
|
|
...this.getDefaults(),
|
|
...options,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Remove a cookie by name.
|
|
*
|
|
* You can specify the additional cookie parameters via the "options" parameter.
|
|
*
|
|
* @param {String} name
|
|
* @param {Object} options
|
|
* @returns {void}
|
|
*/
|
|
remove(name, options) {
|
|
BaseCookie.remove(name, {
|
|
...this.getDefaults(),
|
|
...options,
|
|
});
|
|
}
|
|
}
|