@@ -20,8 +20,16 @@ export class BaseNodeMapper {
2020 } ;
2121 }
2222
23- static getNodesWithChildren ( data : BaseNodeDataJsonApi [ ] , parentId : string ) : NodeShortInfoModel [ ] {
24- return this . getAllDescendants ( data , parentId ) . map ( ( item ) => ( {
23+ static getNodesWithChildren (
24+ data : BaseNodeDataJsonApi [ ] ,
25+ parentId : string ,
26+ includeAncestors = false
27+ ) : NodeShortInfoModel [ ] {
28+ const nodes = includeAncestors
29+ ? this . getAllAncestorsAndDescendants ( data , parentId )
30+ : this . getAllDescendants ( data , parentId ) ;
31+
32+ return nodes . map ( ( item ) => ( {
2533 id : item . id ,
2634 title : replaceBadEncodedChars ( item . attributes . title ) ,
2735 isPublic : item . attributes . public ,
@@ -82,4 +90,23 @@ export class BaseNodeMapper {
8290
8391 return [ parent , ...descendants ] ;
8492 }
93+
94+ static getAllAncestors ( allNodes : BaseNodeDataJsonApi [ ] , nodeId : string ) : BaseNodeDataJsonApi [ ] {
95+ const node = allNodes . find ( ( n ) => n . id === nodeId ) ;
96+ if ( ! node ) return [ ] ;
97+
98+ const parentId = node . relationships . parent ?. data ?. id ;
99+ if ( ! parentId ) return [ node ] ;
100+
101+ const ancestors = this . getAllAncestors ( allNodes , parentId ) ;
102+
103+ return [ node , ...ancestors ] ;
104+ }
105+
106+ static getAllAncestorsAndDescendants ( allNodes : BaseNodeDataJsonApi [ ] , nodeId : string ) : BaseNodeDataJsonApi [ ] {
107+ const ancestors = this . getAllAncestors ( allNodes , nodeId ) ;
108+ const descendants = this . getAllDescendants ( allNodes , nodeId ) . slice ( 1 ) ;
109+
110+ return [ ...ancestors , ...descendants ] ;
111+ }
85112}
0 commit comments