UNPKG

@nent/core

Version:

Functional elements to add routing, data-binding, dynamic HTML, declarative actions, audio, video, and so much more. Supercharge static HTML files into web apps without script or builds.

210 lines (205 loc) 7.4 kB
/*! * NENT 2022 */ import { r as registerInstance, d as createEvent, h, H as Host, a as getElement } from './index-916ca544.js'; import { a as actionBus, e as eventBus } from './index-f7016b94.js'; import { a as addDataProvider, r as removeDataProvider } from './factory-acbf0d3d.js'; import { D as DATA_COMMANDS } from './interfaces-4b724211.js'; import { D as DATA_EVENTS } from './interfaces-8c5cd1b8.js'; import './index-4bfabbbd.js'; import './values-ddfac998.js'; import './promises-584c4ece.js'; import './logging-5a93c8af.js'; /* istanbul ignore file */ /** * If the value is true, return the key, otherwise return the key and value * @param {string} key - The name of the attribute. * @param {string | boolean | undefined} value - The value of the attribute. * @returns function stringifyAttribute( * key: string, * value: string | boolean | undefined, * ): string { * if (!value) { * return '' * } */ function stringifyAttribute(key, value) { if (!value) { return ''; } const stringified = `; ${key}`; if (value === true) { return stringified; // Boolean attributes shouldn't have a value } return `${stringified}=${value}`; } /** * It takes a `CookieAttributes` object and returns a string of attributes that can be used in a cookie * @param {CookieAttributes} attributes - CookieAttributes * @returns A string of the cookie attributes. */ function stringifyAttributes(attributes) { if (typeof attributes.expires === 'number') { const expires = new Date(); expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e5); attributes.expires = expires; } return (stringifyAttribute('Expires', attributes.expires ? attributes.expires.toUTCString() : '') + stringifyAttribute('Domain', attributes.domain) + stringifyAttribute('Path', attributes.path) + stringifyAttribute('Secure', attributes.secure) + stringifyAttribute('SameSite', attributes.sameSite)); } function readValue(value) { return value.replace(/%3B/g, ';'); } function writeValue(value) { return value.replace(/;/g, '%3B'); } /** * It takes a key, value, and attributes, and returns a string * @param {string} key - The name of the cookie. * @param {string} value - The value of the cookie. * @param {CookieAttributes} attributes - CookieAttributes * @returns A string that is the key and value of the cookie, and the attributes of the cookie. */ function encode(key, value, attributes) { return `${writeValue(key).replace(/=/g, '%3D')}=${writeValue(value)}${stringifyAttributes(attributes)}`; } /** * It splits the cookie string into an array of cookies, then splits each cookie into an array of name * and value, then decodes the name and value, and finally returns an object with the decoded name and * value * @param {string} cookieString - The string of cookies to parse. * @returns A function that takes a string and returns an object. */ function parse(cookieString) { const result = {}; const cookies = cookieString ? cookieString.split('; ') : []; for (const cookie of cookies) { const parts = cookie.split('='); const value = parts.slice(1).join('='); const name = readValue(parts[0]).replace(/%3D/g, '='); result[name] = readValue(value); } return result; } function getAll(document) { return parse(document.cookie); } /** * "Given a document and a key, return the value of the cookie with that key." * * The first line of the function is a comment. Comments are ignored by the compiler * @param {HTMLDocument} document - The document object. * @param {string} key - The key of the cookie you want to get. * @returns The value of the cookie with the given key. */ function getCookie(document, key) { return getAll(document)[key]; } /** * "Set a cookie on the document with the given key and value, and the given attributes." * * The first three parameters are required. The last parameter is optional * @param {HTMLDocument} document - HTMLDocument - The document object. * @param {string} key - The name of the cookie. * @param {string} value - The value of the cookie. * @param {CookieAttributes} [attributes] - CookieAttributes */ function setCookie(document, key, value, attributes) { document.cookie = encode(key, value, Object.assign({ path: '/' }, attributes)); } /* It's a data provider that uses cookies to store data */ class CookieService { /** * A constructor function. * @param {Document} document - The document object. * @param {IEventEmitter} eventBus - IEventEmitter - This is the event bus that the component will * use to communicate with other components. * @param {string} name - The name of the component. */ constructor(document, eventBus, name) { this.document = document; this.eventBus = eventBus; this.name = name; } async get(key) { return getCookie(this.document, key) || null; } async set(key, value) { const existing = await this.get(key); if (existing == value) return; setCookie(this.document, key, value, { sameSite: 'strict' }); this.eventBus.emit(DATA_EVENTS.DataChanged, { provider: this.name, }); } } const DataCookie = class { constructor(hostRef) { registerInstance(this, hostRef); this.didConsent = createEvent(this, "didConsent", 6); this.consentKey = 'consent'; this.hide = false; /** * When skipConsent is true, the accept-cookies banner will not * be displayed before accessing cookie-data. */ this.skipConsent = false; /** * Provider name to use in nent expressions. */ this.name = 'cookie'; } /** * Immediately register the provider. */ async registerProvider() { addDataProvider(this.name, this.provider); this.actionSubscription = actionBus.on(this.name, async (action) => { if (action.command == DATA_COMMANDS.SetData) { const { data } = action; await Promise.all(Object.keys(action.data).map(key => this.provider.set(key, data[key]))); } }); await this.provider.set(this.consentKey, true.toString()); this.didConsent.emit({ consented: true }); this.hide = true; } async componentWillLoad() { this.provider = new CookieService(this.el.ownerDocument, eventBus, this.name); if (this.skipConsent) { this.registerProvider(); this.hide = true; return; } const consented = await this.provider.get(this.consentKey); if (consented != null) { this.hide = true; if (consented == 'true') this.registerProvider(); } } async handleConsentResponse(ev, consented) { ev.preventDefault(); if (consented) { await this.registerProvider(); } else { this.hide = true; } } disconnectedCallback() { var _a; removeDataProvider(this.name); (_a = this.actionSubscription) === null || _a === void 0 ? void 0 : _a.call(this); } render() { return (h(Host, { hidden: this.hide }, h("slot", null), h("a", { id: "accept", onClick: async (ev) => await this.handleConsentResponse(ev, true) }, h("slot", { name: "accept" }, "Accept")), h("a", { id: "reject", onClick: async (ev) => await this.handleConsentResponse(ev, false) }, h("slot", { name: "reject" }, "Reject")))); } get el() { return getElement(this); } }; DataCookie.style = ":host {display:block;}"; export { DataCookie as n_data_cookie };