UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

104 lines 2.61 kB
import _pushInstanceProperty from "core-js-pure/stable/instance/push.js"; import _Object$hasOwn from "core-js-pure/stable/object/has-own.js"; import _JSON$parse from "core-js-pure/stable/json/parse.js"; import { warn } from "../../shared/component-helper.js"; export class AccordionStore { constructor(id) { this._id = id; this._instances = []; } onChange({ id }) { this._instances.forEach(inst => { if (inst.context.expandBehavior === 'single' && inst._id !== id) { inst.close(); } }); } addInstance(instance) { var _context; _pushInstanceProperty(_context = this._instances).call(_context, instance); } removeInstance(instance) { this._instances = this._instances.filter(inst => inst !== instance); } } export class Store { constructor({ id, group }) { this.id = id; this.group = group; return this; } storeId(id = this.id) { if (this.group) { if (this.group[0] === '#') { return null; } id = this.group; } return `dnb-accordion-${id}`; } saveState(expanded, id = this.id, opts = {}) { if (id) { try { const store = this.getData() || {}; if (this.group) { if (expanded) { store.id = id; } else if (opts && opts.force) { store.id = null; } } else { store.expanded = expanded; } const storeId = this.storeId(id); if (storeId) { window.localStorage.setItem(storeId, JSON.stringify(store)); } } catch (e) {} } else { rememberWarning(); } } getData(id = this.id) { const storeId = this.storeId(id); if (storeId) { try { if (_Object$hasOwn(window.localStorage, storeId)) { return _JSON$parse(window.localStorage.getItem(storeId)); } } catch (e) {} } return null; } getState(id = this.id) { let state = null; const store = this.getData(id); if (store) { if (typeof store.id !== 'undefined') { state = id === store.id; } else if (typeof store.expanded !== 'undefined') { state = store.expanded; } } return state; } flush(id = this.id) { if (id) { try { const storeId = this.storeId(id); if (storeId) { window.localStorage.setItem(storeId, null); } } catch (e) {} } } } export function rememberWarning(type = 'accordion') { warn(`Missing "id" prop the ${type}! "remember_state" is enabled.`); } //# sourceMappingURL=AccordionStore.js.map