From c10ad5bb29e6ae5c7b3a878ab8159c19d73ee17f Mon Sep 17 00:00:00 2001 From: artemnih <23387542+artemnih@users.noreply.github.com> Date: Fri, 4 Mar 2022 12:59:57 -0500 Subject: [PATCH] fix: calculate delta on each update --- .../src/lib/components/tree/tree.component.ts | 2 +- .../view-switcher/view-switcher.component.ts | 2 +- .../src/lib/services/explorer.service.ts | 17 ++++++++++++----- projects/ngx-explorer/src/lib/shared/utils.ts | 4 ++++ 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/projects/ngx-explorer/src/lib/components/tree/tree.component.ts b/projects/ngx-explorer/src/lib/components/tree/tree.component.ts index ea91b72..d87ba14 100644 --- a/projects/ngx-explorer/src/lib/components/tree/tree.component.ts +++ b/projects/ngx-explorer/src/lib/components/tree/tree.component.ts @@ -17,7 +17,7 @@ interface TreeNode extends XNode { encapsulation: ViewEncapsulation.None }) export class TreeComponent implements OnDestroy { - public treeNodes: any = []; + public treeNodes: TreeNode[] = []; private expandedIds: string[] = []; private sub = new Subscription(); diff --git a/projects/ngx-explorer/src/lib/components/view-switcher/view-switcher.component.ts b/projects/ngx-explorer/src/lib/components/view-switcher/view-switcher.component.ts index 68331c3..b3c9d13 100644 --- a/projects/ngx-explorer/src/lib/components/view-switcher/view-switcher.component.ts +++ b/projects/ngx-explorer/src/lib/components/view-switcher/view-switcher.component.ts @@ -11,7 +11,7 @@ import { CURRENT_VIEW } from '../../injection-tokens/current-view.token'; }) export class ViewSwitcherComponent { - public avialableView = AvialableView; + public readonly avialableView = AvialableView; constructor(@Inject(CURRENT_VIEW) private currentView: BehaviorSubject) { } diff --git a/projects/ngx-explorer/src/lib/services/explorer.service.ts b/projects/ngx-explorer/src/lib/services/explorer.service.ts index 4a95521..c9b9f8e 100644 --- a/projects/ngx-explorer/src/lib/services/explorer.service.ts +++ b/projects/ngx-explorer/src/lib/services/explorer.service.ts @@ -37,7 +37,6 @@ export class ExplorerService { const breadcrumbs = Utils.buildBreadcrumbs(this.flatPointers, parent); this._breadcrumbs.next(breadcrumbs); this._selectedNodes.next([]); - }) } @@ -48,13 +47,12 @@ export class ExplorerService { public createNode(name: string) { const parent = this._openedNode.value; this.dataService.createNode(parent.data, name).subscribe(() => { - // as option, get new data and insert into children this.refresh(); }) } public refresh() { - this.openNode(this._openedNode.value.id); // TODO: temp, until left nav is done + this.openNode(this._openedNode.value.id); } public rename(name: string) { @@ -104,7 +102,7 @@ export class ExplorerService { } public download() { - const target = this._selectedNodes.value[0]; // TODO: add mutliple selection support + const target = this._selectedNodes.value[0]; this.dataService.download(target.data).subscribe(() => { this.refresh(); }); @@ -121,7 +119,16 @@ export class ExplorerService { .pipe(tap(({ leafs, nodes }: NodeContent) => { const childrenNodes = nodes.map(data => Utils.createNode(id, false, data)); const childrenLeafs = leafs.map(data => Utils.createNode(id, true, data)); - parent.children = childrenNodes.concat(childrenLeafs); + const newChildren = childrenNodes.concat(childrenLeafs); + const oldChildren = parent.children; + const added = newChildren.filter(c => !oldChildren.find(o => Utils.compareObjects(o.data, c.data))); + const removed = oldChildren.filter(o => !newChildren.find(c => Utils.compareObjects(o.data, c.data))); + added.forEach(c => parent.children.push(c)); + removed.forEach(c => { + const index = parent.children.findIndex(o => o.id === c.id); + parent.children.splice(index, 1); + }); + this.flatPointers = Utils.getHashMap(this.inTree); this._tree.next(this.inTree); })) diff --git a/projects/ngx-explorer/src/lib/shared/utils.ts b/projects/ngx-explorer/src/lib/shared/utils.ts index e38c8af..d638abe 100644 --- a/projects/ngx-explorer/src/lib/shared/utils.ts +++ b/projects/ngx-explorer/src/lib/shared/utils.ts @@ -37,4 +37,8 @@ export class Utils { } return pieces; } + + static compareObjects(a: any, b: any) { + return JSON.stringify(a) === JSON.stringify(b); + } } \ No newline at end of file