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: Fix or-attribute-card and or-chart

Rich Turnerea195284d62c
diff --git a/ui/component/core/src/event.ts b/ui/component/core/src/event.ts
index e0512f3d8..7c302dbb3 100644
--- a/ui/component/core/src/event.ts
+++ b/ui/component/core/src/event.ts
@@ -672,7 +672,7 @@ export class WebSocketEventProvider extends EventProviderImpl {
protected _doSendWithReply<T extends SharedEvent, U extends SharedEvent>(event: EventRequestResponseWrapper<T>): Promise<U> {
if (!event.messageId) {
- event.messageId = new Date().getTime().toString(10);
+ event.messageId = (new Date().getTime() + (Math.random() * 10)).toString(10);
}
if (this._repliesDeferred.has(event.messageId)) {
diff --git a/ui/component/or-asset-tree/src/index.ts b/ui/component/or-asset-tree/src/index.ts
index 7dd4f9aa4..b8d77e8eb 100644
--- a/ui/component/or-asset-tree/src/index.ts
+++ b/ui/component/or-asset-tree/src/index.ts
@@ -4,7 +4,7 @@ import "@openremote/or-icon";
import {Asset, AssetQuery, AssetTreeNode, ClientRole, SharedEvent, AssetsEvent, AssetEvent, AssetEventCause} from "@openremote/model";
import "@openremote/or-translate";
import {style} from "./style";
-import manager, {AssetModelUtil, OREvent, EventCallback, subscribe} from "@openremote/core";
+import manager, {Util, AssetModelUtil, OREvent, EventCallback, subscribe} from "@openremote/core";
import {InputType} from "@openremote/or-input";
import Qs from "qs";
import {getAssetDescriptorIconTemplate} from "@openremote/or-icon";
@@ -262,7 +262,9 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
}
if (_changedProperties.has("selectedIds")) {
- this._updateSelectedNodes();
+ if (!Util.objectsEqual(_changedProperties.get("selectedIds"), this.selectedIds)) {
+ this._updateSelectedNodes();
+ }
}
if (_changedProperties.has("sortBy")) {
diff --git a/ui/component/or-attribute-card/src/index.ts b/ui/component/or-attribute-card/src/index.ts
index b472d5ee5..d405d0626 100644
--- a/ui/component/or-attribute-card/src/index.ts
+++ b/ui/component/or-attribute-card/src/index.ts
@@ -1,7 +1,7 @@
import {css, customElement, html, LitElement, property, PropertyValues, query, unsafeCSS} from "lit-element";
import {classMap} from "lit-html/directives/class-map";
import i18next from "i18next";
-import {Asset, AssetAttribute, Attribute, DatapointInterval, MetaItemType, ValueDatapoint} from "@openremote/model";
+import {Asset, AssetAttribute, Attribute, DatapointInterval, MetaItemType, ValueDatapoint, AssetEvent} from "@openremote/model";
import {manager, DefaultColor3, DefaultColor4, Util, AssetModelUtil} from "@openremote/core";
import Chart, {ChartTooltipCallback} from "chart.js";
import {getContentWithMenuTemplate, OrChartConfig} from "@openremote/or-chart";
@@ -10,7 +10,7 @@ import {getAssetDescriptorIconTemplate} from "@openremote/or-icon";
import "@openremote/or-mwc-components/dist/or-mwc-dialog";
import {getMetaValue} from "@openremote/core/dist/util";
import moment from "moment";
-import {OrAssetTreeRequestSelectEvent} from "@openremote/or-asset-tree";
+import {OrAssetTreeRequestSelectEvent, OrAssetTreeSelectionChangedEvent} from "@openremote/or-asset-tree";
import {DialogAction, OrMwcDialog} from "@openremote/or-mwc-components/dist/or-mwc-dialog";
const dialogStyle = require("!!raw-loader!@material/dialog/dist/mdc.dialog.css");
@@ -189,16 +189,16 @@ export class OrAttributeCard extends LitElement {
private now: Date = new Date();
private currentPeriod?: { start: number; end: number };
- private asset: Asset = {};
- private formattedMainValue: {value: number|undefined, unit: string} = {
- value: undefined,
- unit: ""
- };
+ private asset?: Asset;
+ private formattedMainValue?: {value: number|undefined, unit: string};
@query("#chart")
private _chartElem!: HTMLCanvasElement;
private _chart?: Chart;
+ @query("#mdc-dialog")
+ private _dialog!: OrMwcDialog;
+
static get styles() {
return [
css`${unsafeCSS(dialogStyle)}`,
@@ -206,56 +206,35 @@ export class OrAttributeCard extends LitElement {
];
}
- constructor(){
+ constructor() {
super();
this.addEventListener(OrAttributeCardAddAttributeEvent.NAME, this._setAttribute)
}
connectedCallback() {
super.connectedCallback();
-
+ this.addEventListener(OrAssetTreeSelectionChangedEvent.NAME, this._onTreeSelectionChanged);
this._style = window.getComputedStyle(this);
this.getData();
}
-
- renderDialogHTML() {
- const dialog: OrMwcDialog = this.shadowRoot!.getElementById("mdc-dialog") as OrMwcDialog;
- if(!this.shadowRoot) return
- if (dialog) {
- dialog.dialogContent =html`
- <or-asset-tree id="chart-asset-tree"
- .selectedIds="${this.asset ? [this.asset.id] : null}]"
- @or-asset-tree-request-select="${(e: OrAssetTreeRequestSelectEvent) => {
- this.asset = {...e.detail.detail.node.asset!}
- this._getAttributeOptions();
- let attributes = [...Util.getAssetAttributes(this.asset)];
- this.assetAttributes = [...attributes];
- }}">
- </or-asset-tree>
- ${this.asset && this.asset.attributes ? html`
- <or-input id="attribute-picker"
- style="display:flex;"
- .label="${i18next.t("attribute")}"
- .type="${InputType.LIST}"
- .options="${this._getAttributeOptions()}"
- .value="${this.attributeName}"></or-input>
- ` : ``}
- `;
- this.requestUpdate();
- }
+ disconnectedCallback(): void {
+ super.disconnectedCallback();
+ this._cleanup();
+ this.removeEventListener(OrAssetTreeSelectionChangedEvent.NAME, this._onTreeSelectionChanged);
}
updated(changedProperties: PropertyValues) {
- if (changedProperties.has("assetAttributes")) {
- this.renderDialogHTML();
+ if (changedProperties.has("asset") || changedProperties.has("assetAttributes")) {
+ if (this._dialog && this._dialog.isOpen) {
+ this._refreshDialog();
+ }
}
if (changedProperties.has("assetId") && this.assetId && changedProperties.get("assetId") !== this.assetId) {
this.getSettings();
}
-
if (!this.data || !this.data.length) {
return;
}
@@ -318,32 +297,17 @@ export class OrAttributeCard extends LitElement {
if (changedProperties.has("mainValue")) {
this.formattedMainValue = this.getFormattedValue(this.mainValue!);
}
-
}
async onCompleted() {
await this.updateComplete;
}
- disconnectedCallback(): void {
- super.disconnectedCallback();
- this._cleanup();
- }
-
- protected _openDialog() {
- const dialog: OrMwcDialog = this.shadowRoot!.getElementById("mdc-dialog") as OrMwcDialog;
-
- if (dialog) {
- dialog.dialogContent = html`
+ protected _refreshDialog() {
+ if (this._dialog) {
+ this._dialog.dialogContent = html`
<or-asset-tree id="chart-asset-tree"
- .selectedIds="${this.asset ? [this.asset.id] : null}]"
- @or-asset-tree-request-select="${(e: OrAssetTreeRequestSelectEvent) => {
- this.asset = {...e.detail.detail.node.asset!};
- let attributes = [...Util.getAssetAttributes(this.asset)];
- this.assetAttributes = [...attributes];
- this._getAttributeOptions();
- }}">
- </or-asset-tree>
+ .selectedIds="${this.asset ? [this.asset.id] : null}"></or-asset-tree>
${this.asset && this.asset.attributes ? html`
<or-input id="attribute-picker"
style="display:flex;"
@@ -353,7 +317,38 @@ export class OrAttributeCard extends LitElement {
.value="${this.attributeName}"></or-input>
` : ``}
`;
- dialog.open();
+ }
+ }
+
+ protected _openDialog() {
+ if (this._dialog) {
+ this._refreshDialog();
+ this._dialog.open();
+ }
+ }
+
+ protected _onTreeSelectionChanged(event: OrAssetTreeSelectionChangedEvent) {
+ // Need to fully load the asset
+ if (!manager.events) {
+ return;
+ }
+
+ const selectedNode = event.detail && event.detail.length > 0 ? event.detail[0] : undefined;
+
+ if (!selectedNode) {
+ this.asset = undefined;
+ } else {
+ // fully load the asset
+ manager.events.sendEventWithReply({
+ event: {
+ eventType: "read-asset",
+ assetId: selectedNode.asset!.id
+ }
+ }).then((ev) => {
+ this.asset = (ev as AssetEvent).asset;
+ const attributes = [...Util.getAssetAttributes(this.asset!)];
+ this.assetAttributes = [...attributes];
+ }).catch(() => this.asset = undefined);
}
}
@@ -517,7 +512,7 @@ export class OrAttributeCard extends LitElement {
<div class="panel" id="attribute-card">
<div class="panel-content-wrapper">
<div class="panel-title">
- <span class="panel-title-text">${this.asset.name} - ${i18next.t(this.attributeName)}</span>
+ <span class="panel-title-text">${this.asset ? (this.asset.name + " - " + i18next.t(this.attributeName)) : ""}</span>
<or-input icon="pencil" type="button" @click="${() => this._openDialog()}"></or-input>
</div>
<div class="panel-content">
@@ -646,7 +641,11 @@ export class OrAttributeCard extends LitElement {
return Math.max.apply(Math, data.map((e: ValueDatapoint<any>) => e.y || false ));
}
- protected getFormattedValue(value: number): {value: number, unit: string} {
+ protected getFormattedValue(value: number): {value: number, unit: string} | undefined {
+ if (!this.asset) {
+ return;
+ }
+
const attr = this.asset.attributes![this.attributeName!];
const roundedVal = +value.toFixed(1); // + operator prevents str return
diff --git a/ui/component/or-chart/src/index.ts b/ui/component/or-chart/src/index.ts
index 27b45b492..b36c5064b 100644
--- a/ui/component/or-chart/src/index.ts
+++ b/ui/component/or-chart/src/index.ts
@@ -11,7 +11,7 @@ import {
} from "lit-element";
import i18next from "i18next";
import {translate} from "@openremote/or-translate";
-import {Asset, AssetAttribute, AttributeRef, DatapointInterval, MetaItemType, Attribute} from "@openremote/model";
+import {Asset, AssetAttribute, AttributeRef, DatapointInterval, MetaItemType, Attribute, ReadAssetEvent, AssetEvent} from "@openremote/model";
import manager, {
AssetModelUtil,
DefaultColor2,
@@ -398,12 +398,7 @@ export class OrChart extends translate(i18next)(LitElement) {
protected _style!: CSSStyleDeclaration;
firstUpdated() {
- if(this.shadowRoot) {
- const assetTreeElement = this.shadowRoot.getElementById('chart-asset-tree');
- if(assetTreeElement){
- assetTreeElement.addEventListener(OrAssetTreeSelectionChangedEvent.NAME, (evt) => this._onTreeSelectionChanged(evt));
- }
-
+ if (this._dialogElem) {
this._dialog = new MDCDialog(this._dialogElem);
if(!this.activeAssetId) {
this.getSettings();
@@ -411,30 +406,42 @@ export class OrChart extends translate(i18next)(LitElement) {
}
}
-
protected _onTreeSelectionChanged(event: OrAssetTreeSelectionChangedEvent) {
- const nodes = event.detail;
- if(nodes[0] && nodes[0].asset){
- this.activeAsset = nodes[0].asset;
+ // Need to fully load the asset
+ if (!manager.events) {
+ return;
+ }
+
+ const selectedNode = event.detail && event.detail.length > 0 ? event.detail[0] : undefined;
+
+ if (!selectedNode) {
+ this.activeAsset = undefined;
+ } else {
+ // fully load the asset
+ manager.events.sendEventWithReply({
+ event: {
+ eventType: "read-asset",
+ assetId: selectedNode.asset!.id
+ } as ReadAssetEvent
+ }).then((ev) => {
+ this.activeAsset = (ev as AssetEvent).asset;
+ }).catch(() => this.activeAsset = undefined);
}
}
connectedCallback() {
super.connectedCallback();
this._style = window.getComputedStyle(this);
+ this.addEventListener(OrAssetTreeSelectionChangedEvent.NAME, this._onTreeSelectionChanged);
}
disconnectedCallback(): void {
super.disconnectedCallback();
this._cleanup();
+ this.removeEventListener(OrAssetTreeSelectionChangedEvent.NAME, this._onTreeSelectionChanged);
}
- shouldUpdate(_changedProperties: PropertyValues): boolean {
- return super.shouldUpdate(_changedProperties);
- }
-
render() {
-
const disabled = this._loading;
const endDateInputType = this.getInputType();
return html`
@@ -523,7 +530,7 @@ export class OrChart extends translate(i18next)(LitElement) {
<div class="mdc-dialog__surface">
<h2 class="mdc-dialog__title" id="my-dialog-title">${i18next.t("addAttribute")}</h2>
<div class="dialog-container mdc-dialog__content" id="my-dialog-content">
- <or-asset-tree id="chart-asset-tree" .selectedIds="${this.activeAsset ? [this.activeAsset.id] : null}]"></or-asset-tree>
+ <or-asset-tree id="chart-asset-tree" .selectedIds="${this.activeAsset ? [this.activeAsset.id] : undefined}"></or-asset-tree>
${this.activeAsset && this.activeAsset.attributes ? html`
<or-input id="chart-attribute-picker"
style="display:flex;"
@@ -822,6 +829,9 @@ export class OrChart extends translate(i18next)(LitElement) {
this._chart.destroy();
this._chart = undefined;
}
+ if (this._dialog) {
+ this._dialog.destroy();
+ }
}
protected _deleteAttribute (index:number) {
diff --git a/ui/component/or-mwc-components/src/or-mwc-dialog.ts b/ui/component/or-mwc-components/src/or-mwc-dialog.ts
index 1895e6219..248b52e83 100644
--- a/ui/component/or-mwc-components/src/or-mwc-dialog.ts
+++ b/ui/component/or-mwc-components/src/or-mwc-dialog.ts
@@ -125,6 +125,10 @@ export class OrMwcDialog extends LitElement {
protected _mdcComponent?: MDCDialog;
+ public get isOpen() {
+ return this._mdcComponent ? this._mdcComponent.isOpen : false;
+ }
+
public open() {
if (this._mdcElem && !this._mdcComponent) {
this._mdcComponent = new MDCDialog(this._mdcElem);