@legumeinfo/web-components
Version:
Web Components for the Legume Information System and other AgBio databases
124 lines • 4.52 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { LitElement, html, css } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
/**
* @htmlElement `<lis-inline-loading-element>`
*
* A Web Component that provides a consistent inline loading element. When in the
* loading state, the element shows a spinner. When a search succeeds, the spinner
* is hidden. When no results are returned or an error occurs, an alert icon is
* shown. Optionally, a tooltip may be shown with every icon with a context-specific
* message.
*
* @example
* By default, the loading element is not visible. It should be interacted with via
* JavaScript:
* ```html
* <!-- the loading element -->
* <lis-inline-loading-element id="loading"></lis-inline-alert-element>
*
* <!-- interact with the element JavaScript -->
* <script type="text/javascript">
* // get the loading element
* const loadingElement = document.getElementById('loading');
* // activate the spinner overlay
* loadingElement.loading();
* // hide the spinner overlay / reset the element
* loadingElement.success();
* // show an alert that no results were returned
* loadingElement.noResults();
* // show an alert that reports an error
* loadingElement.failure();
* </script>
* ```
*
* @example
* By default, the loading element uses "data" in its tooltip messages, e.g. "No data
* loaded". This can be override using the {@link dataType | `dataType`}
* attribute/property:
* ```html
* <!-- set the dataType attributes via HTML -->
* <lis-inline-loading-element id="loading" dataType="special sauce"></lis-inline-loading-element>
*
* <!-- set the dataType property via JavaScript
* <script type="text/javascript">
* // get the loading element
* const loadingElement = document.getElementById('loading');
* // set the element's dataTypeproperty
* loadingElement.dataType = 'secret sauce';
* </script>
* ```
*/
let LisInlineLoadingElement = class LisInlineLoadingElement extends LitElement {
constructor() {
super(...arguments);
/**
* The type of data being loaded.
*
* @attribute
*/
this.dataType = 'data';
// The current state of the element, i.e. what UI elements should be visible.
this.state = 'loaded';
this.content = 'loaded';
}
/** @ignore */
// disable Shadow DOM to inherit global styles
createRenderRoot() {
return this;
}
loading() {
this.content = '';
this.state = 'loading';
}
success() {
this.content = '';
this.state = 'loaded';
}
noResults() {
this.content = `No ${this.dataType} found`;
this.state = 'info';
}
failure() {
this.content = `Failed to load ${this.dataType}`;
this.state = 'error';
}
error(content) {
this.content = content;
this.state = 'info';
}
/** @ignore */
// used by Lit to draw the template
render() {
if (this.state == 'loaded')
return html ``;
if (this.state == 'error' || this.state == 'info') {
const icon = this.state == 'error' ? 'warning' : 'info';
return html `<span uk-icon="${icon}" uk-tooltip="${this.content}"></span>`;
}
return html `<span uk-spinner></span>`;
}
};
/** @ignore */
// used by Lit to style the Shadow DOM
// not necessary but exclusion breaks TypeDoc
LisInlineLoadingElement.styles = css ``;
__decorate([
property({ type: String })
], LisInlineLoadingElement.prototype, "dataType", void 0);
__decorate([
state()
], LisInlineLoadingElement.prototype, "state", void 0);
__decorate([
state()
], LisInlineLoadingElement.prototype, "content", void 0);
LisInlineLoadingElement = __decorate([
customElement('lis-inline-loading-element')
], LisInlineLoadingElement);
export { LisInlineLoadingElement };
//# sourceMappingURL=lis-inline-loading-element.js.map