100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
UI updates: or-input overhaul no longer just a simple input overlay can now take an asset and attribute and figure out what control to show
Rich Turnerd28ff0dce268
diff --git a/ui/component/or-input/package.json b/ui/component/or-input/package.jsonindex 3fb9aae74..73bd9d986 100644--- a/ui/component/or-input/package.json+++ b/ui/component/or-input/package.json@@ -12,10 +12,8 @@"license": "AGPL-3.0-or-later","dependencies": {"lit-element": "^2.1.0",- "@material/select": "^1.0.1",- "@material/list": "^1.0.1",- "@material/menu": "^1.0.1",- "@material/menu-surface": "^1.0.1"+ "@openremote/core": "^1.0.0",+ "@openremote/or-select": "^1.0.0"},"devDependencies": {"@polymer/decorators": "^3.0.0",diff --git a/ui/component/or-input/src/index.ts b/ui/component/or-input/src/index.tsindex 4edc7fcc7..7580b9d99 100644--- a/ui/component/or-input/src/index.ts+++ b/ui/component/or-input/src/index.ts@@ -1,24 +1,123 @@-import {html, LitElement, property, PropertyValues} from "lit-element";-import openremote from "@openremote/core";-+import {customElement, html, LitElement, property, query, TemplateResult} from "lit-element";import {orInputStyle} from "./style";-class OrInput extends LitElement {+import {+ AttributeDescriptor,+ AttributeValueDescriptor,+ AttributeValueType,+ MetaItemType,+ ValueType+} from "@openremote/model";+import {AssetModelUtil} from "@openremote/core";+import "@openremote/or-select";+import {OrSelectChangedEvent} from "@openremote/or-select";++// Allows consumers of the or-input component to provide custom input controls; if the value is changed the custom control+// must call the valueChangeNotifier.+class InputProviders {+ public _providers: Array<(assetType: string, attributeName: string, attributeDescriptor: AttributeDescriptor | undefined, value: any | undefined, valueChangeNotifier: (value: any | undefined) => void, readonly: boolean) => TemplateResult | undefined> = [];++ public addInputProvider(callback: (assetType: string, attributeName: string, attributeDescriptor: AttributeDescriptor | undefined, value: any | undefined, valueChangeNotifier: (value: any | undefined) => void, readonly: boolean) => TemplateResult | undefined) {+ this._providers.push(callback);+ }++ public removeInputProvider(callback: (assetType: string, attributeName: string, attributeDescriptor: AttributeDescriptor | undefined, value: any | undefined, valueChangeNotifier: (value: any | undefined) => void, readonly: boolean) => TemplateResult | undefined) {+ const i = this._providers.indexOf(callback);+ if (i <= 0) {+ this._providers.splice(i, 1);+ }+ }+}++const inputProviders = new InputProviders();+export default inputProviders;++export class OrInputChangedEvent extends CustomEvent<OrInputChangedEventDetail> {++ public static readonly NAME = "or-input-changed";++ constructor(value?: any, previousValue?: any) {+ super(OrInputChangedEvent.NAME, {+ detail: {+ value: value,+ previousValue: previousValue+ },+ bubbles: true,+ composed: true+ });+ }+}++export interface OrInputChangedEventDetail {+ value?: any;+ previousValue?: any;+}++declare global {+ export interface HTMLElementEventMap {+ [OrInputChangedEvent.NAME]: OrInputChangedEvent;+ }+}++export enum InputType {+ BUTTON = "button",+ CHECKBOX = "checkbox",+ COLOUR = "color",+ DATE = "date",+ DATETIME = "datetime",+ DECIMAL = "decimal",+ EMAIL = "email",+ GEO_JSON_POINT = "geoPos",+ INTEGER = "integer",+ JSON = "json",+ NUMBER = "number",+ PASSWORD = "password",+ RADIO = "radio",+ SWITCH_MOMENTARY = "switchMomentary",+ SWITCH_TOGGLE = "checkbox",+ SELECT = "select",+ SLIDER = "range",+ TELEPHONE = "telephone",+ TEXT = "text",+ TEXTAREA = "textarea",+ TIME = "time",+ URL = "url"+}++@customElement("or-input")+export class OrInput extends LitElement {++ @property({type: String})+ public assetType?: string;@property({type: String})- public type: string = "text";+ public attributeName?: string;++ @property({type: Object})+ public value?: any;@property({type: String})- public name: string = "";+ public type?: InputType;@property({type: Boolean})- public required: boolean = false;+ public allowNull: boolean = true;++ @property({type: Boolean})+ public readonly: boolean = false;@property({type: Boolean})- public disabled: boolean = false;+ public required: boolean = false;++ @property({type: Object})+ public attributeDescriptor?: AttributeDescriptor;++ @property({type: Object})+ public attributeValueDescriptor?: AttributeValueDescriptor;@property({type: String})- public value: string = "";+ public valueType?: ValueType;+ @query("#or-input")+ protected _input!: HTMLInputElement;static get styles() {return [@@ -28,35 +127,175 @@ class OrInput extends LitElement {protected render() {- return html`- <input class="or-input" ?required="${this.required}" type="${this.type}" name="${this.name}" @change="${this.onChange}" value="${this.value}" ?disabled="${this.disabled}" />- `;+ if (!this.assetType || !this.attributeName) {+ return;+ }++ if (!this.attributeDescriptor) {+ this.attributeDescriptor = AssetModelUtil.getAttributeDescriptorFromAsset(this.assetType, this.attributeName);+ }++ for (const inputProvider of inputProviders._providers) {+ const template = inputProvider(this.assetType, this.attributeName, this.attributeDescriptor, this.value, (newValue: any) => this.onValueChange(newValue), this.readonly);+ if (template) {+ return template;+ }+ }++ const valueType = this.getValueType();++ if (!this.type) {+ const valueDescriptor = this.getValueDescriptor();+ if (valueDescriptor && valueDescriptor.name) {+ // @ts-ignore+ this.type = InputType[valueDescriptor.name];++ if (!this.type) {+ switch (valueDescriptor.name) {+ case AttributeValueType.BOOLEAN.name:+ this.type = InputType.CHECKBOX;+ break;+ case AttributeValueType.STRING.name:+ this.type = InputType.TEXT;+ break;+ case AttributeValueType.ARRAY.name:+ case AttributeValueType.OBJECT.name:+ this.type = InputType.JSON;+ break;+ }+ }+ }+ }++ if (this.type) {+ switch (this.type) {+ case InputType.BUTTON:+ const isExecutable = this.attributeDescriptor && this.attributeDescriptor.metaItemDescriptors && this.attributeDescriptor.metaItemDescriptors.find((mid) => mid.urn === MetaItemType.EXECUTABLE.urn);+ if (isExecutable) {+ return html`<span>EXECUTABLE BUTTON NOT IMPLEMENTED</span>`;+ }+ case InputType.SWITCH_MOMENTARY:+ if (!valueType || valueType === ValueType.BOOLEAN) {+ return html`<input type="button" @onmousedown="${() => this.onValueChange(true)}" @onmouseup="${() => this.onValueChange(false)}" value="" ?readonly="${this.readonly}" />`;+ }+ break;+ case InputType.DECIMAL:+ case InputType.INTEGER:+ case InputType.NUMBER:+ case InputType.SLIDER:+ if (valueType && valueType !== ValueType.NUMBER) {+ break;+ }+ case InputType.DATE:+ case InputType.DATETIME:+ case InputType.TIME:+ if (valueType && valueType !== ValueType.NUMBER && valueType !== ValueType.STRING) {+ break;+ }+ case InputType.SWITCH_TOGGLE:+ case InputType.CHECKBOX:+ case InputType.COLOUR:+ case InputType.EMAIL:+ case InputType.PASSWORD:+ case InputType.TELEPHONE:+ case InputType.TEXT:+ case InputType.URL:+ case InputType.SELECT:+ if (this.type === InputType.TEXT || this.type === InputType.SELECT) {+ const options = this.getAllowedValues();+ if (options || this.type === InputType.SELECT) {+ return html`+ <or-select ?required="${this.required}" .options="${options}" .value="${this.value}" ?readonly="${this.readonly}" @or-select-changed="${(e: OrSelectChangedEvent) => this.onValueChange(e.detail.value)}"></or-select>+ `;+ }+ }++ const step = this.type === InputType.INTEGER ? "1" : undefined;+ const min = this.getMinValue();+ const max = this.getMaxValue();++ return html`<input ?required="${this.required}" type="${this.type}" step="${step}" min="${min}" max="${max}" value="${this.value}" @change="${(e: Event) => this.onValueChange((e.target as HTMLInputElement).value)}" />`;+ case InputType.GEO_JSON_POINT:+ break;+ }+ }++ return html`<span>INPUT TYPE NOT IMPLEMENTED</span>`;}- onChange() {- if (this.shadowRoot) {- const input = (<HTMLInputElement> this.shadowRoot.querySelector(".or-input"));- const value = input.value;- const name = input.name;+ protected onValueChange(newValue: any | undefined) {+ const previousValue = this.value;+ this.value = newValue;+ this.dispatchEvent(new OrInputChangedEvent(this.value, previousValue));+ }- // Launch event for all parent elements- const event = new CustomEvent("or-input:changed", {- detail: { value: value, name: name },- bubbles: true,- composed: true });+ protected getValueDescriptor() {+ if (this.attributeValueDescriptor) {+ return this.attributeValueDescriptor;+ }- this.dispatchEvent(event);+ if (this.attributeDescriptor) {+ return this.attributeDescriptor.valueDescriptor;}}- constructor() {- super();+ protected getValueType() {+ if (this.valueType) {+ return this.valueType;+ }++ const descriptor = this.getValueDescriptor();+ if (descriptor) {+ return descriptor.valueType;+ }++ if (this.value) {+ if (typeof this.value === "number") {+ return ValueType.NUMBER;+ }+ if (typeof this.value === "string") {+ return ValueType.STRING;+ }+ if (typeof this.value === "boolean") {+ return ValueType.BOOLEAN;+ }+ if (Array.isArray(this.value)) {+ return ValueType.ARRAY;+ }+ return ValueType.OBJECT;+ }}- protected firstUpdated(_changedProperties: PropertyValues): void {- super.firstUpdated(_changedProperties);- this.disabled = !openremote.hasRole("write:assets");+ protected getMinValue() {+ if (!this.attributeDescriptor) {+ return;+ }++ const descriptor = AssetModelUtil.getMetaInitialValueFromMetaDescriptors(MetaItemType.RANGE_MIN, this.attributeDescriptor.metaItemDescriptors);+ if (descriptor) {+ return descriptor.initialValue;+ }}-}-window.customElements.define("or-input", OrInput);+ protected getMaxValue() {+ if (!this.attributeDescriptor) {+ return;+ }++ const descriptor = AssetModelUtil.getMetaInitialValueFromMetaDescriptors(MetaItemType.RANGE_MAX, this.attributeDescriptor.metaItemDescriptors);+ if (descriptor) {+ return descriptor.initialValue;+ }+ }++ protected getAllowedValues() {+ if (!this.attributeDescriptor) {+ return;+ }++ const allowedValues = AssetModelUtil.getMetaInitialValueFromMetaDescriptors(MetaItemType.ALLOWED_VALUES, this.attributeDescriptor.metaItemDescriptors);+ if (allowedValues && Array.isArray(allowedValues) && allowedValues.length > 0) {+ return [...allowedValues];+ }+ }+}diff --git a/ui/component/or-input/src/style.ts b/ui/component/or-input/src/style.tsindex d0c1084b2..c149a1633 100644--- a/ui/component/or-input/src/style.ts+++ b/ui/component/or-input/src/style.ts@@ -1,4 +1,4 @@-import {css} from "lit-element";+ import {css} from "lit-element";// TODO this is temp work, should be replaced with material design components / styling// language=CSS@@ -15,5 +15,5 @@ export const orInputStyle = css`background-color: #f2f2f2;}- input:focus:required:invalid { border-bottom: 2px solid red;}-`;\ No newline at end of file+ input:invalid { border-bottom: 2px solid red;}+`;diff --git a/ui/component/or-input/tsconfig.json b/ui/component/or-input/tsconfig.jsonindex 0191a5e16..acb1a2837 100644--- a/ui/component/or-input/tsconfig.json+++ b/ui/component/or-input/tsconfig.json@@ -10,6 +10,6 @@],"references": [{ "path": "../core" },- { "path": "../rest" }+ { "path": "../or-select" }]}\ No newline at end of file