@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
261 lines • 7.9 kB
JavaScript
import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
var _GlobalStatusProvider;
import _pushInstanceProperty from "core-js-pure/stable/instance/push.js";
import _JSON$parse from "core-js-pure/stable/json/parse.js";
import { makeUniqueId, warn, convertJsxToString, slugify } from "../../shared/component-helper.js";
export class GlobalStatusProviderItem {
constructor(id, props = 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) {
var _context;
_pushInstanceProperty(_context = this._onUpdateEvents).call(_context, 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 {
var _context2;
_pushInstanceProperty(_context2 = this.stack).call(_context2, 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, 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 !== 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) {
var _context3;
this.stack = this.stack.filter(cur => {
return cur.status_id !== status_id;
});
_pushInstanceProperty(_context3 = this.stack).call(_context3, 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 !== 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(({
status,
cb
}, i) => {
if (typeof cb === 'function') {
cb(status);
}
this._onReadyEvents[i] = null;
return false;
});
return true;
}
addOnReady(status, cb) {
var _context4;
_pushInstanceProperty(_context4 = this._onReadyEvents).call(_context4, {
status,
cb
});
}
}
class GlobalStatusProvider {
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 {
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 = {
...cur
};
if (typeof cur.items === 'string' && cur.items[0] === '[') {
cur.items = _JSON$parse(cur.items);
}
if (cur.item) {
var _context5;
if (typeof cur.item === 'string' && cur.item[0] === '{') {
cur.item = _JSON$parse(cur.item);
}
cur.items = cur.items || [];
_pushInstanceProperty(_context5 = cur.items).call(_context5, 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 {
_pushInstanceProperty(_acc).call(_acc, item);
}
return _acc;
}, acc.items || []);
}
Object.assign(acc, cur);
return acc;
}, {});
return globalStatus;
}
}
_GlobalStatusProvider = GlobalStatusProvider;
_defineProperty(GlobalStatusProvider, "providers", {});
_defineProperty(GlobalStatusProvider, "create", (id = 'main', props = 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