@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
114 lines (113 loc) • 3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Store = exports.AccordionStore = void 0;
exports.rememberWarning = rememberWarning;
var _push = _interopRequireDefault(require("core-js-pure/stable/instance/push.js"));
var _hasOwn = _interopRequireDefault(require("core-js-pure/stable/object/has-own.js"));
var _parse = _interopRequireDefault(require("core-js-pure/stable/json/parse.js"));
var _componentHelper = require("../../shared/component-helper.js");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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;
(0, _push.default)(_context = this._instances).call(_context, instance);
}
removeInstance(instance) {
this._instances = this._instances.filter(inst => inst !== instance);
}
}
exports.AccordionStore = AccordionStore;
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 ((0, _hasOwn.default)(window.localStorage, storeId)) {
return (0, _parse.default)(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) {}
}
}
}
exports.Store = Store;
function rememberWarning(type = 'accordion') {
(0, _componentHelper.warn)(`Missing "id" prop the ${type}! "rememberState" is enabled.`);
}
//# sourceMappingURL=AccordionStore.js.map