UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

250 lines 6.76 kB
import { makeUniqueId, warn, convertJsxToString, slugify } from "../../shared/component-helper.js"; export class GlobalStatusProviderItem { constructor(id, props = null) { 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, { buffer_delay = 0, isEmpty = false } = {}) { 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, opts = {}) { this.remove('internal-close', { preventRerender: true }); const newProps = { ...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?.preventRerender) { this.forceRerender(globalStatus, props, { buffer_delay: 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, opts = {}) { 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 { ...cur, ...newProps }; } return cur; }); } this.restack(status_id); const globalStatus = GlobalStatusProvider.combineMessages(this.stack); if (!opts?.preventRerender) { this.forceRerender(globalStatus, null, { buffer_delay: 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, opts = {}) { if (status_id) { this.stack = this.stack.filter(cur => { return cur.status_id !== status_id; }); const globalStatus = GlobalStatusProvider.combineMessages(this.stack); if (!opts?.preventRerender) { this.forceRerender(globalStatus, null, { buffer_delay: 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(({ status, cb }, i) => { if (typeof cb === 'function') { cb(status); } this._onReadyEvents[i] = null; return false; }); return true; } addOnReady(status, cb) { this._onReadyEvents.push({ status, cb }); } stack = []; globalStatus = {}; _onUpdateEvents = []; _onReadyEvents = []; } class GlobalStatusProvider { static providers = {}; static create = (id = 'main', props = null) => { return GlobalStatusProvider.providers[id] = new GlobalStatusProviderItem(id, props); }; static init(id = 'main', onReady = null, props = 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(id = 'main') { return GlobalStatusProvider.providers[id] || null; } static remove(id = 'main') { if (GlobalStatusProvider.providers[id]) { delete GlobalStatusProvider.providers[id]; } } static prepareItemWithStatusId(item, status_id = 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 { if (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 = { ...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(({ item_id }) => 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; } } if (typeof window !== 'undefined') { window.GlobalStatusProvider = GlobalStatusProvider; } GlobalStatusProvider._supportsSpacingProps = true; export default GlobalStatusProvider; //# sourceMappingURL=GlobalStatusProvider.js.map