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
Allow creation of array type custom attributes
Rich Turner614bacec8ac5
diff --git a/manager/src/web/shared/locales/en/or.json b/manager/src/web/shared/locales/en/or.jsonindex 95b1cecf7..073962456 100644--- a/manager/src/web/shared/locales/en/or.json+++ b/manager/src/web/shared/locales/en/or.json@@ -385,5 +385,6 @@"logCategory": {},"readPermissions": "Read permissions","writePermissions": "Write permissions",- "valueType": "Value type"+ "valueType": "Value type",+ "arrayDimensions": "Array dimensions"}diff --git a/ui/component/or-asset-viewer/src/or-add-attribute-panel.ts b/ui/component/or-asset-viewer/src/or-add-attribute-panel.tsindex b787040d4..3e7d3fbc9 100644--- a/ui/component/or-asset-viewer/src/or-add-attribute-panel.ts+++ b/ui/component/or-asset-viewer/src/or-add-attribute-panel.ts@@ -1,4 +1,4 @@-import {customElement, html, LitElement, property, css} from "lit-element";+import {customElement, html, LitElement, property, css, query, PropertyValues} from "lit-element";import {InputType, OrMwcInput, OrInputChangedEvent} from "@openremote/or-mwc-components/or-mwc-input";import i18next from "i18next";import {Asset, Attribute} from "@openremote/model";@@ -32,14 +32,33 @@ export class OrAddAttributePanel extends LitElement {@property({attribute: false})protected attribute: Attribute<any> = {};+ @property()+ protected isCustom: boolean = false;++ @property()+ public isArray: boolean = false;++ @property()+ public arrayDimensions: number = 1;++ @query("#array-input")+ protected arrayInput!: OrMwcInput;++ @query("#wrapper")+ protected wrapperElem!: HTMLDivElement;++ protected attributeTypes?: [string, string][];+ protected attributeValueTypes?: [string, string][];+public static get styles() {return css`#attribute-creator > * {padding: 20px;}- #attribute-creator > * or-mwc-input {+ or-mwc-input {width: 300px;+ display: block;}.hidden {@@ -47,56 +66,66 @@ export class OrAddAttributePanel extends LitElement {}`;}+ protected shouldUpdate(_changedProperties: PropertyValues) {+ if (_changedProperties.has("asset")) {+ this.attributeTypes = undefined;+ this.attributeValueTypes = undefined;+++ this.attributeTypes = (AssetModelUtil.getAssetTypeInfo(this.asset.type!)?.attributeDescriptors || [])+ .filter((descriptor) => !this.asset.attributes![descriptor.name!])+ .sort(Util.sortByString((descriptor) => descriptor.name!))+ .map((descriptor) => {+ return [+ descriptor.name!,+ Util.getAttributeLabel(undefined, descriptor, this.asset.type, false)+ ]+ });+ this.attributeTypes = [["@custom", i18next.t("custom")], ...this.attributeTypes];++ this.attributeValueTypes = (AssetModelUtil.getAssetTypeInfo(this.asset.type!)?.valueDescriptors || [])+ .sort(Util.sortByString((descriptor) => descriptor[0]))+ .map((descriptor) => {+ return [+ descriptor,+ Util.getValueDescriptorLabel(descriptor)+ ];+ });+ }+++ return super.shouldUpdate(_changedProperties);+ }+protected render() {- let attributeTypes: [string, string][] = (AssetModelUtil.getAssetTypeInfo(this.asset.type!)?.attributeDescriptors || [])- .filter((descriptor) => !this.asset.attributes![descriptor.name!])- .sort(Util.sortByString((descriptor) => descriptor.name!))- .map((descriptor) => {- return [- descriptor.name!,- Util.getAttributeLabel(undefined, descriptor, this.asset.type, false)- ]- });- attributeTypes = [["@custom", i18next.t("custom")], ...attributeTypes];-- const attributeValueTypes: [string, string][] = (AssetModelUtil.getAssetTypeInfo(this.asset.type!)?.valueDescriptors || [])- .sort(Util.sortByString((descriptor) => descriptor[0]))- .map((descriptor) => {- return [- descriptor,- Util.getValueDescriptorLabel(descriptor)- ];- });+ if (!this.attributeTypes || !this.attributeValueTypes) {+ return;+ }return html`<div id="attribute-creator"><div>- <or-mwc-input .type="${InputType.SELECT}" .options="${attributeTypes}" .label="${i18next.t("type")}" @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this.onTypeChanged(ev.detail.value)}"></or-mwc-input>+ <or-mwc-input .type="${InputType.SELECT}" .options="${this.attributeTypes}" .label="${i18next.t("type")}" @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this.onTypeChanged(ev.detail.value)}"></or-mwc-input></div>- <div id="name-input" class="hidden">- <or-mwc-input .type="${InputType.TEXT}" .label="${i18next.t("name")}" pattern="\\w+" required @keyup="${(ev: KeyboardEvent) => this.onNameChanged((ev.target as OrMwcInput).currentValue)}" @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this.onNameChanged(ev.detail.value)}"></or-mwc-input>- </div>- <div id="type-input" class="hidden">- <or-mwc-input .type="${InputType.SELECT}" .options="${attributeValueTypes}" .label="${i18next.t("valueType")}" @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this.onValueTypeChanged(ev.detail.value)}"></or-mwc-input>+ <div id="wrapper" class="hidden">+ <or-mwc-input id="name-input" .type="${InputType.TEXT}" .label="${i18next.t("name")}" pattern="\\w+" required @keyup="${(ev: KeyboardEvent) => this.onNameChanged((ev.target as OrMwcInput).currentValue)}" @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this.onNameChanged(ev.detail.value)}"></or-mwc-input>+ <or-mwc-input id="type-input" .type="${InputType.SELECT}" .value="${this.attribute ? this.attribute.type : undefined}" .options="${this.attributeValueTypes}" .label="${i18next.t("valueType")}" @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this.onValueTypeChanged(ev.detail.value)}"></or-mwc-input>+ <or-mwc-input id="array-checkbox" .type="${InputType.CHECKBOX}" .value="${this.isArray}" .label="${i18next.t("array")}" @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this.onArrayChanged(ev.detail.value, 1)}"></or-mwc-input>+ <or-mwc-input id="array-input" .type="${InputType.NUMBER}" ?disabled="${!this.isArray}" .value="${this.arrayDimensions}" min="1" max="2" .label="${i18next.t("arrayDimensions")}" @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this.onArrayChanged(true, ev.detail.value)}"></or-mwc-input></div></div>`;}protected onTypeChanged(name: string) {- const nameInput = this.shadowRoot!.getElementById("name-input");- const typeInput = this.shadowRoot!.getElementById("type-input");-if (name === "@custom") {- nameInput!.classList.remove("hidden");- typeInput!.classList.remove("hidden");+ this.wrapperElem.classList.remove("hidden");this.attribute = {meta: {}};} else {- nameInput!.classList.add("hidden");- typeInput!.classList.add("hidden");+ this.wrapperElem.classList.add("hidden");const descriptor = AssetModelUtil.getAttributeDescriptor(name, this.asset.type!)!;this.attribute = {};this.attribute.name = descriptor.name;@@ -117,8 +146,28 @@ export class OrAddAttributePanel extends LitElement {};protected onValueTypeChanged(valueType: string) {- // @ts-ignorethis.attribute.type = valueType;- this.dispatchEvent(new OrAddAttributePanelAttributeChangedEvent(this.attribute));+ this.updateAttributeType();};++ protected onArrayChanged(array: boolean, dimensions: number) {+ this.isArray = array;+ this.arrayDimensions = dimensions;+ this.updateAttributeType();+ }++ protected updateAttributeType() {+ if (!this.attribute || !this.attribute.type) {+ return;+ }++ let type = this.attribute.type.replace("[]", "");+ if (this.isArray) {+ for (let i=1; i<=this.arrayDimensions; i++) {+ type += "[]";+ }+ }+ this.attribute.type = type;+ this.dispatchEvent(new OrAddAttributePanelAttributeChangedEvent(this.attribute));+ }}