Skip to content

Commit 0db681c

Browse files
motabassrychkog
authored andcommitted
fix(tree, menu): return proper value for positionInParent call; change z-index of the menu
* added z-index of 999 to prevent right click menu from being covered by other elements * implemented lastIndex as second parameter of NodeRemovedEvent (#111) * fixed null pointer in positionInParent() for top level node
1 parent 40fec4d commit 0db681c

6 files changed

Lines changed: 100 additions & 29 deletions

File tree

src/styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
background-color: #eee;
1414
color: #212121;
1515
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
16+
z-index: 999;
1617
}
1718

1819
.node-menu .node-menu-content li.node-menu-item {

src/tree.events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class NodeMovedEvent extends NodeDestructiveEvent {
2525
}
2626

2727
export class NodeRemovedEvent extends NodeDestructiveEvent {
28-
public constructor(node: Tree) {
28+
public constructor(node: Tree, public lastIndex: number) {
2929
super(node);
3030
}
3131
}

src/tree.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class TreeService {
3333
}
3434

3535
public fireNodeRemoved(tree: Tree): void {
36-
this.nodeRemoved$.next(new NodeRemovedEvent(tree));
36+
this.nodeRemoved$.next(new NodeRemovedEvent(tree, tree.positionInParent));
3737
}
3838

3939
public fireNodeCreated(tree: Tree): void {

src/tree.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
omit,
88
size,
99
once,
10-
includes
10+
includes,
11+
isNil
1112
} from './utils/fn.utils';
1213

1314
import { Observable, Observer } from 'rxjs/Rx';
@@ -262,6 +263,10 @@ export class Tree {
262263
* @returns {number} The position inside a parent.
263264
*/
264265
public get positionInParent(): number {
266+
if (this.isRoot()) {
267+
return -1;
268+
}
269+
265270
return this.parent.children ? this.parent.children.indexOf(this) : -1;
266271
}
267272

@@ -318,7 +323,7 @@ export class Tree {
318323
* @returns {boolean} A flag indicating whether or not this tree is the root.
319324
*/
320325
public isRoot(): boolean {
321-
return this.parent === null;
326+
return isNil(this.parent);
322327
}
323328

324329
/**
@@ -440,7 +445,7 @@ export class Tree {
440445
} else if (this.node._foldingType === FoldingType.Expanded) {
441446
return get(this.node.settings, 'cssClasses.expanded', null);
442447
} else if (this.node._foldingType === FoldingType.Empty) {
443-
return get(this.node.settings, 'cssClasses.empty', null);
448+
return get(this.node.settings, 'cssClasses.empty', null);
444449
}
445450

446451
return get(this.node.settings, 'cssClasses.leaf', null);

test/tree.service.spec.ts

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,30 @@ describe('TreeService', () => {
4343
it('fires node removed events', () => {
4444
spyOn(treeService.nodeRemoved$, 'next');
4545

46-
const tree = new Tree({value: 'Master'});
46+
const tree = new Tree({ value: 'Master' });
4747
treeService.fireNodeRemoved(tree);
4848

4949
expect(treeService.nodeRemoved$.next).toHaveBeenCalledTimes(1);
50-
expect(treeService.nodeRemoved$.next).toHaveBeenCalledWith(new NodeRemovedEvent(tree));
50+
expect(treeService.nodeRemoved$.next).toHaveBeenCalledWith(new NodeRemovedEvent(tree, -1));
51+
});
52+
53+
it('fires node removed events witch corretly identified postion removed node used to have in its parent', () => {
54+
spyOn(treeService.nodeRemoved$, 'next');
55+
56+
const child1 = { value: 'Servant#1' };
57+
const child2 = { value: 'Servant#2' };
58+
const tree = new Tree({ value: 'Master', children: [child1, child2] });
59+
treeService.fireNodeRemoved(tree.children[1]);
60+
61+
expect(treeService.nodeRemoved$.next).toHaveBeenCalledTimes(1);
62+
expect(treeService.nodeRemoved$.next).toHaveBeenCalledWith(new NodeRemovedEvent(tree.children[1], 1));
5163
});
5264

5365
it('fires node moved events', () => {
5466
spyOn(treeService.nodeMoved$, 'next');
5567

56-
const parent = new Tree({value: 'Master Pa'});
57-
const tree = new Tree({value: 'Master'}, parent);
68+
const parent = new Tree({ value: 'Master Pa' });
69+
const tree = new Tree({ value: 'Master' }, parent);
5870

5971
treeService.fireNodeMoved(tree, parent);
6072

@@ -65,7 +77,7 @@ describe('TreeService', () => {
6577
it('fires node created events', () => {
6678
spyOn(treeService.nodeCreated$, 'next');
6779

68-
const tree = new Tree({value: 'Master'});
80+
const tree = new Tree({ value: 'Master' });
6981

7082
treeService.fireNodeCreated(tree, parent);
7183

@@ -76,7 +88,7 @@ describe('TreeService', () => {
7688
it('fires node selected events', () => {
7789
spyOn(treeService.nodeSelected$, 'next');
7890

79-
const tree = new Tree({value: 'Master'});
91+
const tree = new Tree({ value: 'Master' });
8092

8193
treeService.fireNodeSelected(tree);
8294

@@ -87,7 +99,7 @@ describe('TreeService', () => {
8799
it('fires node renamed events', () => {
88100
spyOn(treeService.nodeRenamed$, 'next');
89101

90-
const tree = new Tree({value: 'Master'});
102+
const tree = new Tree({ value: 'Master' });
91103

92104
treeService.fireNodeRenamed('Bla', tree);
93105

@@ -98,7 +110,7 @@ describe('TreeService', () => {
98110
it('fires node expanded events', () => {
99111
spyOn(treeService.nodeExpanded$, 'next');
100112

101-
const tree = new Tree({value: 'Master'});
113+
const tree = new Tree({ value: 'Master' });
102114

103115
treeService.fireNodeExpanded(tree);
104116

@@ -109,7 +121,7 @@ describe('TreeService', () => {
109121
it('fires node collapsed events', () => {
110122
spyOn(treeService.nodeCollapsed$, 'next');
111123

112-
const tree = new Tree({value: 'Master'});
124+
const tree = new Tree({ value: 'Master' });
113125

114126
treeService.fireNodeCollapsed(tree);
115127

@@ -118,9 +130,9 @@ describe('TreeService', () => {
118130
});
119131

120132
it('fires events on which other tree should remove selection', done => {
121-
const selectedTree = new Tree({value: 'Master'});
133+
const selectedTree = new Tree({ value: 'Master' });
122134

123-
const tree = new Tree({value: 'Master'});
135+
const tree = new Tree({ value: 'Master' });
124136
treeService.unselectStream(tree)
125137
.subscribe((e: NodeSelectedEvent) => {
126138
expect(e.node).toBe(selectedTree);
@@ -134,8 +146,8 @@ describe('TreeService', () => {
134146
const masterTree = new Tree({
135147
value: 'Master',
136148
children: [
137-
{value: 'Servant#1'},
138-
{value: 'Servant#2'}
149+
{ value: 'Servant#1' },
150+
{ value: 'Servant#2' }
139151
]
140152
});
141153

@@ -156,12 +168,12 @@ describe('TreeService', () => {
156168
const masterTree = new Tree({
157169
value: 'Master',
158170
children: [
159-
{value: 'Servant#1'},
160-
{value: 'Servant#2'}
171+
{ value: 'Servant#1' },
172+
{ value: 'Servant#2' }
161173
]
162174
});
163175

164-
const tree = new Tree({value: 'tree'});
176+
const tree = new Tree({ value: 'tree' });
165177

166178
const elementRef = new ElementRef(null);
167179

@@ -208,8 +220,8 @@ describe('TreeService', () => {
208220
const masterTree = new Tree({
209221
value: 'Master',
210222
children: [
211-
{value: 'Servant#1'},
212-
{value: 'Servant#2'}
223+
{ value: 'Servant#1' },
224+
{ value: 'Servant#2' }
213225
]
214226
});
215227

@@ -226,8 +238,8 @@ describe('TreeService', () => {
226238
const masterTree = new Tree({
227239
value: 'Master',
228240
children: [
229-
{value: 'Servant#1'},
230-
{value: 'Servant#2'}
241+
{ value: 'Servant#1' },
242+
{ value: 'Servant#2' }
231243
]
232244
});
233245

test/tree.spec.ts

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ describe('Tree', () => {
8181

8282
it('should know how to detect Renamable node', () => {
8383
const renamableNode = {
84-
setName: () => {},
85-
toString: () => {}
84+
setName: () => { },
85+
toString: () => { }
8686
};
8787

8888
const renamableNodeImposter = {
@@ -232,6 +232,59 @@ describe('Tree', () => {
232232
expect(child.value).toEqual('Master');
233233
});
234234

235+
it('has position which equals to -1 when does not have a parent', () => {
236+
const fonts: TreeModel = {
237+
value: 'Serif - All my children and I are STATIC ¯\\_(ツ)_/¯',
238+
children: [
239+
{ value: 'Antiqua' },
240+
{ value: 'DejaVu Serif' },
241+
{ value: 'Garamond' },
242+
{ value: 'Georgia' },
243+
{ value: 'Times New Roman' },
244+
]
245+
};
246+
247+
const tree = new Tree(fonts);
248+
expect(tree.positionInParent).toEqual(-1);
249+
});
250+
251+
it('has position which equals to -1 when still connected to parent but parent already does not have children', () => {
252+
const fonts: TreeModel = {
253+
value: 'Serif - All my children and I are STATIC ¯\\_(ツ)_/¯',
254+
children: [
255+
{ value: 'Antiqua' },
256+
{ value: 'DejaVu Serif' },
257+
{ value: 'Garamond' },
258+
{ value: 'Georgia' },
259+
{ value: 'Times New Roman' },
260+
]
261+
};
262+
263+
const tree = new Tree(fonts);
264+
const child = tree.children[3];
265+
266+
(tree as any)._children = null;
267+
expect(child.positionInParent).toEqual(-1);
268+
});
269+
270+
it('has position which equals to actual position rendered', () => {
271+
const fonts: TreeModel = {
272+
value: 'Serif - All my children and I are STATIC ¯\\_(ツ)_/¯',
273+
children: [
274+
{ value: 'Antiqua' },
275+
{ value: 'DejaVu Serif' },
276+
{ value: 'Garamond' },
277+
{ value: 'Georgia' },
278+
{ value: 'Times New Roman' },
279+
]
280+
};
281+
282+
const tree = new Tree(fonts);
283+
const child = tree.children[4];
284+
285+
expect(child.positionInParent).toEqual(4);
286+
});
287+
235288
it('adds child to a particular position in a tree', () => {
236289
const fonts: TreeModel = {
237290
value: 'Serif - All my children and I are STATIC ¯\\_(ツ)_/¯',
@@ -311,7 +364,7 @@ describe('Tree', () => {
311364
children: null
312365
});
313366

314-
const addedSibling = tree.addSibling(new Tree({value: 'bla'}));
367+
const addedSibling = tree.addSibling(new Tree({ value: 'bla' }));
315368

316369
expect(addedSibling).toBeNull();
317370
expect(tree.parent).toBeNull();
@@ -729,7 +782,7 @@ describe('Tree', () => {
729782
it('cannot switch "Empty" folding type', () => {
730783
const masterTree = new Tree({
731784
value: 'Master',
732-
children: []
785+
children: []
733786
});
734787

735788
expect(masterTree.foldingType).toEqual(FoldingType.Empty);

0 commit comments

Comments
 (0)