@legumeinfo/web-components
Version:
Web Components for the Legume Information System and other AgBio databases
47 lines • 1.64 kB
JavaScript
/**
* A controller that allows components to subsribe to the
* {@link !DOMContentLoaded | `DOMContentLoaded`} event in a manner that
* triggers changes in the component's template when the event occurs.
*/
export class LisDomContentLoadedController {
/**
* @param host - The controller that's using the controller.
*/
constructor(host) {
/** @ignore */
this._listeners = [];
(this.host = host).addController(this);
}
/** @ignore */
hostConnected() {
document.addEventListener('DOMContentLoaded', this._contentLoaded.bind(this));
}
/** @ignore */
hostDisconnected() {
document.removeEventListener('DOMContentLoaded', this._contentLoaded.bind(this));
}
/**
* Adds a listener to the {@link !DOMContentLoaded | `DOMContentLoaded`} event.
*
* @param listener - The listener to subscribe to the
* {@link !DOMContentLoaded | `DOMContentLoaded`} event.
*/
addListener(listener) {
// each listener is called in the scope of the host
this._listeners.push(listener.bind(this.host));
}
/** @ignore */
// calls all listeners of the DOMContentLoaded event
_contentLoaded(event) {
// redraw the host
this.host.requestUpdate();
// wait for the redraw to complete in case any listeners rely on state from the template
this.host.updateComplete.then(() => {
// call each listener
this._listeners.forEach((listener) => {
listener(event);
});
});
}
}
//# sourceMappingURL=lis-dom-content-loaded-controller.js.map