Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

UI updates: Added ordinal enum ALLOWED_VALUES meta item support, enabled asset editor save asset and other minor updates

Rich Turnerd44789e31a4e
diff --git a/deployment/manager/shared/locales/en/or.json b/deployment/manager/shared/locales/en/or.json
index 91544ced4..c1a27b728 100644
--- a/deployment/manager/shared/locales/en/or.json
+++ b/deployment/manager/shared/locales/en/or.json
@@ -317,5 +317,10 @@
"urn:openremote:asset:meta:ruleEventExpires": "Rule event expires",
"urn:openremote:asset:meta:protocolConfiguration": "Protocol configuration"
},
- "addMetaItems": "Add configuration items"
+ "addMetaItems": "Add configuration items",
+ "assetModified": "Asset modified",
+ "confirmContinueAssetModified": "Asset has been modified, discard changes?",
+ "noCommand": "No command",
+ "start": "Start",
+ "stop": "Stop"
}
diff --git a/model/src/main/java/org/openremote/model/asset/AssetType.java b/model/src/main/java/org/openremote/model/asset/AssetType.java
index 6f4ae237c..87d381d2d 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetType.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetType.java
@@ -594,7 +594,11 @@ public enum AssetType implements AssetDescriptor {
DESCRIPTION.withInitialValue("Positive is consuming, negative is delivering to network")),
new AttributeDescriptorImpl("command", NUMBER, null,
LABEL.withInitialValue("Command"),
- DESCRIPTION.withInitialValue("0 = No command, 1 = Start, 2 = Stop")),
+ DESCRIPTION.withInitialValue("0 = No command, 1 = Start, 2 = Stop"),
+ ALLOWED_VALUES.withInitialValue(Values.createArray(
+ Values.create("noCommand"),
+ Values.create("start"),
+ Values.create("stop")))),
new AttributeDescriptorImpl("energyCharged", NUMBER, null,
LABEL.withInitialValue("Energy charged"),
UNIT_TYPE.withInitialValue(UNITS_ENERGY_KILOWATT_HOUR),
diff --git a/model/src/main/java/org/openremote/model/attribute/MetaItemType.java b/model/src/main/java/org/openremote/model/attribute/MetaItemType.java
index 6caf8692a..4ac793b5d 100644
--- a/model/src/main/java/org/openremote/model/attribute/MetaItemType.java
+++ b/model/src/main/java/org/openremote/model/attribute/MetaItemType.java
@@ -401,8 +401,9 @@ public enum MetaItemType implements MetaItemDescriptor {
true),
/**
- * Indicates the discreet values allowed for this attribute, only makes sense for attributes that
- * support a limited number of values otherwise can be <code>null</code>.
+ * Indicates the discreet values allowed for this attribute, only makes sense for attributes that support a
+ * limited number of values otherwise can be <code>null</code>. For number attributes, if the values are of type
+ * string then they are assumed to define zero based enum ordinal values.
*/
ALLOWED_VALUES(
ASSET_META_NAMESPACE + ":allowedValues",
diff --git a/ui/component/or-asset-tree/src/index.ts b/ui/component/or-asset-tree/src/index.ts
index 49a41fd18..f20684bcc 100644
--- a/ui/component/or-asset-tree/src/index.ts
+++ b/ui/component/or-asset-tree/src/index.ts
@@ -108,6 +108,19 @@ export class OrAssetTreeRequestAddEvent extends CustomEvent<RequestEventDetail<A
}
}
+export class OrAssetTreeAssetAddedEvent extends CustomEvent<Asset> {
+
+ public static readonly NAME = "or-asset-tree-asset-added";
+
+ constructor(asset: Asset) {
+ super(OrAssetTreeRequestAddEvent.NAME, {
+ bubbles: true,
+ composed: true,
+ detail: asset
+ });
+ }
+}
+
export class OrAssetTreeRequestDeleteEvent extends CustomEvent<RequestEventDetail<UiAssetTreeNode[]>> {
public static readonly NAME = "or-asset-tree-request-delete";
@@ -248,6 +261,7 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
protected _connected: boolean = false;
protected _selectedNodes: UiAssetTreeNode[] = [];
protected _initCallback?: EventCallback;
+ protected _addedAssetId?: string;
public get selectedNodes(): UiAssetTreeNode[] {
return this._selectedNodes ? [...this._selectedNodes] : [];
@@ -611,7 +625,7 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
this._nodes = undefined;
}).catch((reason) => {
// TODO: Error announcement
- // Clear nodes to refetch them
+ // Clear nodes to re-fetch them
this._nodes = undefined;
});
this.disabled = true;
@@ -621,10 +635,10 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
return window.confirm(i18next.t("confirmDeleteAssets"));
}
- protected _doAdd(asset: Asset) {
- manager.rest.api.AssetResource.create(asset).then((response) => {
- this._loadAssets();
- });
+ protected async _doAdd(asset: Asset) {
+ const response = await manager.rest.api.AssetResource.create(asset);
+ this._addedAssetId = response.data.id!;
+ this.dispatchEvent(new OrAssetTreeAssetAddedEvent(response.data));
}
protected _doCopy(node: UiAssetTreeNode) {
@@ -727,6 +741,10 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
}
});
this._buildTreeNodes(assets, this._getSortFunction());
+ if (this._addedAssetId) {
+ this.selectedIds = [this._addedAssetId];
+ this._addedAssetId = undefined;
+ }
}
}
diff --git a/ui/component/or-asset-viewer/src/index.ts b/ui/component/or-asset-viewer/src/index.ts
index c481c3f2a..476e97961 100644
--- a/ui/component/or-asset-viewer/src/index.ts
+++ b/ui/component/or-asset-viewer/src/index.ts
@@ -160,18 +160,43 @@ class EventHandler {
}
}
-export class OrComputeGridEvent extends CustomEvent<void> {
+export class OrAssetViewerComputeGridEvent extends CustomEvent<void> {
- public static readonly NAME = "or-compute-grid-event";
+ public static readonly NAME = "or-asset-viewer-compute-grid-event";
constructor() {
- super(OrComputeGridEvent.NAME, {
+ super(OrAssetViewerComputeGridEvent.NAME, {
bubbles: true,
composed: true
});
}
}
+export type SaveResult = {
+ asset: Asset,
+ statusCode: number
+};
+
+export class OrAssetViewerSaveResultEvent extends CustomEvent<SaveResult> {
+
+ public static readonly NAME = "or-asset-viewer-save-result-event";
+
+ constructor(saveResult:SaveResult) {
+ super(OrAssetViewerComputeGridEvent.NAME, {
+ bubbles: true,
+ composed: true,
+ detail: saveResult
+ });
+ }
+}
+
+declare global {
+ export interface HTMLElementEventMap {
+ [OrAssetViewerComputeGridEvent.NAME]: OrAssetViewerComputeGridEvent;
+ [OrAssetViewerSaveResultEvent.NAME]: OrAssetViewerSaveResultEvent;
+ }
+}
+
const onRenderComplete = new EventHandler();
export function getPanel(name: string, panelConfig: PanelConfig, content: TemplateResult | undefined) {
@@ -731,10 +756,10 @@ export class OrAssetViewer extends subscribe(manager)(translate(i18next)(LitElem
constructor() {
super();
- this.addEventListener(OrComputeGridEvent.NAME, () => OrAssetViewer.generateGrid(this.shadowRoot));
+ this.addEventListener(OrAssetViewerComputeGridEvent.NAME, () => OrAssetViewer.generateGrid(this.shadowRoot));
this.addEventListener(OrChartEvent.NAME, () => OrAssetViewer.generateGrid(this.shadowRoot));
this.addEventListener(OrAttributeHistoryEvent.NAME, () => OrAssetViewer.generateGrid(this.shadowRoot));
- this.addEventListener(OrEditAssetChangedEvent.NAME, () => this._onAssetChanged());
+ this.addEventListener(OrEditAssetChangedEvent.NAME, () => this._onAssetModified());
}
public isModified() {
@@ -825,10 +850,10 @@ export class OrAssetViewer extends subscribe(manager)(translate(i18next)(LitElem
</a>
<div id="title">
<or-icon title="${descriptor && descriptor.type ? descriptor.type : "unset"}" style="--or-icon-fill: ${descriptor && descriptor.color ? "#" + descriptor.color : "unset"}" icon="${descriptor && descriptor.icon ? descriptor.icon : AssetType.THING.icon}"></or-icon>
- ${editMode ? html`<or-input id="name-input" .type="${InputType.TEXT}" min="1" max="1023" required outlined .label="${i18next.t("name")}" .value="${this.asset.name}"></or-input>` : html`<span>${this.asset.name}</span>`}
+ ${editMode ? html`<or-input id="name-input" .type="${InputType.TEXT}" min="1" max="1023" required outlined .label="${i18next.t("name")}" .value="${this.asset.name}" @or-input-changed="${(e: OrInputChangedEvent) => {this.asset!.name = e.detail.value; this._onAssetModified();}}"></or-input>` : html`<span>${this.asset.name}</span>`}
</div>
<div id="created-time" class="mobileHidden"><or-translate value="createdOnWithDate" .options="${{ date: new Date(this.asset!.createdOn!) } as i18next.TOptions<i18next.InitOptions>}"></or-translate></div>
- ${editMode ? html`<or-input id="save-btn" raised .type="${InputType.BUTTON}" .label="${i18next.t("save")}" @or-input-changed="${() => this._saveAsset()}"></or-input>` : ``}
+ ${editMode ? html`<or-input id="save-btn" .disabled="${!this.isModified()}" raised .type="${InputType.BUTTON}" .label="${i18next.t("save")}" @or-input-changed="${() => this._saveAsset()}"></or-input>` : ``}
</div>
<div id="container" style="${this._viewerConfig.viewerStyles ? styleMap(this._viewerConfig.viewerStyles) : ""}">
${content}
@@ -850,11 +875,7 @@ export class OrAssetViewer extends subscribe(manager)(translate(i18next)(LitElem
super.assetIds = undefined;
}
} else if (_changedProperties.has("editMode") && !this.editMode) {
- this.asset = undefined;
- if (this.assetId) {
- this._loading = true;
- super._refreshEventSubscriptions();
- }
+ this._reloadAsset();
}
this.onCompleted().then(() => {
@@ -864,6 +885,15 @@ export class OrAssetViewer extends subscribe(manager)(translate(i18next)(LitElem
});
}
+ protected _reloadAsset() {
+ this.asset = undefined;
+ this._assetModified = false;
+ if (this.assetId) {
+ this._loading = true;
+ super._refreshEventSubscriptions();
+ }
+ }
+
protected _isReadonly() {
return this.readonly || !manager.hasRole(ClientRole.WRITE_ASSETS);
}
@@ -876,11 +906,22 @@ export class OrAssetViewer extends subscribe(manager)(translate(i18next)(LitElem
}
- protected _saveAsset() {
-
+ protected async _saveAsset() {
+ if (!this.asset) {
+ return;
+ }
+ const response = await manager.rest.api.AssetResource.update(this.asset.id!, this.asset);
+ this.dispatchEvent(new OrAssetViewerSaveResultEvent({
+ asset: this.asset,
+ statusCode: response.status
+ }));
+
+ if (response.status === 204) {
+ this._reloadAsset();
+ }
}
- protected _onAssetChanged() {
+ protected _onAssetModified() {
this._assetModified = true;
this.requestUpdate();
}
diff --git a/ui/component/or-asset-viewer/src/or-edit-asset-panel.ts b/ui/component/or-asset-viewer/src/or-edit-asset-panel.ts
index b8f76ece1..06cb1bb33 100644
--- a/ui/component/or-asset-viewer/src/or-edit-asset-panel.ts
+++ b/ui/component/or-asset-viewer/src/or-edit-asset-panel.ts
@@ -113,6 +113,7 @@ export class OrEditAssetChangedEvent extends CustomEvent<void> {
});
}
}
+
declare global {
export interface HTMLElementEventMap {
[OrEditAssetChangedEvent.NAME]: OrEditAssetChangedEvent;
@@ -140,7 +141,7 @@ export class OrEditAssetPanel extends LitElement {
const updatePublicRead = (publicRead: boolean) => {
this.asset.accessPublicRead = publicRead;
- this.onModified();
+ this._onModified();
};
// Properties panel fields
@@ -207,7 +208,7 @@ export class OrEditAssetPanel extends LitElement {
const deleteAttribute = () => {
this.attrs!.splice(this.attrs!.indexOf(attribute), 1);
- this.onModified();
+ this._onModified();
};
return html`
@@ -236,7 +237,7 @@ export class OrEditAssetPanel extends LitElement {
`;
};
- protected onModified() {
+ protected _onModified() {
this.dispatchEvent(new OrEditAssetChangedEvent());
this.requestUpdate();
}
@@ -245,7 +246,7 @@ export class OrEditAssetPanel extends LitElement {
const removeMetaItem = () => {
attribute.meta!.splice(attribute.meta!.indexOf(metaItem), 1);
- this.onModified();
+ this._onModified();
};
const descriptor = AssetModelUtil.getMetaItemDescriptor(metaItem.name);
@@ -335,7 +336,7 @@ export class OrEditAssetPanel extends LitElement {
actionName: "add",
action: () => {
this.attrs!.push(attr);
- this.onModified();
+ this._onModified();
},
content: html`<or-input id="add-btn" .type="${InputType.BUTTON}" disabled .label="${i18next.t("add")}"></or-input>`
},
@@ -406,7 +407,7 @@ export class OrEditAssetPanel extends LitElement {
value: descriptor.initialValue
}
);
- this.onModified();
+ this._onModified();
}
});
}
@@ -441,13 +442,13 @@ export class OrEditAssetPanel extends LitElement {
parentNode = parentNode.parent;
}
this.asset.path = path;
- this.onModified();
+ this._onModified();
};
const clearParent = () => {
this.asset.parentId = undefined;
this.asset.path = [this.asset.id!];
- this.onModified();
+ this._onModified();
};
const blockEvent = (ev: Event) => {
diff --git a/ui/component/or-attribute-input/src/index.ts b/ui/component/or-attribute-input/src/index.ts
index b76a7445d..5b8f8ff7c 100644
--- a/ui/component/or-attribute-input/src/index.ts
+++ b/ui/component/or-attribute-input/src/index.ts
@@ -745,16 +745,21 @@ export class OrAttributeInput extends subscribe(manager)(translate(i18next)(LitE
this._step = Util.getMetaValue(MetaItemType.STEP, this.attribute, this._attributeDescriptor, this._attributeValueDescriptor) as number;
this._options = Util.getMetaValue(MetaItemType.ALLOWED_VALUES, this.attribute, this._attributeDescriptor);
- if (!this.inputType && this._inputType === InputType.TEXT && this._options && Array.isArray(this._options) && this._options.length > 0) {
- this._inputType = InputType.SELECT;
- }
-
- if (!this.inputType && this._inputType === InputType.TEXT && Util.getMetaValue(MetaItemType.MULTILINE, this.attribute, this._attributeDescriptor, this._attributeValueDescriptor)) {
- this._inputType = InputType.TEXTAREA;
+ if (this._inputType === InputType.TEXT) {
+ if (this._options && Array.isArray(this._options) && this._options.length > 0) {
+ this._inputType = InputType.SELECT;
+ } else if (Util.getMetaValue(MetaItemType.MULTILINE, this.attribute, this._attributeDescriptor, this._attributeValueDescriptor)) {
+ this._inputType = InputType.TEXTAREA;
+ }
}
- if (!this.inputType && this._inputType === InputType.NUMBER && this._min !== undefined && this._max) {
- this._inputType = InputType.RANGE;
+ if (this._inputType === InputType.NUMBER) {
+ if (this._options && Array.isArray(this._options) && this._options.length > 0) {
+ this._inputType = InputType.SELECT;
+ this._options = this._options.map((opt, index) => [index, opt]);
+ } else if (this._min !== undefined && this._max) {
+ this._inputType = InputType.RANGE;
+ }
}
this._valueFormat = Util.getAttributeValueFormat(this.attribute, this._attributeDescriptor, this._attributeValueDescriptor);
diff --git a/ui/component/or-input/src/index.ts b/ui/component/or-input/src/index.ts
index b32c462ec..6708bad66 100644
--- a/ui/component/or-input/src/index.ts
+++ b/ui/component/or-input/src/index.ts
@@ -692,55 +692,57 @@ export class OrInput extends LitElement {
componentId = "number";
}
- const classes = {
- "mdc-text-field": true,
- "mdc-text-field--filled": !outlined,
- "mdc-text-field--outlined": outlined,
- "mdc-text-field--textarea": type === InputType.TEXTAREA || type === InputType.JSON,
- "mdc-text-field--disabled": this.disabled,
- "mdc-text-field--fullwidth": this.fullWidth && !outlined,
- "dense-comfortable": denseComfortable,
- "dense-compact": denseCompact,
- "is-rounded": rounded,
- "mdc-text-field--label-floating" : hasValue,
- "mdc-text-field--no-label": !this.label,
- "mdc-text-field--with-leading-icon": !!this.icon,
- "mdc-text-field--with-trailing-icon": !!this.iconTrailing
- };
+ if (!(this.type === InputType.RANGE && this.disableSliderNumberInput)) {
+ const classes = {
+ "mdc-text-field": true,
+ "mdc-text-field--filled": !outlined,
+ "mdc-text-field--outlined": outlined,
+ "mdc-text-field--textarea": type === InputType.TEXTAREA || type === InputType.JSON,
+ "mdc-text-field--disabled": this.disabled,
+ "mdc-text-field--fullwidth": this.fullWidth && !outlined,
+ "dense-comfortable": denseComfortable,
+ "dense-compact": denseCompact,
+ "is-rounded": rounded,
+ "mdc-text-field--label-floating": hasValue,
+ "mdc-text-field--no-label": !this.label,
+ "mdc-text-field--with-leading-icon": !!this.icon,
+ "mdc-text-field--with-trailing-icon": !!this.iconTrailing
+ };
- inputElem = type === InputType.TEXTAREA || type === InputType.JSON
- ? html`
- <textarea id="elem" class="mdc-text-field__input" ?required="${this.required}"
- ?readonly="${this.readonly}" ?disabled="${this.disabled}" minlength="${ifDefined(this.minLength)}"
- maxlength="${ifDefined(this.maxLength)}" rows="${this.rows ? this.rows : 5}"
- cols="${ifDefined(this.cols)}" aria-label="${ifDefined(label)}"
- aria-labelledby="${ifDefined(label ? "label" : undefined)}"
- @change="${(e: Event) => this.onValueChange((e.target as HTMLTextAreaElement), (e.target as HTMLTextAreaElement).value)}">${valMinMax[0] ? valMinMax[0] : ""}</textarea>`
- : html`
- <input type="${type}" id="elem" aria-labelledby="${ifDefined(label ? "label" : undefined)}"
- class="mdc-text-field__input" ?required="${this.required}" ?readonly="${this.readonly}"
- ?disabled="${this.disabled}" min="${ifDefined(valMinMax[1])}" max="${ifDefined(valMinMax[2])}"
- step="${ifDefined(this.step)}" minlength="${ifDefined(this.minLength)}" pattern="${ifDefined(this.pattern)}"
- maxlength="${ifDefined(this.maxLength)}" placeholder="${ifDefined(this.placeHolder)}"
- .value="${valMinMax[0] !== null && valMinMax[0] !== undefined ? valMinMax[0] : ""}"
- @change="${(e: Event) => this.onValueChange((e.target as HTMLInputElement), (e.target as HTMLInputElement).value)}" />`;
-
- inputElem = html`
- <label id="${componentId}" class="${classMap(classes)}">
- ${this.icon ? html`<or-icon class="mdc-text-field__icon mdc-text-field__icon--leading" aria-hidden="true" icon="${this.icon}"></or-icon>` : ``}
- ${outlined ? `` : html`<span class="mdc-text-field__ripple"></span>`}
- ${inputElem}
- ${outlined ? this.renderOutlined(labelTemplate) : labelTemplate}
- ${outlined ? `` : html`<span class="mdc-line-ripple"></span>`}
- ${this.iconTrailing ? html`<or-icon class="mdc-text-field__icon mdc-text-field__icon--trailing" aria-hidden="true" icon="${this.iconTrailing}"></or-icon>` : ``}
- </label>
- ${hasHelper ? html`
- <div class="mdc-text-field-helper-line">
- <div class="mdc-text-field-helper-text ${classMap(helperClasses)}">${showValidationMessage ? this.validationMessage : this.helperText}</div>
- ${this.charCounter && !this.readonly ? html`<div class="mdc-text-field-character-counter"></div>` : ``}
- </div>
- ` : ``}
- `;
+ inputElem = type === InputType.TEXTAREA || type === InputType.JSON
+ ? html`
+ <textarea id="elem" class="mdc-text-field__input" ?required="${this.required}"
+ ?readonly="${this.readonly}" ?disabled="${this.disabled}" minlength="${ifDefined(this.minLength)}"
+ maxlength="${ifDefined(this.maxLength)}" rows="${this.rows ? this.rows : 5}"
+ cols="${ifDefined(this.cols)}" aria-label="${ifDefined(label)}"
+ aria-labelledby="${ifDefined(label ? "label" : undefined)}"
+ @change="${(e: Event) => this.onValueChange((e.target as HTMLTextAreaElement), (e.target as HTMLTextAreaElement).value)}">${valMinMax[0] ? valMinMax[0] : ""}</textarea>`
+ : html`
+ <input type="${type}" id="elem" aria-labelledby="${ifDefined(label ? "label" : undefined)}"
+ class="mdc-text-field__input" ?required="${this.required}" ?readonly="${this.readonly}"
+ ?disabled="${this.disabled}" min="${ifDefined(valMinMax[1])}" max="${ifDefined(valMinMax[2])}"
+ step="${ifDefined(this.step)}" minlength="${ifDefined(this.minLength)}" pattern="${ifDefined(this.pattern)}"
+ maxlength="${ifDefined(this.maxLength)}" placeholder="${ifDefined(this.placeHolder)}"
+ .value="${valMinMax[0] !== null && valMinMax[0] !== undefined ? valMinMax[0] : ""}"
+ @change="${(e: Event) => this.onValueChange((e.target as HTMLInputElement), (e.target as HTMLInputElement).value)}" />`;
+
+ inputElem = html`
+ <label id="${componentId}" class="${classMap(classes)}">
+ ${this.icon ? html`<or-icon class="mdc-text-field__icon mdc-text-field__icon--leading" aria-hidden="true" icon="${this.icon}"></or-icon>` : ``}
+ ${outlined ? `` : html`<span class="mdc-text-field__ripple"></span>`}
+ ${inputElem}
+ ${outlined ? this.renderOutlined(labelTemplate) : labelTemplate}
+ ${outlined ? `` : html`<span class="mdc-line-ripple"></span>`}
+ ${this.iconTrailing ? html`<or-icon class="mdc-text-field__icon mdc-text-field__icon--trailing" aria-hidden="true" icon="${this.iconTrailing}"></or-icon>` : ``}
+ </label>
+ ${hasHelper ? html`
+ <div class="mdc-text-field-helper-line">
+ <div class="mdc-text-field-helper-text ${classMap(helperClasses)}">${showValidationMessage ? this.validationMessage : this.helperText}</div>
+ ${this.charCounter && !this.readonly ? html`<div class="mdc-text-field-character-counter"></div>` : ``}
+ </div>
+ ` : ``}
+ `;
+ }
if (this.type === InputType.RANGE) {
inputElem = html`