Skip to content

armgit / core

Public Imported from GitHub
0

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

Feature/filter asset tree (#576)

Jérôme Verviere33ed9a83604
diff --git a/manager/src/web/shared/locales/ar/or.json b/manager/src/web/shared/locales/ar/or.json
index 487a6213e..d61f09627 100644
--- a/manager/src/web/shared/locales/ar/or.json
+++ b/manager/src/web/shared/locales/ar/or.json
@@ -97,6 +97,7 @@
"showOnMap": "إظهار على الخريطة",
"noAssetSelected": "الرجاء إختيار مكون من اليسار",
"loading": "جاري التحميل",
+ "noAssetsFound": "No assets found",
"location": "الموقع",
"hour": "الساعة",
"day": "اليوم",
diff --git a/manager/src/web/shared/locales/cn/or.json b/manager/src/web/shared/locales/cn/or.json
index 24bd646e1..143f2456a 100644
--- a/manager/src/web/shared/locales/cn/or.json
+++ b/manager/src/web/shared/locales/cn/or.json
@@ -97,6 +97,7 @@
"showOnMap": "在地图显示",
"noAssetSelected": "请选择左列资源",
"loading": "加载中...",
+ "noAssetsFound": "No assets found",
"location": "位置",
"hour": "时",
"day": "日",
diff --git a/manager/src/web/shared/locales/de/or.json b/manager/src/web/shared/locales/de/or.json
index ab2b0fbd5..960d1a4b2 100644
--- a/manager/src/web/shared/locales/de/or.json
+++ b/manager/src/web/shared/locales/de/or.json
@@ -107,6 +107,7 @@
"noAssetSelected": "Bitte wählen Sie links ein Asset aus",
"noAssetTypeSelected": "Bitte wählen Sie links ein Asset Type aus",
"loading": "Wird geladen...",
+ "noAssetsFound": "No assets found",
"location": "Standort",
"hour": "Stunde",
"day": "Tag",
diff --git a/manager/src/web/shared/locales/en/or.json b/manager/src/web/shared/locales/en/or.json
index 1d0fe0dcd..38ed9b7f8 100644
--- a/manager/src/web/shared/locales/en/or.json
+++ b/manager/src/web/shared/locales/en/or.json
@@ -107,6 +107,8 @@
"noAssetSelected": "Please select an asset on the left",
"noAssetTypeSelected": "Please select an asset type on the left",
"loading": "Loading...",
+ "noAssetsFound": "No assets found",
+ "filterInvite": "Filter with at least 3 characters",
"location": "Location",
"hour": "Hour",
"day": "Day",
diff --git a/manager/src/web/shared/locales/es/or.json b/manager/src/web/shared/locales/es/or.json
index 95fe9d649..447779d95 100644
--- a/manager/src/web/shared/locales/es/or.json
+++ b/manager/src/web/shared/locales/es/or.json
@@ -97,6 +97,7 @@
"showOnMap": "Mostrar en el mapa",
"noAssetSelected": "Seleccione un activo a la izquierda",
"loading": "Cargando...",
+ "noAssetsFound": "No assets found",
"location": "Posición",
"hour": "Hora",
"day": "Día",
diff --git a/manager/src/web/shared/locales/fr/or.json b/manager/src/web/shared/locales/fr/or.json
index 08311527f..803dc44ec 100644
--- a/manager/src/web/shared/locales/fr/or.json
+++ b/manager/src/web/shared/locales/fr/or.json
@@ -97,6 +97,7 @@
"showOnMap": "Montrer sur la carte",
"noAssetSelected": "Veuillez sélectionner une ressource à gauche",
"loading": "Chargement...",
+ "noAssetsFound": "Aucune ressource trouvée",
"location": "Position",
"hour": "Heure",
"day": "Jour",
diff --git a/manager/src/web/shared/locales/it/or.json b/manager/src/web/shared/locales/it/or.json
index 202adefa1..00aa320da 100644
--- a/manager/src/web/shared/locales/it/or.json
+++ b/manager/src/web/shared/locales/it/or.json
@@ -97,6 +97,7 @@
"showOnMap": "Mostra sulla mappa",
"noAssetSelected": "Seleziona un asset sulla sinistra",
"loading": "In caricamento...",
+ "noAssetsFound": "No assets found",
"location": "Posizione",
"hour": "ora",
"day": "Giorno",
diff --git a/manager/src/web/shared/locales/nl/or.json b/manager/src/web/shared/locales/nl/or.json
index 29f962b1c..8db8c2a1b 100644
--- a/manager/src/web/shared/locales/nl/or.json
+++ b/manager/src/web/shared/locales/nl/or.json
@@ -107,6 +107,7 @@
"noAssetSelected": "Kies een asset uit de lijst aan de linkerkant",
"noAssetTypeSelected": "Kies een asset type uit de lijst aan de linkerkant",
"loading": "Aan het laden...",
+ "noAssetsFound": "Geen assets gevonden",
"location": "Locatie",
"hour": "Uur",
"day": "Dag",
diff --git a/manager/src/web/shared/locales/pt/or.json b/manager/src/web/shared/locales/pt/or.json
index 8135bcbb0..32efb795c 100644
--- a/manager/src/web/shared/locales/pt/or.json
+++ b/manager/src/web/shared/locales/pt/or.json
@@ -97,6 +97,7 @@
"showOnMap": "Mostrar no mapa",
"noAssetSelected": "Selecione um ativo à esquerda",
"loading": "Carregando ...",
+ "noAssetsFound": "No assets found",
"location": "Posição",
"hour": "Hora",
"day": "Dia",
diff --git a/profile/dev-ui.yml b/profile/dev-ui.yml
index 8948a31fb..1fcf9a8aa 100644
--- a/profile/dev-ui.yml
+++ b/profile/dev-ui.yml
@@ -49,8 +49,8 @@ services:
volumes:
- temp-data:/tmp
environment:
- SETUP_WIPE_CLEAN_INSTALL: true
+ SETUP_WIPE_CLEAN_INSTALL: "true"
EXTERNAL_URL: http://localhost:8080
- DEV_MODE: true
+ DEV_MODE: "true"
ports:
- "8080:8080"
diff --git a/ui/component/or-asset-tree/src/index.ts b/ui/component/or-asset-tree/src/index.ts
index 9540b063f..e49df26b5 100644
--- a/ui/component/or-asset-tree/src/index.ts
+++ b/ui/component/or-asset-tree/src/index.ts
@@ -1,6 +1,7 @@
import {html, LitElement, PropertyValues, TemplateResult} from "lit";
-import {customElement, property} from "lit/decorators.js";
+import {customElement, property, query, state} from "lit/decorators.js";
import "@openremote/or-mwc-components/or-mwc-input";
+import {InputType, OrInputChangedEvent, OrMwcInput} from "@openremote/or-mwc-components/or-mwc-input";
import "@openremote/or-icon";
import {
Asset,
@@ -8,6 +9,7 @@ import {
AssetEvent,
AssetEventCause,
AssetQuery,
+ AssetQueryMatch,
AssetsEvent,
AssetTreeNode,
AssetTypeInfo,
@@ -18,8 +20,7 @@ import {
} from "@openremote/model";
import "@openremote/or-translate";
import {style} from "./style";
-import manager, {AssetModelUtil, EventCallback, OREvent, subscribe, Util} from "@openremote/core";
-import {InputType, OrMwcInput} from "@openremote/or-mwc-components/or-mwc-input";
+import manager, {AssetModelUtil, EventCallback, subscribe, Util} from "@openremote/core";
import Qs from "qs";
import {getAssetDescriptorIconTemplate, OrIcon} from "@openremote/or-icon";
import "@openremote/or-mwc-components/or-mwc-menu";
@@ -70,6 +71,8 @@ export interface UiAssetTreeNode extends AssetTreeNode {
children: UiAssetTreeNode[];
someChildrenSelected: boolean;
allChildrenSelected: boolean;
+ notMatchingFilter: boolean;
+ hidden: boolean;
}
export interface NodeSelectEventDetail {
@@ -269,6 +272,13 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
protected _expandedNodes: UiAssetTreeNode[] = [];
protected _initCallback?: EventCallback;
+ protected _filterValue?: string;
+ protected _searchInputTimer?: number = undefined;
+ @query("#clearIcon")
+ protected _clearIcon!: HTMLElement;
+ @query("#filterInput")
+ protected _filterInput!: OrMwcInput;
+
public get selectedNodes(): UiAssetTreeNode[] {
return this._selectedNodes ? [...this._selectedNodes] : [];
}
@@ -306,7 +316,6 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
}
protected render() {
-
return html`
<div id="header">
<div id="title-container">
@@ -327,17 +336,46 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
(v) => this._onSortClicked(v as string))}
</div>
</div>
-
+
+ <div id="asset-tree-filter">
+ <or-mwc-input id="filterInput"
+ ?disabled="${this._loading}"
+ style="width: 100%;"
+ type="${ InputType.TEXT }"
+ icon="magnify"
+ compact="true"
+ outlined="true"
+ @input="${(e: KeyboardEvent) => {
+ // Means some input is occurring so delay filter
+ this._onFilterInput(e);
+ }}"
+ @or-mwc-input-changed="${ (e: OrInputChangedEvent) => {
+ // Means field has lost focus so do filter immediately
+ this._doFiltering((e.detail.value as string) || undefined);
+ }}">
+ </or-mwc-input>
+ <or-icon id="clearIcon" icon="close" @click="${() => {
+ // Wipe the current value and hide the clear button
+ this._filterInput.value = undefined;
+ this._clearIcon.classList.remove("visible");
+
+ // Call filtering
+ this._doFiltering(undefined);
+ }}"></or-icon>
+ </div>
+
${!this._nodes
? html`
<span id="loading"><or-translate value="loading"></or-translate></span>`
- : html`
+ : (this._nodes.length === 0
+ ? html `<span id="noAssetsFound"><or-translate value="noAssetsFound"></or-translate></span>`
+ : html`
<div id="list-container">
<ol id="list">
- ${this._nodes.map((treeNode) => this._treeNodeTemplate(treeNode, 0))}
+ ${this._nodes.map((treeNode) => this._treeNodeTemplate(treeNode, 0)).filter(t => !!t)}
</ol>
</div>
- `
+ `)
}
<div id="footer">
@@ -538,6 +576,121 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
this._onNodeClicked(null, null);
}
+ protected _onFilterInput(e: KeyboardEvent) {
+ // Use composed path to get event source within shadow DOM
+ let value: string | undefined;
+
+ if (e.composedPath()) {
+ value = ((e.composedPath()[0] as HTMLInputElement).value) || undefined;
+
+ if (value) {
+ this._clearIcon.classList.add("visible");
+ } else {
+ this._clearIcon.classList.remove("visible");
+ }
+ }
+
+ if (this._searchInputTimer) {
+ clearTimeout(this._searchInputTimer);
+ }
+
+ this._searchInputTimer = window.setTimeout(() => {
+ this._doFiltering(value);
+ }, 1500);
+ }
+
+ protected async _doFiltering(value?: string) {
+ // Clear timeout in case we got here from value change
+ if (this._searchInputTimer) {
+ clearTimeout(this._searchInputTimer);
+ this._searchInputTimer = undefined;
+ }
+
+ if (this.isConnected && this._nodes) {
+
+ if (this._filterValue === value) {
+ // This value has already been filtered so ignore or is not having the minimal number of characters
+ return;
+ }
+
+ this._filterValue = value;
+
+ if (!this._filterValue) {
+ // Clear the filter
+ OrAssetTree._forEachNodeRecursive(this._nodes!, (node) => {
+ node.notMatchingFilter = false;
+ node.hidden = false;
+ });
+ this.requestUpdate("_nodes");
+ return;
+ }
+
+ this.disabled = true;
+
+ // Use a matcher function - this can be altered independent of the filtering logic
+ // Maybe we should just filter in memory for basic matches like name
+ this.getMatcher(false).then((matcher: (asset: Asset) => boolean) => {
+ if (this._nodes) {
+ this._nodes.forEach((node: UiAssetTreeNode) => {
+ this.filterTreeNode(node, matcher);
+ });
+ this.disabled = false;
+ }
+ });
+ }
+ }
+
+ protected getMatcher(requireQuery: boolean): Promise<((asset: Asset) => boolean)> {
+ if (requireQuery) {
+ return this.getMatcherFromQuery();
+ } else {
+ return this.getSimpleNameMatcher();
+ }
+ }
+
+ protected async getSimpleNameMatcher(): Promise<((asset: Asset) => boolean)> {
+ return (asset) => {
+ return asset.name!.toLowerCase().includes(this._filterValue!.toLowerCase());
+ };
+ }
+
+ protected async getMatcherFromQuery(): Promise<((asset: Asset) => boolean)> {
+ const query: AssetQuery = {
+ select: {
+ excludePath: true,
+ excludeAttributes: true,
+ excludeParentInfo: true
+ },
+ names: [{
+ predicateType: "string",
+ match: AssetQueryMatch.CONTAINS,
+ value: this._filterValue,
+ caseSensitive: false
+ }]
+ };
+
+ const response = await manager.rest.api.AssetResource.queryAssets(query);
+ const foundAssetIds: string[] = response.data.map((asset: Asset) => asset.id!);
+
+ return (asset) => {
+ return foundAssetIds.includes(asset.id!);
+ };
+ }
+
+ protected filterTreeNode(currentNode: UiAssetTreeNode, matcher: (asset: Asset) => boolean): boolean {
+ let nodeOrDescendantMatches = matcher(currentNode.asset!);
+ currentNode.notMatchingFilter = !nodeOrDescendantMatches;
+
+ const childOrDescendantMatches = currentNode.children.map((childNode) => {
+ return this.filterTreeNode(childNode, matcher);
+ });
+
+ nodeOrDescendantMatches = nodeOrDescendantMatches || childOrDescendantMatches.some(m => m);
+ currentNode.expanded = nodeOrDescendantMatches && currentNode.children.length > 0;
+ currentNode.hidden = !nodeOrDescendantMatches;
+ return nodeOrDescendantMatches;
+ }
+
protected async _onCopyClicked() {
if (this._selectedNodes.length !== 1) {
return;
@@ -900,7 +1053,6 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
}
protected _buildTreeNodes(assets: Asset[], sortFunction: (a: UiAssetTreeNode, b: UiAssetTreeNode) => number) {
-
if (!assets || assets.length === 0) {
this._nodes = [];
} else {
@@ -990,7 +1142,7 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
});
}
- protected _treeNodeTemplate(treeNode: UiAssetTreeNode, level: number): TemplateResult | string {
+ protected _treeNodeTemplate(treeNode: UiAssetTreeNode, level: number): TemplateResult | string | undefined {
const descriptor = AssetModelUtil.getAssetDescriptor(treeNode.asset!.type!);
@@ -999,17 +1151,27 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
parentCheckboxIcon = 'checkbox-multiple-marked';
} else if (treeNode.someChildrenSelected) {
parentCheckboxIcon = 'checkbox-multiple-marked-outline';
- }else {
+ } else {
parentCheckboxIcon = 'checkbox-multiple-blank-outline';
}
+ if (treeNode.hidden) {
+ return html``;
+ }
+
+ let filterColorForNonMatchingAsset: boolean = false;
+
+ if (treeNode.asset && treeNode.notMatchingFilter) {
+ filterColorForNonMatchingAsset = true;
+ }
+
return html`
<li ?data-selected="${treeNode.selected}" ?data-expanded="${treeNode.expanded}" @click="${(evt: MouseEvent) => this._onNodeClicked(evt, treeNode)}">
<div class="node-container" style="padding-left: ${level * 22}px">
<div class="node-name">
<div class="expander" ?data-expandable="${treeNode.expandable}"></div>
- ${getAssetDescriptorIconTemplate(descriptor)}
- <span>${treeNode.asset!.name}</span>
+ ${getAssetDescriptorIconTemplate(descriptor, undefined, undefined, (filterColorForNonMatchingAsset ? '#d3d3d3;' : undefined))}
+ <span style="color: ${filterColorForNonMatchingAsset ? '#d3d3d3;' : ''}">${treeNode.asset!.name}</span>
${this.checkboxes ? html`
<span class="mdc-list-item__graphic">
${treeNode.expandable
@@ -1025,7 +1187,7 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
</div>
</div>
<ol>
- ${!treeNode.children ? `` : treeNode.children.map((childNode) => this._treeNodeTemplate(childNode, level + 1))}
+ ${!treeNode.children ? `` : treeNode.children.map((childNode) => this._treeNodeTemplate(childNode, level + 1)).filter(t => !!t)}
</ol>
</li>
`;
diff --git a/ui/component/or-asset-tree/src/style.ts b/ui/component/or-asset-tree/src/style.ts
index 077d4c8f2..841144f83 100644
--- a/ui/component/or-asset-tree/src/style.ts
+++ b/ui/component/or-asset-tree/src/style.ts
@@ -217,5 +217,41 @@ export const style = css`
.mdc-checkbox or-icon[icon="checkbox-multiple-marked-outline"] {
color: var(--internal-or-asset-tree-selected-color);
}
+
+ #asset-tree-filter {
+ display: flex;
+ align-items: center;
+ position: relative;
+ }
+
+ #filterInput {
+ padding: 5px;
+ }
+
+ #clearIcon {
+ width: 16px;
+ height: 16px;
+ position: absolute;
+ right: 0;
+ padding-right: 14px;
+ cursor: pointer;
+ display: none;
+ }
+
+ #clearIcon.visible {
+ display: block;
+ }
+ #noAssetsFound {
+ flex: 1 0 auto;
+ display: inline-flex;
+ align-items: center;
+ text-align: center;
+ margin: 0 auto;
+ font-size: 14px;
+ }
+
+ .filterMatching {
+ color: #808080;
+ }
`;
diff --git a/ui/component/or-mwc-components/src/or-mwc-input.ts b/ui/component/or-mwc-components/src/or-mwc-input.ts
index 3a4dea1f3..408f44baf 100644
--- a/ui/component/or-mwc-components/src/or-mwc-input.ts
+++ b/ui/component/or-mwc-components/src/or-mwc-input.ts
@@ -1263,7 +1263,7 @@ export class OrMwcInput extends LitElement {
if ((e.code === "Enter" || e.code === "NumpadEnter")) {
this.onValueChange((e.target as HTMLInputElement), (e.target as HTMLInputElement).value, true);
}}}"
- @blur="${(e: Event) => {if ((e.target as HTMLInputElement).value === "") this.reportValidity()}}"
+ @blur="${(e: Event) => {if ((e.target as HTMLInputElement).value === "") this.reportValidity()}}"
@change="${(e: Event) => this.onValueChange((e.target as HTMLInputElement), (e.target as HTMLInputElement).value)}" />`;
inputElem = html`