Skip to content

armgit / core

Public Imported from GitHub
0

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

UI Bugfix; Insights navigation causing the browser tab to hang (#1338)

Martin Peeterse5bb69762c87
diff --git a/ui/component/core/src/util.ts b/ui/component/core/src/util.ts
index 16cb942de..ba716f39a 100644
--- a/ui/component/core/src/util.ts
+++ b/ui/component/core/src/util.ts
@@ -29,6 +29,7 @@ import Qs from "qs";
import {AssetModelUtil} from "@openremote/model";
import moment from "moment";
import DateTimeFormatOptions = Intl.DateTimeFormatOptions;
+import {transform} from "lodash";
export class Deferred<T> {
@@ -274,6 +275,20 @@ export function objectsEqual(obj1?: any, obj2?: any, deep: boolean = true): bool
return false;
}
+/**
+ * Deep diff between two object, using lodash
+ * @param {Object} object Object compared
+ * @param {Object} base Object to compare with
+ * @return {Object} Return a new object who represent the diff
+ */
+export function difference(object?: any, base?: any): any {
+ const changes = (object: any, base: any) => transform(object, function(result: any, value, key: string | number | symbol) {
+ if (!objectsEqual(value, base?.[key])) {
+ result[key] = (isObject(value) && isObject(base?.[key])) ? changes(value, base?.[key]) : value;
+ }
+ });
+ return changes(object, base);
+}
export function arrayRemove<T>(arr: T[], item: T) {
if (arr.length === 0) {
diff --git a/ui/component/or-dashboard-builder/src/index.ts b/ui/component/or-dashboard-builder/src/index.ts
index f307a6c15..d7a7c14fb 100644
--- a/ui/component/or-dashboard-builder/src/index.ts
+++ b/ui/component/or-dashboard-builder/src/index.ts
@@ -329,9 +329,28 @@ export class OrDashboardBuilder extends LitElement {
// On any update (except widget selection), check whether hasChanged should be updated.
if(!(changedProps.size === 1 && changedProps.has('selectedWidget'))) {
- const dashboardEqual = Util.objectsEqual(this.selectedDashboard, this.initialDashboardJSON ? JSON.parse(this.initialDashboardJSON) : undefined);
- const templateEqual = Util.objectsEqual(this.currentTemplate, this.initialTemplateJSON ? JSON.parse(this.initialTemplateJSON) : undefined);
- this.hasChanged = (!dashboardEqual || !templateEqual);
+
+ // Check for dashboard changes
+ const initialDashboard = this.initialDashboardJSON ? JSON.parse(this.initialDashboardJSON) : undefined;
+ const dashboardDiff = Util.difference(this.selectedDashboard, initialDashboard);
+ if(Object.keys(dashboardDiff).length > 0) {
+ this.hasChanged = true;
+ console.debug("Dashboard has detected changes! Diff:", dashboardDiff);
+ }
+
+ // Check for template changes
+ else {
+ const initialTemplate = this.initialTemplateJSON ? JSON.parse(this.initialTemplateJSON) : undefined;
+ const templateDiff = Util.difference(this.currentTemplate, initialTemplate);
+ if(Object.keys(templateDiff).length > 0) {
+ this.hasChanged = true;
+ console.debug("Template has detected changes! Diff:", templateDiff);
+ } else {
+
+ // If no changes detected between dashboard nor template
+ this.hasChanged = false;
+ }
+ }
}
// Support for realm switching
diff --git a/ui/component/or-dashboard-builder/src/or-dashboard-widgetcontainer.ts b/ui/component/or-dashboard-builder/src/or-dashboard-widgetcontainer.ts
index 9f3439fe3..077396d71 100644
--- a/ui/component/or-dashboard-builder/src/or-dashboard-widgetcontainer.ts
+++ b/ui/component/or-dashboard-builder/src/or-dashboard-widgetcontainer.ts
@@ -1,4 +1,3 @@
-import {i18next} from "@openremote/or-translate";
import {html, LitElement, PropertyValues} from "lit";
import {customElement, property, query, state} from "lit/decorators.js";
import {when} from "lit/directives/when.js";
@@ -8,6 +7,7 @@ import {DashboardWidget} from "@openremote/model";
import {OrWidget, WidgetManifest} from "./util/or-widget";
import {WidgetService} from "./service/widget-service";
import {WidgetConfig} from "./util/widget-config";
+import {Util} from "@openremote/core";
/* ------------------------------------ */
@@ -21,6 +21,9 @@ export class OrDashboardWidgetContainer extends LitElement {
@property()
protected readonly widget!: DashboardWidget;
+ @property() // Optional property, that overrides widget.widgetConfig. Useful for view-only mode.
+ protected widgetConfig?: WidgetConfig;
+
@property()
protected readonly editMode!: boolean;
@@ -49,7 +52,7 @@ export class OrDashboardWidgetContainer extends LitElement {
this.resizeObserver?.disconnect();
}
- shouldUpdate(changedProps: Map<PropertyKey, unknown>): boolean {
+ shouldUpdate(changedProps: PropertyValues): boolean {
const changed = changedProps;
// Update config if some values in the spec are not set.
@@ -57,7 +60,11 @@ export class OrDashboardWidgetContainer extends LitElement {
if (this.widget) {
const manifest = WidgetService.getManifest(this.widget.widgetTypeId!);
if (manifest) {
- this.widget.widgetConfig = WidgetService.correctToConfigSpec(manifest, this.widget.widgetConfig);
+ if(this.editMode) {
+ this.widget.widgetConfig = WidgetService.correctToConfigSpec(manifest, this.widget.widgetConfig);
+ } else {
+ this.widgetConfig = WidgetService.correctToConfigSpec(manifest, this.widget.widgetConfig);
+ }
}
}
@@ -67,7 +74,7 @@ export class OrDashboardWidgetContainer extends LitElement {
const oldVal = changedProps.get('widget') as DashboardWidget | undefined;
const idChanged = oldVal?.id !== this.widget?.id;
const nameChanged = oldVal?.displayName !== this.widget?.displayName;
- const configChanged = JSON.stringify(oldVal?.widgetConfig) !== JSON.stringify(this.widget?.widgetConfig);
+ const configChanged = !Util.objectsEqual(oldVal?.widgetConfig, this.getWidgetConfig());
if (!(idChanged || nameChanged || configChanged)) {
changed.delete('widget');
}
@@ -76,7 +83,7 @@ export class OrDashboardWidgetContainer extends LitElement {
return (changed.size === 0 ? false : super.shouldUpdate(changedProps));
}
- willUpdate(changedProps: Map<string, any>) {
+ willUpdate(changedProps: PropertyValues) {
super.willUpdate(changedProps);
if (!this.manifest && this.widget) {
@@ -85,7 +92,7 @@ export class OrDashboardWidgetContainer extends LitElement {
// Create widget
if (changedProps.has("widget") && this.widget) {
- this.initializeWidgetElem(this.manifest!, this.widget.widgetConfig);
+ this.initializeWidgetElem(this.manifest!, this.getWidgetConfig()!);
}
}
@@ -152,4 +159,8 @@ export class OrDashboardWidgetContainer extends LitElement {
public refreshContent(force: boolean) {
this.orWidget?.refreshContent(force);
}
+
+ public getWidgetConfig(): WidgetConfig | undefined {
+ return this.widgetConfig || this.widget?.widgetConfig;
+ }
}
diff --git a/ui/component/or-dashboard-builder/src/service/widget-service.ts b/ui/component/or-dashboard-builder/src/service/widget-service.ts
index 03af1f2df..75df65385 100644
--- a/ui/component/or-dashboard-builder/src/service/widget-service.ts
+++ b/ui/component/or-dashboard-builder/src/service/widget-service.ts
@@ -42,7 +42,13 @@ export class WidgetService {
// Method used to correct the OrWidgetConfig specification
// So, if certain fields are removed or invalid, it will be corrected by merging the object with the default OrWidgetConfig.
+ // Will only return a different object if it actually changes. This prevents child objects being returned in a different order.
public static correctToConfigSpec(manifest: WidgetManifest, widgetConfig: WidgetConfig) {
- return Util.mergeObjects(manifest.getDefaultConfig(), widgetConfig, false) as WidgetConfig;
+ const newConfig = Util.mergeObjects(manifest.getDefaultConfig(), widgetConfig, false) as WidgetConfig;
+ if(Util.objectsEqual(newConfig, widgetConfig)) {
+ return widgetConfig;
+ } else {
+ return newConfig;
+ }
}
}
diff --git a/ui/component/or-dashboard-builder/src/widgets/gateway-widget.ts b/ui/component/or-dashboard-builder/src/widgets/gateway-widget.ts
index d5092e56c..91f816ad0 100644
--- a/ui/component/or-dashboard-builder/src/widgets/gateway-widget.ts
+++ b/ui/component/or-dashboard-builder/src/widgets/gateway-widget.ts
@@ -111,6 +111,8 @@ export class GatewayWidget extends OrWidget {
console.log("Existing tunnel found!", info);
this._setActiveTunnel(info, true)
}
+ }).catch(e => {
+ console.error(e);
}).finally(() => {
this._loading = false;
});