@roots/bud-client
Version:
Client scripts for @roots/bud
48 lines (47 loc) • 1.37 kB
JavaScript
/**
* Activity indicator controller
*/
export class Controller {
/**
* Initialization
*/
constructor() {
/**
* Active WHM payload
*/
this.payload = null;
this.node = document.createElement(`bud-activity-indicator`);
this.update = this.update.bind(this);
}
/**
* Append `bud-error` element to the DOM
*/
addNode() {
if (document.body.querySelector(`bud-activity-indicator`)) {
if (typeof this.timer.unref === `function`)
this.timer.unref();
this.removeNode();
}
document.body?.appendChild(this.node);
this.timer = setTimeout(this.removeNode, 3000);
}
/**
* Remove `bud-error` element from the DOM (if present)
*/
removeNode() {
document.body.querySelector(`bud-activity-indicator`)?.remove();
}
/**
* Update activity indicator
*/
update(payload) {
if (!payload.action)
return;
this.node.setAttribute(`action`, payload.action);
this.node.toggleAttribute(`has-errors`, payload?.errors?.length && payload.errors.length > 0 ? true : false);
this.node.toggleAttribute(`has-warnings`, payload?.warnings?.length && payload.warnings?.length > 0
? true
: false);
this.addNode();
}
}