fix: remove node and its children in AbstractTreeService

pull/5376/head
Arman Ozak 5 years ago
parent cc7b272f81
commit df7bb4a06d

@ -51,9 +51,16 @@ export abstract class AbstractTreeService<T extends object> {
}
private filterWith(setOrMap: Set<string> | Map<string, T>): T[] {
return this._flat$.value.filter(
item => !setOrMap.has(item[this.id]) && !setOrMap.has(item[this.parentId]),
);
return this._flat$.value.filter(item => !setOrMap.has(item[this.id]));
}
private findItemsToRemove(set: Set<string>): Set<string> {
return this._flat$.value.reduce((acc, item) => {
if (!acc.has(item[this.parentId])) return acc;
const childSet = new Set([item[this.id]]);
const children = this.findItemsToRemove(childSet);
return new Set([...acc, ...children]);
}, set);
}
private publish(flatItems: T[], visibleItems: T[]): T[] {
@ -104,7 +111,8 @@ export abstract class AbstractTreeService<T extends object> {
const set = new Set<string>();
identifiers.forEach(id => set.add(id));
const flatItems = this.filterWith(set);
const setToRemove = this.findItemsToRemove(set);
const flatItems = this.filterWith(setToRemove);
const visibleItems = flatItems.filter(item => !this.hide(item));
return this.publish(flatItems, visibleItems);

@ -10,6 +10,7 @@ describe('Routes Service', () => {
{ path: '/foo', name: 'foo' },
{ path: '/foo/bar', name: 'bar', parentName: 'foo', invisible: true, order: 2 },
{ path: '/foo/bar/baz', name: 'baz', parentName: 'bar', order: 1 },
{ path: '/foo/bar/baz/qux', name: 'qux', parentName: 'baz', order: 1 },
{ path: '/foo/x', name: 'x', parentName: 'foo', order: 1 },
];
@ -32,11 +33,12 @@ describe('Routes Service', () => {
const tree = await service.tree$.pipe(take(1)).toPromise();
const visible = await service.visible$.pipe(take(1)).toPromise();
expect(flat.length).toBe(4);
expect(flat[3].name).toBe('foo');
expect(flat.length).toBe(5);
expect(flat[0].name).toBe('baz');
expect(flat[1].name).toBe('x');
expect(flat[2].name).toBe('bar');
expect(flat[1].name).toBe('qux');
expect(flat[2].name).toBe('x');
expect(flat[3].name).toBe('bar');
expect(flat[4].name).toBe('foo');
expect(tree.length).toBe(1);
expect(tree[0].name).toBe('foo');
@ -44,6 +46,7 @@ describe('Routes Service', () => {
expect(tree[0].children[0].name).toBe('x');
expect(tree[0].children[1].name).toBe('bar');
expect(tree[0].children[1].children[0].name).toBe('baz');
expect(tree[0].children[1].children[0].children[0].name).toBe('qux');
expect(visible.length).toBe(1);
expect(visible[0].name).toBe('foo');
@ -74,7 +77,8 @@ describe('Routes Service', () => {
expect(service.hasChildren('foo')).toBe(true);
expect(service.hasChildren('bar')).toBe(true);
expect(service.hasChildren('baz')).toBe(false);
expect(service.hasChildren('baz')).toBe(true);
expect(service.hasChildren('qux')).toBe(false);
});
});
@ -123,11 +127,12 @@ describe('Routes Service', () => {
const tree = service.tree;
const visible = service.visible;
expect(flat.length).toBe(4);
expect(flat[3].name).toBe('foo');
expect(flat.length).toBe(5);
expect(flat[0].name).toBe('baz');
expect(flat[1].name).toBe('x');
expect(flat[2].name).toBe('bar');
expect(flat[1].name).toBe('qux');
expect(flat[2].name).toBe('x');
expect(flat[3].name).toBe('bar');
expect(flat[4].name).toBe('foo');
expect(tree.length).toBe(1);
expect(tree[0].name).toBe('foo');
@ -135,6 +140,7 @@ describe('Routes Service', () => {
expect(tree[0].children[0].name).toBe('x');
expect(tree[0].children[1].name).toBe('bar');
expect(tree[0].children[1].children[0].name).toBe('baz');
expect(tree[0].children[1].children[0].children[0].name).toBe('qux');
expect(visible.length).toBe(1);
expect(visible[0].name).toBe('foo');

Loading…
Cancel
Save