Skip to content

Commit 68c4dcf

Browse files
author
Georgii Rychko
authored
feat(Tree): adds ability to acquire tree underlying model (#168). Closes #147
Thanks @avadhootha for this feature #159
1 parent 3d0826a commit 68c4dcf

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- [isCollapsed - check whether a node is collapsed](#iscollapsed---check-whether-a-node-is-collapsed)
3333
- [expand - expands a node](#expand---expands-a-node)
3434
- [isExpanded - checks whether a node is expanded](#isexpanded---checks-whether-a-node-is-expanded)
35+
- [toTreeModel - converts a tree to a TreeModel instance](#totreemodel---converts-a-tree-to-a-treemodel-instance)
3536
- [rename - renames a node (changes its value underneath)](#rename---renames-a-node-changes-its-value-underneath)
3637
- [startRenaming - changes the node template so that text input appears and lets a user type a new name](#startrenaming---changes-the-node-template-so-that-text-input-appears-and-lets-a-user-type-a-new-name)
3738
- [remove - removes a node from the tree](#remove---removes-a-node-from-the-tree)
@@ -659,6 +660,14 @@ This method expands the node in case it can be expanded. On successful expanding
659660
oopNodeController.isExpanded();
660661
```
661662

663+
#### toTreeModel - converts a tree to a TreeModel instance
664+
665+
Actually controller makes and returns a clone of tree's underlying model
666+
667+
```typescript
668+
oopNodeController.toTreeModel();
669+
```
670+
662671
This method returns true if the node is expanded and false otherwise.
663672

664673
#### rename - renames a node (changes its value underneath)

src/tree-controller.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export class TreeController {
4444
return this.tree.isNodeCollapsed();
4545
}
4646

47+
public toTreeModel(): TreeModel {
48+
return this.tree.toTreeModel();
49+
}
50+
4751
public rename(newValue: string): void {
4852
this.tree.markAsBeingRenamed();
4953
this.component.applyNewValue({ type: 'keyup', value: newValue });

src/tree.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
size,
99
once,
1010
includes,
11-
isNil
11+
isNil,
12+
defaultsDeep
1213
} from './utils/fn.utils';
1314

1415
import { Observable, Observer } from 'rxjs/Rx';
@@ -582,4 +583,20 @@ export class Tree {
582583
public markAsModified(): void {
583584
this.node._status = TreeStatus.Modified;
584585
}
586+
587+
/**
588+
* Makes a clone of an underlying TreeModel instance
589+
* @returns {TreeModel} a clone of an underlying TreeModel instance
590+
*/
591+
public toTreeModel(): TreeModel {
592+
const model = defaultsDeep(this.isLeaf() ? {} : {children: []}, this.node);
593+
594+
if (this.children) {
595+
this.children.forEach(child => {
596+
model.children.push(child.toTreeModel());
597+
});
598+
}
599+
600+
return model;
601+
}
585602
}

test/tree-controller.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,21 @@ describe('TreeController', () => {
347347

348348
expect(lordInternalTreeInstance.tree.isBeingRenamed()).toEqual(true);
349349
});
350+
351+
it('knows how to convert a tree to tree model', () => {
352+
353+
const model = { value: 'bla' };
354+
355+
const tree: any = {
356+
toTreeModel: jasmine.createSpy('tree.toTreeModel').and.returnValue(model)
357+
};
358+
359+
const controller = new TreeController({tree, treeService: null} as any);
360+
361+
const actualModel = controller.toTreeModel();
362+
363+
expect(actualModel).toBe(model);
364+
});
350365
});
351366

352367
function nodeNameOf(internalTreeDebugElement: DebugElement): string {

test/tree.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,4 +1129,28 @@ describe('Tree', () => {
11291129
expect(tree.isBranch()).toBeTruthy();
11301130
});
11311131

1132+
it('can be converted to TreeModel', () => {
1133+
1134+
const model: TreeModel = {
1135+
id: 6,
1136+
value: 'root',
1137+
emitLoadNextLevel: false,
1138+
settings: {
1139+
isCollapsedOnInit: true,
1140+
static: false,
1141+
leftMenu: false,
1142+
rightMenu: true
1143+
},
1144+
children: [
1145+
{
1146+
value: 'child#1',
1147+
emitLoadNextLevel: false,
1148+
settings: { isCollapsedOnInit: true, static: false, leftMenu: false, rightMenu: true } }
1149+
]
1150+
};
1151+
1152+
const tree: Tree = new Tree(model);
1153+
1154+
expect(tree.toTreeModel()).toEqual(model);
1155+
});
11321156
});

0 commit comments

Comments
 (0)