@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
110 lines • 3.04 kB
JavaScript
import { warn } from '../../shared/component-helper';
export class AccordionStore {
constructor(id) {
this._id = id;
this._instances = [];
}
onChange(_ref) {
let {
id
} = _ref;
this._instances.forEach(inst => {
const closeAccordion = inst.context.expandBehaviour === 'single' && inst.context.expandBehavior === 'single';
if (closeAccordion && inst._id !== id) {
inst.close();
}
});
}
addInstance(instance) {
this._instances.push(instance);
}
removeInstance(instance) {
this._instances = this._instances.filter(inst => inst !== instance);
}
}
export class Store {
constructor(_ref2) {
let {
id,
group
} = _ref2;
this.id = id;
this.group = group;
return this;
}
storeId() {
let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.id;
if (this.group) {
if (this.group[0] === '#') {
return null;
}
id = this.group;
}
return `dnb-accordion-${id}`;
}
saveState(expanded) {
let id = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.id;
let opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
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() {
let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.id;
const storeId = this.storeId(id);
if (storeId) {
try {
if (Object.prototype.hasOwnProperty.call(window.localStorage, storeId)) {
return JSON.parse(window.localStorage.getItem(storeId));
}
} catch (e) {}
}
return null;
}
getState() {
let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 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() {
let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.id;
if (id) {
try {
const storeId = this.storeId(id);
if (storeId) {
window.localStorage.setItem(storeId, null);
}
} catch (e) {}
}
}
}
export function rememberWarning() {
let type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'accordion';
warn(`Missing "id" prop the ${type}! "remember_state" is enabled.`);
}
//# sourceMappingURL=AccordionStore.js.map