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: Dashboard table widget added support for 'all of this type' (#1539)
Wouter Voorbergadcaa719a51b
diff --git a/manager/src/main/resources/org/openremote/manager/setup/database/V20250217_01__KeepExistingWidgetConfig.sql b/manager/src/main/resources/org/openremote/manager/setup/database/V20250217_01__KeepExistingWidgetConfig.sqlnew file mode 100644index 000000000..52c56ebcf--- /dev/null+++ b/manager/src/main/resources/org/openremote/manager/setup/database/V20250217_01__KeepExistingWidgetConfig.sql@@ -0,0 +1,32 @@+/*+* Set existing widgets configs to match their pre-update behavior.+*/++WITH json_data AS (+ SELECT+ id,+ template,+ jsonb_set(+ template,+ '{widgets}',+ (+ SELECT jsonb_agg(+ jsonb_set(+ widget,+ '{widgetConfig,allOfType}',+ CASE+ WHEN (widget ->> 'widgetTypeId') = 'map' THEN 'true'::jsonb+ WHEN (widget ->> 'widgetTypeId') = 'table' THEN 'false'::jsonb+ ELSE widget -> 'widgetConfig' -> 'allOfType'+ END+ )+ )+ FROM jsonb_array_elements(template -> 'widgets') AS widget+ )+ ) AS updated_json+ FROM dashboard+)+UPDATE dashboard+SET template = json_data.updated_json+FROM json_data+WHERE dashboard.id = json_data.id;\ No newline at end of filediff --git a/ui/app/shared/locales/en/or.json b/ui/app/shared/locales/en/or.jsonindex a959a1784..e3f1dadf5 100644--- a/ui/app/shared/locales/en/or.json+++ b/ui/app/shared/locales/en/or.json@@ -7,6 +7,7 @@"asset": "Asset","asset_plural": "Assets","assets": "Assets",+ "allAssetsofType": "All assets of this type","attribute": "Attribute","attributes": "Attributes","attribute_plural": "Attributes",diff --git a/ui/component/or-dashboard-builder/src/panels/assettypes-panel.ts b/ui/component/or-dashboard-builder/src/panels/assettypes-panel.tsindex a19754106..18b59430a 100644--- a/ui/component/or-dashboard-builder/src/panels/assettypes-panel.ts+++ b/ui/component/or-dashboard-builder/src/panels/assettypes-panel.ts@@ -24,6 +24,20 @@ export class AssetTypeSelectEvent extends CustomEvent<string> {}}+export class AssetAllOfTypeSwitchEvent extends CustomEvent<boolean> {++ public static readonly NAME = "alloftype-switch";++ constructor (allOfType: boolean) {+ super(AssetAllOfTypeSwitchEvent.NAME, {+ bubbles: true,+ composed: true,+ detail: allOfType+ });+ }++}+export class AssetIdsSelectEvent extends CustomEvent<string | string[]> {public static readonly NAME = "assetids-select"@@ -53,7 +67,8 @@ export class AttributeNamesSelectEvent extends CustomEvent<string | string[]> {export interface AssetTypesFilterConfig {assets?: {enabled?: boolean,- multi?: boolean+ multi?: boolean,+ allOfTypeOption?: boolean},attributes?: {enabled?: boolean,@@ -83,6 +98,9 @@ export class AssettypesPanel extends LitElement {}}+ @property() // Whether to include all assets of this type or if the user can choose specific assets+ protected allOfType: boolean = false;+@property() // IDs of assets; either undefined, a single entry, or multi selectprotected assetIds: undefined | string | string[];@@ -108,6 +126,9 @@ export class AssettypesPanel extends LitElement {this._attributeSelectList = this.getAttributesByType(this.assetType)!;this.dispatchEvent(new AssetTypeSelectEvent(this.assetType));}+ if (changedProps.has("allOfType")) {+ this.dispatchEvent(new AssetAllOfTypeSwitchEvent(this.allOfType));+ }if (changedProps.has("assetIds") && this.assetIds) {this.dispatchEvent(new AssetIdsSelectEvent(this.assetIds));}@@ -141,12 +162,26 @@ export class AssettypesPanel extends LitElement {}</div>+ <!-- Switch to include all assets of this type -->+ ${when(this.config.assets?.allOfTypeOption, () => {+ return html`+ <div style="display: flex; justify-content: space-between; align-items: center;">+ <span><or-translate value="allAssetsofType"></or-translate></span>+ <or-mwc-input .type="${InputType.SWITCH}" style="width: 70px;"+ .value="${this.allOfType}"+ .disabled="${!this.assetType}"+ @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this.onAssetAllOfTypeToggle(ev)}"+ ></or-mwc-input>+ </div>+ `;+ })}+<!-- Select one or more assets -->${when(this.config.assets?.enabled, () => {const assetIds = (typeof this.assetIds === 'string') ? [this.assetIds] : this.assetIds;return html`<div>- <or-mwc-input .type="${InputType.BUTTON}" .label="${(this.assetIds?.length || 0) + ' ' + i18next.t('assets')}" .disabled="${!this.assetType}" fullWidth outlined comfortable style="width: 100%;"+ <or-mwc-input .type="${InputType.BUTTON}" .label="${(this.assetIds?.length || 0) + ' ' + i18next.t('assets')}" .disabled="${!this.assetType || this.allOfType}" fullWidth outlined comfortable style="width: 100%;"@or-mwc-input-changed="${(ev: OrInputChangedEvent) => this._openAssetSelector(this.assetType!, assetIds, this.config.assets?.multi)}"></or-mwc-input></div>@@ -245,6 +280,10 @@ export class AssettypesPanel extends LitElement {}}+ protected onAssetAllOfTypeToggle(ev: OrInputChangedEvent) {+ this.allOfType = ev.detail.value;+ }+protected _openAssetSelector(assetType: string, assetIds?: string[], multi = false) {const assetTreeRef: Ref<OrAssetTree> = createRef();const config = {diff --git a/ui/component/or-dashboard-builder/src/panels/thresholds-panel.ts b/ui/component/or-dashboard-builder/src/panels/thresholds-panel.tsindex dee82bd53..a346c493b 100644--- a/ui/component/or-dashboard-builder/src/panels/thresholds-panel.ts+++ b/ui/component/or-dashboard-builder/src/panels/thresholds-panel.ts@@ -133,7 +133,7 @@ export class ThresholdsPanel extends LitElement {<div id="thresholds-list" class="expanded-panel"><!-- Thresholds by number -->- ${(this.valueType === 'number' || this.valueType === 'positiveInteger'+ ${(this.valueType === 'number' || this.valueType === 'integer' || this.valueType === 'positiveInteger'|| this.valueType === 'positiveNumber' || this.valueType === 'negativeInteger'|| this.valueType === 'negativeNumber') ? html`${(this.thresholds as [number, string][]).sort((x, y) => (x[0] < y[0]) ? -1 : 1).map((threshold, index) => {diff --git a/ui/component/or-dashboard-builder/src/settings/map-settings.ts b/ui/component/or-dashboard-builder/src/settings/map-settings.tsindex 58a2cd171..69fb66109 100644--- a/ui/component/or-dashboard-builder/src/settings/map-settings.ts+++ b/ui/component/or-dashboard-builder/src/settings/map-settings.ts@@ -9,7 +9,7 @@ import "../panels/assettypes-panel";import "../panels/thresholds-panel";import {LngLat} from "maplibre-gl"; // TODO: Replace this importimport {when} from "lit/directives/when.js";-import {AssetTypeSelectEvent, AssetTypesFilterConfig, AttributeNamesSelectEvent} from "../panels/assettypes-panel";+import {AssetIdsSelectEvent, AssetTypeSelectEvent, AssetAllOfTypeSwitchEvent, AssetTypesFilterConfig, AttributeNamesSelectEvent} from "../panels/assettypes-panel";import manager from "@openremote/core";import {showSnackbar} from "@openremote/or-mwc-components/or-mwc-snackbar";import {BoolColorsChangeEvent, ThresholdChangeEvent} from "../panels/thresholds-panel";@@ -32,8 +32,13 @@ export class MapSettings extends AssetWidgetSettings {}protected render(): TemplateResult {- const allowedValueTypes = ["boolean", "number", "positiveInteger", "positiveNumber", "negativeInteger", "negativeNumber", "text"];+ const allowedValueTypes = ["boolean", "number", "integer", "positiveInteger", "positiveNumber", "negativeInteger", "negativeNumber", "text"];const config = {+ assets: {+ enabled: true,+ multi: true,+ allOfTypeOption: true+ },attributes: {enabled: true,valueTypes: allowedValueTypes@@ -71,7 +76,10 @@ export class MapSettings extends AssetWidgetSettings {<!-- Panel where Asset type and the selected attribute can be customized --><settings-panel displayName="attributes" expanded="${true}"><assettypes-panel .assetType="${this.widgetConfig.assetType}" .attributeNames="${this.widgetConfig.attributeName}" .config="${config}"+ .allOfType="${this.widgetConfig.allOfType}" .assetIds="${this.widgetConfig.assetIds}"@assettype-select="${(ev: AssetTypeSelectEvent) => this.onAssetTypeSelect(ev)}"+ @alloftype-switch="${(ev: AssetAllOfTypeSwitchEvent) => this.onAssetAllOfTypeSwitch(ev)}"+ @assetids-select="${(ev: AssetIdsSelectEvent) => this.onAssetIdsSelect(ev)}"@attributenames-select="${(ev: AttributeNamesSelectEvent) => this.onAttributeNameSelect(ev)}"></assettypes-panel>@@ -148,27 +156,50 @@ export class MapSettings extends AssetWidgetSettings {}}+ protected onAssetAllOfTypeSwitch(ev: AssetAllOfTypeSwitchEvent) {+ this.widgetConfig.allOfType = ev.detail as boolean;+ this.notifyConfigUpdate();+ }++ protected onAssetIdsSelect(ev: AssetIdsSelectEvent) {+ this.widgetConfig.assetIds = ev.detail as string[];+ this.notifyConfigUpdate();+ }+protected async onAttributeNameSelect(ev: AttributeNamesSelectEvent) {const attrName = ev.detail as string;this.widgetConfig.attributeName = attrName;- await manager.rest.api.AssetResource.queryAssets({- realm: {- name: manager.displayRealm- },- select: {- attributes: [attrName, 'location']- },- types: [this.widgetConfig.assetType!],- }).then(response => {- this.widgetConfig.assetIds = response.data.map((a) => a.id!);- this.widgetConfig.valueType = (response.data.length > 0) ? response.data[0].attributes![attrName].type : "text"; // sometimes no asset exists of that assetType, so using 'text' as fallback.- }).catch((reason) => {- console.error(reason);- showSnackbar(undefined, "errorOccurred");- });-- this.notifyConfigUpdate()- }++ const queryAssets = async (ids?: string[]) => {+ try {+ const response = await manager.rest.api.AssetResource.queryAssets({+ realm: { name: manager.displayRealm },+ select: { attributes: [attrName, 'location'] },+ types: [this.widgetConfig.assetType!],+ ids: ids+ });+ this.widgetConfig.assetIds = response.data.map((a) => a.id!);+ this.widgetConfig.valueType = response.data.length ? response.data[0].attributes![attrName].type : "text";+ if (!response.data[0].attributes![attrName].type) {+ throw new TypeError("Data does not contain property 'attributes' or 'type'.")}+ } catch (reason) {+ console.error(reason);+ if (reason instanceof TypeError) {+ showSnackbar(undefined, "noAttributesToShow");+ } else {+ showSnackbar(undefined, "errorOccurred");+ }+ }+ };++ if (this.widgetConfig.allOfType) {+ await queryAssets();+ } else {+ await queryAssets(this.widgetConfig.assetIds!);+ }++ this.notifyConfigUpdate();+ }protected onShowLabelsToggle(ev: OrInputChangedEvent) {this.widgetConfig.showLabels = ev.detail.value;diff --git a/ui/component/or-dashboard-builder/src/settings/table-settings.ts b/ui/component/or-dashboard-builder/src/settings/table-settings.tsindex f275ccdfc..7a4de1287 100644--- a/ui/component/or-dashboard-builder/src/settings/table-settings.ts+++ b/ui/component/or-dashboard-builder/src/settings/table-settings.ts@@ -3,7 +3,7 @@ import { customElement } from "lit/decorators.js";import {AssetWidgetSettings} from "../util/or-asset-widget";import {TableWidgetConfig} from "../widgets/table-widget";import { InputType, OrInputChangedEvent } from "@openremote/or-mwc-components/or-mwc-input";-import {AssetIdsSelectEvent, AssetTypeSelectEvent, AssetTypesFilterConfig, AttributeNamesSelectEvent} from "../panels/assettypes-panel";+import {AssetIdsSelectEvent, AssetTypeSelectEvent, AssetAllOfTypeSwitchEvent, AssetTypesFilterConfig, AttributeNamesSelectEvent} from "../panels/assettypes-panel";const styling = css`.customMwcInputContainer {@@ -26,7 +26,8 @@ export class TableSettings extends AssetWidgetSettings {const config = {assets: {enabled: true,- multi: true+ multi: true,+ allOfTypeOption: true},attributes: {enabled: true,@@ -40,7 +41,9 @@ export class TableSettings extends AssetWidgetSettings {<div style="padding-bottom: 12px;"><assettypes-panel .assetType="${this.widgetConfig.assetType}" .config="${config}".assetIds="${this.widgetConfig.assetIds}" .attributeNames="${this.widgetConfig.attributeNames}"+ .allOfType="${this.widgetConfig.allOfType}"@assettype-select="${(ev: AssetTypeSelectEvent) => this.onAssetTypeSelect(ev)}"+ @alloftype-switch="${(ev: AssetAllOfTypeSwitchEvent) => this.onAssetAllOfTypeSwitch(ev)}"@assetids-select="${(ev: AssetIdsSelectEvent) => this.onAssetIdsSelect(ev)}"@attributenames-select="${(ev: AttributeNamesSelectEvent) => this.onAttributesSelect(ev)}"></assettypes-panel>@@ -69,6 +72,11 @@ export class TableSettings extends AssetWidgetSettings {this.notifyConfigUpdate();}+ protected onAssetAllOfTypeSwitch(ev: AssetAllOfTypeSwitchEvent) {+ this.widgetConfig.allOfType = ev.detail as boolean;+ this.notifyConfigUpdate();+ }+protected onAssetIdsSelect(ev: AssetIdsSelectEvent) {this.widgetConfig.assetIds = ev.detail as string[];this.notifyConfigUpdate();diff --git a/ui/component/or-dashboard-builder/src/widgets/map-widget.ts b/ui/component/or-dashboard-builder/src/widgets/map-widget.tsindex 2f62b749d..572734371 100644--- a/ui/component/or-dashboard-builder/src/widgets/map-widget.ts+++ b/ui/component/or-dashboard-builder/src/widgets/map-widget.ts@@ -38,6 +38,7 @@ export interface MapWidgetConfig extends WidgetConfig {max?: number,// Asset type related valuesassetType?: string,+ allOfType?: boolean,valueType?: string,attributeName?: string,assetTypes: AssetDescriptor[],@@ -56,6 +57,7 @@ function getDefaultWidgetConfig(): MapWidgetConfig {thresholds: [[0, "#4caf50"], [75, "#ff9800"], [90, "#ef5350"]],assetTypes: [],assetType: undefined,+ allOfType: true,assetIds: [],attributes: [],} as MapWidgetConfig;@@ -97,13 +99,39 @@ export class MapWidget extends OrAssetWidget {}protected async loadAssets() {- if(this.widgetConfig.assetType && this.widgetConfig.attributeName) {+ if(this.widgetConfig.assetType && this.widgetConfig.attributeName && this.widgetConfig.allOfType) {this.fetchAssetsByType([this.widgetConfig.assetType], this.widgetConfig.attributeName).then((assets) => {this.loadedAssets = assets;});- }+ } else if(this.widgetConfig.assetType && this.widgetConfig.attributeName && !this.widgetConfig.allOfType) {+ this.fetchAssetsById([this.widgetConfig.assetType], this.widgetConfig.attributeName, this.widgetConfig.assetIds).then((assets) => {+ this.loadedAssets = assets;+ });+ }}+ protected async fetchAssetsById(assetTypes: string[], attributeName: string, assetIds: string[]) {+ let assets: Asset[] = [];+ await manager.rest.api.AssetResource.queryAssets({+ realm: {+ name:manager.displayRealm+ },+ select: {+ attributes: [attributeName, 'location']+ },+ types: assetTypes,+ ids: assetIds,++ }).then(response => {+ assets = response.data;+ this.markers = {};+ }).catch((reason) => {+ console.error(reason);+ showSnackbar(undefined, "errorOccurred");+ });+ return assets;+ }+protected async fetchAssetsByType(assetTypes: string[], attributeName: string) {let assets: Asset[] = [];await manager.rest.api.AssetResource.queryAssets({diff --git a/ui/component/or-dashboard-builder/src/widgets/table-widget.ts b/ui/component/or-dashboard-builder/src/widgets/table-widget.tsindex 585104d72..8c581d1c1 100644--- a/ui/component/or-dashboard-builder/src/widgets/table-widget.ts+++ b/ui/component/or-dashboard-builder/src/widgets/table-widget.ts@@ -13,6 +13,7 @@ import "@openremote/or-mwc-components/or-mwc-table";export interface TableWidgetConfig extends WidgetConfig {assetType?: string+ allOfType?: boolean,assetIds: string[]attributeNames: string[],tableSize: number,@@ -22,6 +23,7 @@ export interface TableWidgetConfig extends WidgetConfig {function getDefaultConfig(): TableWidgetConfig {return {assetType: undefined,+ allOfType: true,assetIds: [],attributeNames: [],tableSize: 10,@@ -80,16 +82,25 @@ export class TableWidget extends OrAssetWidget {/* --------------------------------------- */protected loadAssets() {- if(this.widgetConfig.assetIds.find(id => !this.isAssetLoaded(id))) {- this.queryAssets({- ids: this.widgetConfig.assetIds,- select: {- attributes: this.widgetConfig.attributeNames- }- }).then((assets) => {- this.loadedAssets = assets;- })- }+ if(this.widgetConfig.allOfType) {+ this.queryAssets({+ types: [this.widgetConfig.assetType],+ select: {+ attributes: this.widgetConfig.attributeNames+ }+ }).then((assets) => {+ this.loadedAssets = assets;+ })+ } else if(this.widgetConfig.assetIds.find(id => !this.isAssetLoaded(id))) {+ this.queryAssets({+ ids: this.widgetConfig.assetIds,+ select: {+ attributes: this.widgetConfig.attributeNames+ }+ }).then((assets) => {+ this.loadedAssets = assets;+ })+ }}protected getColumns(attributeNames: string[]): TableColumn[] {