@legumeinfo/web-components
Version:
Web Components for the Legume Information System and other AgBio databases
141 lines • 5.12 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';
import { createRef, ref } from 'lit/directives/ref.js';
/**
* @htmlElement `<lis-loading-element>`
*
* A Web Component that provides a consistent loading element. When in the loading state,
* the element shows a spinner that covers all content inside of its parent element.
* When a search succeeds, the spinner is hidden. When no results are returned or an
* error occurs, an alert element is shown that reports the state of the search.
*
* @example
* By default, the loading element is not visible. It should be interacted with via
* JavaScript:
* ```html
* <!-- the loading element -->
* <lis-loading-element id="loading"></lis-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 alert 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-loading-element id="loading" dataType="special sauce"></lis-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>
* ```
*
* @example
* Depending on the type of parent element, the spinner overlay may cover an ancestor
* of the parent instead. Use the `uk-inline` class on the parent to force the overlay
* to cover the loading element's parent:
* ```html
* <!-- force the loading overlay to cover its parent element -->
* <div class="uk-inline">
* <lis-loading-element></lis-loading-element>
* </div>
* ```
*/
let LisLoadingElement = class LisLoadingElement 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';
// bind to the alert element in the template
this._alertRef = createRef();
}
/** @ignore */
// disable Shadow DOM to inherit global styles
createRenderRoot() {
return this;
}
loading() {
this.state = 'loading';
}
success() {
this.state = 'loaded';
}
noResults() {
var _a;
const content = `No ${this.dataType} found`;
(_a = this._alertRef.value) === null || _a === void 0 ? void 0 : _a.warning(content);
this.state = 'message';
}
failure() {
var _a;
const content = `Failed to load ${this.dataType}`;
(_a = this._alertRef.value) === null || _a === void 0 ? void 0 : _a.danger(content);
this.state = 'message';
}
error(content) {
var _a;
(_a = this._alertRef.value) === null || _a === void 0 ? void 0 : _a.warning(content);
this.state = 'message';
}
/** @ignore */
// used by Lit to draw the template
render() {
return html `
<div
class="uk-overlay-default uk-position-cover uk-flex uk-flex-center uk-flex-middle"
?hidden=${this.state != 'loading'}
>
<span uk-spinner></span>
</div>
<lis-alert-element
${ref(this._alertRef)}
?hidden=${this.state != 'message'}
></lis-alert-element>
`;
}
};
/** @ignore */
// used by Lit to style the Shadow DOM
// not necessary but exclusion breaks TypeDoc
LisLoadingElement.styles = css ``;
__decorate([
property({ type: String })
], LisLoadingElement.prototype, "dataType", void 0);
__decorate([
state()
], LisLoadingElement.prototype, "state", void 0);
LisLoadingElement = __decorate([
customElement('lis-loading-element')
], LisLoadingElement);
export { LisLoadingElement };
//# sourceMappingURL=lis-loading-element.js.map