Skip to content
Snippets Groups Projects
Commit 993bd453 authored by Marc Hanisch's avatar Marc Hanisch
Browse files

better handling of nodes with its device or platform id

parent 932b61ad
No related branches found
No related tags found
1 merge request!38Feature use configurations data model
......@@ -166,21 +166,21 @@ export class ConfigurationsTree implements Iterable<ConfigurationsTreeNode> {
/**
* returns the path to the node in the tree
*
* @param {number} nodeId - the node to get the path for
* @param {ConfigurationsTreeNode} node - the node to get the path for
* @return {string[]} an array of node names
*/
getPath (nodeId: number): string[] {
const getPathRecursive = (nodeId: number, nodes: ConfigurationsTree, path: string[]): boolean => {
for (const node of nodes) {
if (node.id === nodeId) {
path.push(node.name)
getPath (node: ConfigurationsTreeNode): string[] {
const getPathRecursive = (node: ConfigurationsTreeNode, nodes: ConfigurationsTree, path: string[]): boolean => {
for (const aNode of nodes) {
if (aNode === node) {
path.push(aNode.name)
return true
}
if (!node.canHaveChildren()) {
if (!aNode.canHaveChildren()) {
continue
}
if (getPathRecursive(nodeId, (node as PlatformNode).getTree(), path)) {
path.unshift(node.name)
if (getPathRecursive(node, (aNode as PlatformNode).getTree(), path)) {
path.unshift(aNode.name)
return true
}
}
......@@ -188,18 +188,18 @@ export class ConfigurationsTree implements Iterable<ConfigurationsTreeNode> {
}
const paths: string[] = []
getPathRecursive(nodeId, this, paths)
getPathRecursive(node, this, paths)
return paths
}
/**
* finds a node in the tree by its id
*
* @param {number} nodeId - the id of the node to search
* @param {string} nodeId - the id of the node to search
* @return {ConfigurationsTreeNode|null} the found node, null if it was not found
*/
getById (nodeId: number): ConfigurationsTreeNode | null {
const getByIdRecursive = (nodeId: number, nodes: ConfigurationsTree): ConfigurationsTreeNode | null => {
getById (nodeId: string): ConfigurationsTreeNode | null {
const getByIdRecursive = (nodeId: string, nodes: ConfigurationsTree): ConfigurationsTreeNode | null => {
for (const node of nodes) {
if (node.id === nodeId) {
return node
......@@ -218,6 +218,58 @@ export class ConfigurationsTree implements Iterable<ConfigurationsTreeNode> {
return getByIdRecursive(nodeId, this)
}
/**
* finds a platform node in the tree by its id
*
* @param {number} nodeId - the id of the node to search
* @return {PlatformNode|null} the found node, null if it was not found
*/
getPlatformById (platformId: number): PlatformNode | null {
const getByIdRecursive = (platformId: number, nodes: ConfigurationsTree): PlatformNode | null => {
for (const node of nodes) {
if (node instanceof PlatformNode && node.unpack().id === platformId) {
return node
}
if (!node.canHaveChildren()) {
continue
}
const found = getByIdRecursive(platformId, (node as PlatformNode).getTree())
if (!found) {
continue
}
return found
}
return null
}
return getByIdRecursive(platformId, this)
}
/**
* finds a device node in the tree by its id
*
* @param {number} nodeId - the id of the node to search
* @return {DeviceNode|null} the found node, null if it was not found
*/
getDeviceById (deviceId: number): DeviceNode | null {
const getByIdRecursive = (deviceId: number, nodes: ConfigurationsTree): DeviceNode | null => {
for (const node of nodes) {
if (node instanceof DeviceNode && node.unpack().id === deviceId) {
return node
}
if (!node.canHaveChildren()) {
continue
}
const found = getByIdRecursive(deviceId, (node as PlatformNode).getTree())
if (!found) {
continue
}
return found
}
return null
}
return getByIdRecursive(deviceId, this)
}
/**
* returns the parent of a node in the tree
*
......
......@@ -122,9 +122,9 @@ describe('ConfigurationsTree', () => {
const tree = ConfigurationsTree.fromArray([platformNode])
expect(tree.getPath(2)).toEqual(['Platform', 'Device'])
expect(tree.getPath(deviceNode)).toEqual(['Platform', 'Device'])
// when a node is not found, just an empty array should be returned
expect(tree.getPath(3)).toEqual([])
expect(tree.getPath(new DeviceNode(new Device()))).toEqual([])
})
it('should return a node by its id recursively', () => {
......@@ -143,7 +143,8 @@ describe('ConfigurationsTree', () => {
const tree = ConfigurationsTree.fromArray([platformNode])
expect(Object.is(tree.getById(2), deviceNode)).toBeTruthy()
expect(Object.is(tree.getPlatformById(1), platformNode)).toBeTruthy()
expect(Object.is(tree.getDeviceById(2), deviceNode)).toBeTruthy()
})
it('should return the parent node of a node', () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment