Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
Frontend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Package Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
HUB Terra
SMS
Frontend
Commits
993bd453
Commit
993bd453
authored
4 years ago
by
Marc Hanisch
Browse files
Options
Downloads
Patches
Plain Diff
better handling of nodes with its device or platform id
parent
932b61ad
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!38
Feature use configurations data model
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
models/ConfigurationsTree.ts
+65
-13
65 additions, 13 deletions
models/ConfigurationsTree.ts
test/models/ConfigurationsTree.test.ts
+4
-3
4 additions, 3 deletions
test/models/ConfigurationsTree.test.ts
with
69 additions
and
16 deletions
models/ConfigurationsTree.ts
+
65
−
13
View file @
993bd453
...
...
@@ -166,21 +166,21 @@ export class ConfigurationsTree implements Iterable<ConfigurationsTreeNode> {
/**
* returns the path to the node in the tree
*
* @param {
number
} node
Id
- 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
(
node
Id
:
number
):
string
[]
{
const
getPathRecursive
=
(
node
Id
:
number
,
nodes
:
ConfigurationsTree
,
path
:
string
[]):
boolean
=>
{
for
(
const
n
ode
of
nodes
)
{
if
(
n
ode
.
id
===
node
Id
)
{
path
.
push
(
n
ode
.
name
)
getPath
(
node
:
ConfigurationsTreeNode
):
string
[]
{
const
getPathRecursive
=
(
node
:
ConfigurationsTreeNode
,
nodes
:
ConfigurationsTree
,
path
:
string
[]):
boolean
=>
{
for
(
const
aN
ode
of
nodes
)
{
if
(
aN
ode
===
node
)
{
path
.
push
(
aN
ode
.
name
)
return
true
}
if
(
!
n
ode
.
canHaveChildren
())
{
if
(
!
aN
ode
.
canHaveChildren
())
{
continue
}
if
(
getPathRecursive
(
node
Id
,
(
n
ode
as
PlatformNode
).
getTree
(),
path
))
{
path
.
unshift
(
n
ode
.
name
)
if
(
getPathRecursive
(
node
,
(
aN
ode
as
PlatformNode
).
getTree
(),
path
))
{
path
.
unshift
(
aN
ode
.
name
)
return
true
}
}
...
...
@@ -188,18 +188,18 @@ export class ConfigurationsTree implements Iterable<ConfigurationsTreeNode> {
}
const
paths
:
string
[]
=
[]
getPathRecursive
(
node
Id
,
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
*
...
...
This diff is collapsed.
Click to expand it.
test/models/ConfigurationsTree.test.ts
+
4
−
3
View file @
993bd453
...
...
@@ -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
'
,
()
=>
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment