Skip to content

Commit

Permalink
feat(Tree): adds ability to acquire tree underlying model (#168). Closes
Browse files Browse the repository at this point in the history
 #147

Thanks @avadhootha for this feature
#159
  • Loading branch information
Georgii Rychko authored Nov 19, 2017
1 parent 3d0826a commit 68c4dcf
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
- [isCollapsed - check whether a node is collapsed](#iscollapsed---check-whether-a-node-is-collapsed)
- [expand - expands a node](#expand---expands-a-node)
- [isExpanded - checks whether a node is expanded](#isexpanded---checks-whether-a-node-is-expanded)
- [toTreeModel - converts a tree to a TreeModel instance](#totreemodel---converts-a-tree-to-a-treemodel-instance)
- [rename - renames a node (changes its value underneath)](#rename---renames-a-node-changes-its-value-underneath)
- [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)
- [remove - removes a node from the tree](#remove---removes-a-node-from-the-tree)
Expand Down Expand Up @@ -659,6 +660,14 @@ This method expands the node in case it can be expanded. On successful expanding
oopNodeController.isExpanded();
```

#### toTreeModel - converts a tree to a TreeModel instance

Actually controller makes and returns a clone of tree's underlying model

```typescript
oopNodeController.toTreeModel();
```

This method returns true if the node is expanded and false otherwise.

#### rename - renames a node (changes its value underneath)
Expand Down
4 changes: 4 additions & 0 deletions src/tree-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export class TreeController {
return this.tree.isNodeCollapsed();
}

public toTreeModel(): TreeModel {
return this.tree.toTreeModel();
}

public rename(newValue: string): void {
this.tree.markAsBeingRenamed();
this.component.applyNewValue({ type: 'keyup', value: newValue });
Expand Down
19 changes: 18 additions & 1 deletion src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
size,
once,
includes,
isNil
isNil,
defaultsDeep
} from './utils/fn.utils';

import { Observable, Observer } from 'rxjs/Rx';
Expand Down Expand Up @@ -582,4 +583,20 @@ export class Tree {
public markAsModified(): void {
this.node._status = TreeStatus.Modified;
}

/**
* Makes a clone of an underlying TreeModel instance
* @returns {TreeModel} a clone of an underlying TreeModel instance
*/
public toTreeModel(): TreeModel {
const model = defaultsDeep(this.isLeaf() ? {} : {children: []}, this.node);

if (this.children) {
this.children.forEach(child => {
model.children.push(child.toTreeModel());
});
}

return model;
}
}
15 changes: 15 additions & 0 deletions test/tree-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,21 @@ describe('TreeController', () => {

expect(lordInternalTreeInstance.tree.isBeingRenamed()).toEqual(true);
});

it('knows how to convert a tree to tree model', () => {

const model = { value: 'bla' };

const tree: any = {
toTreeModel: jasmine.createSpy('tree.toTreeModel').and.returnValue(model)
};

const controller = new TreeController({tree, treeService: null} as any);

const actualModel = controller.toTreeModel();

expect(actualModel).toBe(model);
});
});

function nodeNameOf(internalTreeDebugElement: DebugElement): string {
Expand Down
24 changes: 24 additions & 0 deletions test/tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1129,4 +1129,28 @@ describe('Tree', () => {
expect(tree.isBranch()).toBeTruthy();
});

it('can be converted to TreeModel', () => {

const model: TreeModel = {
id: 6,
value: 'root',
emitLoadNextLevel: false,
settings: {
isCollapsedOnInit: true,
static: false,
leftMenu: false,
rightMenu: true
},
children: [
{
value: 'child#1',
emitLoadNextLevel: false,
settings: { isCollapsedOnInit: true, static: false, leftMenu: false, rightMenu: true } }
]
};

const tree: Tree = new Tree(model);

expect(tree.toTreeModel()).toEqual(model);
});
});

0 comments on commit 68c4dcf

Please sign in to comment.