Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
fix: calculate delta on each update
Browse files Browse the repository at this point in the history
  • Loading branch information
artemnih committed Mar 4, 2022
1 parent 7015319 commit c10ad5b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<AvialableView>) {
}
Expand Down
17 changes: 12 additions & 5 deletions projects/ngx-explorer/src/lib/services/explorer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export class ExplorerService {
const breadcrumbs = Utils.buildBreadcrumbs(this.flatPointers, parent);
this._breadcrumbs.next(breadcrumbs);
this._selectedNodes.next([]);

})
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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();
});
Expand All @@ -121,7 +119,16 @@ export class ExplorerService {
.pipe(tap(({ leafs, nodes }: NodeContent<any>) => {
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);
}))
Expand Down
4 changes: 4 additions & 0 deletions projects/ngx-explorer/src/lib/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ export class Utils {
}
return pieces;
}

static compareObjects(a: any, b: any) {
return JSON.stringify(a) === JSON.stringify(b);
}
}

0 comments on commit c10ad5b

Please sign in to comment.