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
Update rules tree to display newly added rules inside group (#1938)
Pawan Bhattarai8a01834f9fbb
diff --git a/ui/component/or-rules/src/index.ts b/ui/component/or-rules/src/index.tsindex 799e1cc77..bb7b74a73 100644--- a/ui/component/or-rules/src/index.ts+++ b/ui/component/or-rules/src/index.ts@@ -1012,6 +1012,15 @@ export class OrRules extends translate(i18next)(LitElement) {const newRulesets = await this._rulesTree?.refresh();this._checkForViewerUpdate(undefined, newRulesets);+ // After the tree has refreshed, check if the saved rule belongs to a group.+ const savedRuleset = event.detail.ruleset;+ const groupId = savedRuleset.meta?.groupId as string;++ // If it has a group ID, expand tree component to show that group and rules that are inside.+ if (groupId && this._rulesTree) {+ this._rulesTree.expandGroup(groupId);+ }+// Select the ruleset if it's newif (event.detail.isNew) {const ruleset = newRulesets?.find(r => r.id && r.id === event.detail.ruleset.id);diff --git a/ui/component/or-rules/src/or-rule-tree.ts b/ui/component/or-rules/src/or-rule-tree.tsindex 0f3cc882c..cb59ae8be 100644--- a/ui/component/or-rules/src/or-rule-tree.ts+++ b/ui/component/or-rules/src/or-rule-tree.ts@@ -127,7 +127,14 @@ export class OrRuleTree extends OrTreeMenu {if (!this.rules) {this._loadRulesets(this.global).then(rulesets => this.rules = rulesets);} else {- this.nodes = [...this._getRuleNodes(this.rules), ...this._cachedEmptyGroupNodes];+ // This stops a group from showing up twice after its first rule is added.+ // It removes the old, empty version of the group from memory so only the new, updated one is displayed.+ const ruleNodes = this._getRuleNodes(this.rules);+ const ruleNodeIds = new Set(ruleNodes.map(node => node.id));+ const emptyGroups = this._cachedEmptyGroupNodes+ .filter(cachedNode => !ruleNodeIds.has(cachedNode.id));+ this.nodes = [...ruleNodes, ...emptyGroups];+ this._cachedEmptyGroupNodes = emptyGroups;}}if (changedProps.has("nodes")) {@@ -466,6 +473,19 @@ export class OrRuleTree extends OrTreeMenu {rules: undefined};+ const selectedNodes = this._findSelectedTreeNodes() as RuleTreeNode[];+ if (selectedNodes.length === 1 && selectedNodes[0].children) {+ const selectedGroupNode = selectedNodes[0];+ const groupId = selectedGroupNode.id;++ if (groupId) {+ ruleset.meta = {+ ...(ruleset.meta || {}),+ groupId: groupId+ };+ }+ }+if (this.config && this.config.rulesetAddHandler && !this.config.rulesetAddHandler(ruleset)) {return;}diff --git a/ui/component/or-tree-menu/src/index.ts b/ui/component/or-tree-menu/src/index.tsindex 01a002933..42d1c7470 100644--- a/ui/component/or-tree-menu/src/index.ts+++ b/ui/component/or-tree-menu/src/index.ts@@ -736,4 +736,22 @@ export class OrTreeMenu extends LitElement {return Util.sortByString(node => node.label);}}++ /**+ * Programmatically finds a group by its ID and sets it to be expanded.+ * This is called by the parent component after a new node is added to a tree group.+ * @param {string} groupId The ID of the group to expand.+ */+ public expandGroup(groupId: string) {+ // Wait for any pending Lit updates to complete to ensure the node list is stable.+ this.updateComplete.then(() => {+ const groupNode = this.nodes.find(node => node.id === groupId && node.children);++ if (groupNode) {+ groupNode.expanded = true;+ // re-render with the change.+ this.requestUpdate();+ }+ });+ }}