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

Commit

Permalink
feat: refactor breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
artemnih committed Oct 5, 2021
1 parent d7f0a2e commit 60d9288
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,11 @@ export class BreadcrumbsComponent {
private sub = new Subscription();

constructor(private explorerService: ExplorerService, private helperService: HelperService) {
this.sub.add(this.explorerService.openedNode.subscribe(n => this.buildBreadcrumbs(n)));
this.sub.add(this.explorerService.breadcrumbs.subscribe(n => this.buildBreadcrumbs(n)));
}

private buildBreadcrumbs(node: XNode) {

// TODO: build breadcrumbs in service and emit to component
const pieces = [];

let currentNode = node;
while (true) {
const crumb = { name: this.helperService.getName(currentNode.data) || 'HOME', node: currentNode };
pieces.unshift(crumb);

if (currentNode.parentId) {
currentNode = this.explorerService.getNode(currentNode.parentId);
} else {
break;
}
}

this.breadcrumbs = pieces;
private buildBreadcrumbs(nodes: XNode[]) {
this.breadcrumbs = nodes.map(n => ({ name: this.helperService.getName(n.data) || 'HOME', node: n }));
}

public click(crumb: Breadcrumb) {
Expand Down
9 changes: 5 additions & 4 deletions projects/ngx-explorer/src/lib/services/explorer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { DataService } from './data.service';
export class ExplorerService {
public readonly selectedNodes = new BehaviorSubject<XNode[]>([]);
public readonly openedNode = new BehaviorSubject<XNode>(undefined);
public readonly breadcrumbs = new BehaviorSubject<XNode[]>([]);

private tree = Utils.createNode();
private flatPointers: Dictionary<XNode> = Utils.getHashMap(this.tree);
Expand Down Expand Up @@ -42,8 +43,8 @@ export class ExplorerService {
parent.children = childrenNodes.concat(childrenLeafs);
this.flatPointers = Utils.getHashMap(this.tree);
this.openedNode.next(parent);
// TODO: update enitre tree
// TODO: update selected nodes: parent node should be selected
const breadcrumbs = Utils.buildBreadcrumbs(this.flatPointers, parent);
this.breadcrumbs.next(breadcrumbs);
});
}

Expand Down Expand Up @@ -75,7 +76,7 @@ export class ExplorerService {
})
} else {
this.dataService.renameNode(node.data, name).subscribe(() => {
this.refresh(); // TODO: refresh entire tree? or all children?
this.refresh();
});
}
}
Expand All @@ -85,7 +86,7 @@ export class ExplorerService {
if (selection.length === 0) {
throw new Error('Nothing selected to remove');
}

const targets = selection.map(node => this.flatPointers[node.id])
const nodes = targets.filter(t => !t.isLeaf).map(data => data.data);
const leafs = targets.filter(t => t.isLeaf).map(data => data.data);
Expand Down
14 changes: 14 additions & 0 deletions projects/ngx-explorer/src/lib/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,18 @@ export class Utils {
children: []
}
}

static buildBreadcrumbs(flatPointers: Dictionary<XNode>, node: XNode) {
const pieces = [] as XNode[];
let currentNode = node;
while (true) {
pieces.unshift(currentNode);
if (currentNode.parentId) {
currentNode = flatPointers[currentNode.parentId];
} else {
break;
}
}
return pieces;
}
}

0 comments on commit 60d9288

Please sign in to comment.