UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

266 lines 9.18 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; var _GlobalStatusProvider; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { makeUniqueId, warn, convertJsxToString, slugify } from '../../shared/component-helper'; export class GlobalStatusProviderItem { constructor(id) { let props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; _defineProperty(this, "stack", []); _defineProperty(this, "globalStatus", {}); _defineProperty(this, "_onUpdateEvents", []); _defineProperty(this, "_onReadyEvents", []); this.internal_id = id; if (props) { this.add(props); } } onUpdate(event) { if (this._onUpdateEvents.filter(cb => cb === event).length === 0) { this._onUpdateEvents.push(event); } } forceRerender(globalStatus, props) { let { buffer_delay = 0, isEmpty = false } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; const run = () => { this._onUpdateEvents.forEach(event => { if (typeof event === 'function') { event(globalStatus, props, { isEmpty }); } }); }; if (buffer_delay > 0) { clearTimeout(this._bufferDelayId); this._bufferDelayId = setTimeout(run, buffer_delay); } else { run(); } } init(props) { return this.add(props, { preventRerender: true }); } add(props) { let opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; this.remove('internal-close', { preventRerender: true }); const newProps = _objectSpread({}, props); if (!newProps.status_id) { newProps.status_id = makeUniqueId(); } if (typeof newProps.show === 'undefined') { newProps.show = true; } if (newProps.children) { newProps.text = newProps.children; delete newProps.children; } const stackIndex = this.stack.findIndex(cur => cur.status_id === newProps.status_id); if (stackIndex > -1) { this.stack[stackIndex] = newProps; } else { this.stack.push(newProps); } const globalStatus = GlobalStatusProvider.combineMessages(this.stack); if (!(opts !== null && opts !== void 0 && opts.preventRerender)) { this.forceRerender(globalStatus, props, { buffer_delay: (props === null || props === void 0 ? void 0 : props.buffer_delay) > -1 ? props.buffer_delay : 0 }); } return globalStatus; } get(status_id) { return this.stack.find(cur => cur.status_id === status_id); } update(status_id, newProps) { let opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; const item = this.get(status_id); if (!item) { this.add(newProps, { preventRerender: true }); } else { this.stack = this.stack.map((cur, i, arr) => { if (status_id ? cur.status_id === status_id : i === arr.length - 1) { return _objectSpread(_objectSpread({}, cur), newProps); } return cur; }); } this.restack(status_id); const globalStatus = GlobalStatusProvider.combineMessages(this.stack); if (!(opts !== null && opts !== void 0 && opts.preventRerender)) { this.forceRerender(globalStatus, null, { buffer_delay: (newProps === null || newProps === void 0 ? void 0 : newProps.buffer_delay) > -1 ? newProps.buffer_delay : 1 }); } } restack(status_id) { const item = this.get(status_id); if (item) { this.stack = this.stack.filter(cur => { return cur.status_id !== status_id; }); this.stack.push(item); } } remove(status_id) { let opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (status_id) { this.stack = this.stack.filter(cur => { return cur.status_id !== status_id; }); const globalStatus = GlobalStatusProvider.combineMessages(this.stack); if (!(opts !== null && opts !== void 0 && opts.preventRerender)) { this.forceRerender(globalStatus, null, { buffer_delay: (opts === null || opts === void 0 ? void 0 : opts.buffer_delay) > -1 ? opts.buffer_delay : 1 }); } } } empty() { this._onUpdateEvents.forEach((cb, i) => { this._onUpdateEvents[i] = null; }); this._onUpdateEvents = []; this._onReadyEvents.forEach((cb, i) => { this._onReadyEvents[i] = null; }); this._onReadyEvents = []; } unbind() { this.empty(); GlobalStatusProvider.remove(this.internal_id); } isReady() { this._onReadyEvents = this._onReadyEvents.filter((_ref, i) => { let { status, cb } = _ref; if (typeof cb === 'function') { cb(status); } this._onReadyEvents[i] = null; return false; }); return true; } addOnReady(status, cb) { this._onReadyEvents.push({ status, cb }); } } class GlobalStatusProvider { static init() { let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'main'; let onReady = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; let props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; const existingStatus = GlobalStatusProvider.get(id); if (existingStatus) { if (props) { existingStatus.add(props); } if (typeof onReady === 'function') { onReady(existingStatus); } return existingStatus; } const newStatus = GlobalStatusProvider.create(id, props); if (onReady) { newStatus.addOnReady(newStatus, onReady); } if (id !== 'main') { warn(`No <GlobalStatus ${`id="${id}"`} /> found.`); } return newStatus; } static get() { let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'main'; return GlobalStatusProvider.providers[id] || null; } static remove() { let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'main'; if (GlobalStatusProvider.providers[id]) { delete GlobalStatusProvider.providers[id]; } } static prepareItemWithStatusId(item) { let status_id = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; if (typeof item === 'string') { item = { text: item }; } if (!item.item_id) { if (status_id && status_id !== 'status-main') { item.item_id = status_id; } else { var _item; if ((_item = item) !== null && _item !== void 0 && _item.text) { item.item_id = slugify(convertJsxToString(item.text)); } else { item.item_id = slugify(item); } } } return item; } static combineMessages(stack) { const globalStatus = stack.reduce((acc, cur) => { cur = _objectSpread({}, cur); if (typeof cur.items === 'string' && cur.items[0] === '[') { cur.items = JSON.parse(cur.items); } if (cur.item) { if (typeof cur.item === 'string' && cur.item[0] === '{') { cur.item = JSON.parse(cur.item); } cur.items = cur.items || []; cur.items.push(cur.item); } if (cur.items) { cur.items = cur.items.reduce((_acc, item) => { item = GlobalStatusProvider.prepareItemWithStatusId(item); const foundAtIndex = _acc.findIndex(_ref2 => { let { item_id } = _ref2; return item_id === item.item_id; }); if (foundAtIndex > -1) { _acc[foundAtIndex] = item; } else { _acc.push(item); } return _acc; }, acc.items || []); } Object.assign(acc, cur); return acc; }, {}); return globalStatus; } } _GlobalStatusProvider = GlobalStatusProvider; _defineProperty(GlobalStatusProvider, "providers", {}); _defineProperty(GlobalStatusProvider, "create", function () { let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'main'; let props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; return _GlobalStatusProvider.providers[id] = new GlobalStatusProviderItem(id, props); }); if (typeof window !== 'undefined') { window.GlobalStatusProvider = GlobalStatusProvider; } GlobalStatusProvider._supportsSpacingProps = true; export default GlobalStatusProvider; //# sourceMappingURL=GlobalStatusProvider.js.map