Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Generate tree from backend endpoints.
  • Loading branch information
dds14002 committed Mar 30, 2020
1 parent 0b9cae7 commit 74000d7
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/app/main/dropdown/dropdown.component.html
@@ -1,4 +1,4 @@
<div class="d-flex align-items-center p-2">
<div class="d-flex align-items-center p-2" style="height: 3.2rem">
<h3 class="mr-2">End Item: </h3>
<p-dropdown [options]="xaList" [(ngModel)]="selectedEndItem" (onChange)="onChange($event)"></p-dropdown>
</div>
4 changes: 2 additions & 2 deletions src/app/main/dropdown/dropdown.component.ts
Expand Up @@ -17,11 +17,11 @@ export class DropdownComponent implements OnInit {

xaList: SelectItem[];

selectedEndItem;
selectedEndItem: any;

async ngOnInit() {
const xas = await this.geiaService.getXAs().toPromise();
this.xaList = xas.map(xa => ({ label: xa.endItemAcronymCode, value: xa.endItemAcronymCode }));
this.xaList = xas.map(xa => ({ label: xa.endItemAcronymCode, value: xa }));
this.xaList.unshift({ label: 'None selected', value: null });
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/main/main.component.html
@@ -1,7 +1,7 @@
<div class="bg-secondary">
<app-dropdown></app-dropdown>
</div>
<div *ngIf="endItemService.endItem$ | async" class="d-flex h-100 w-100" >
<div *ngIf="endItemService.endItem$ | async" class="d-flex w-100" style="height: calc(100% - 3.2rem)">
<app-tree></app-tree>
<app-main-center class="w-100"></app-main-center>
</div>
2 changes: 1 addition & 1 deletion src/app/main/tree/tree.component.html
@@ -1,4 +1,4 @@
<p-tree [value]="filesTree" selectionMode="single" (onNodeSelect)="nodeSelect($event)">
<p-tree [value]="filesTree" selectionMode="single" (onNodeSelect)="nodeSelect($event)" (onNodeExpand)="loadNode($event)">
<ng-template let-node pTemplate="default">
<div class="tree">
<div class="flex-fill">{{ node.label }}</div>
Expand Down
68 changes: 65 additions & 3 deletions src/app/main/tree/tree.component.ts
Expand Up @@ -4,6 +4,7 @@ import { Subscription } from 'rxjs';
import { NodeService } from 'src/app/services/node.service';
import { TreeService } from 'src/app/services/tree.service';
import { EndItemService } from 'src/app/services/enditem.service';
import { GeiaService } from 'src/app/services/api/geia.service';

@Component({
selector: 'app-tree',
Expand All @@ -20,20 +21,81 @@ export class TreeComponent implements OnInit, OnDestroy {
constructor(
private treeService: TreeService,
private nodeService: NodeService,
private endItemService: EndItemService
private endItemService: EndItemService,
private geiaService: GeiaService
) { }

ngOnInit() {
this.dropdownSubscription = this.endItemService.endItem$.subscribe(xa => this.selectedEndItem = xa);
this.nodeService.getFiles().then(files => (this.filesTree = files));
this.dropdownSubscription = this.endItemService.endItem$.subscribe(async xa => {
this.selectedEndItem = xa;
const xbs = await this.geiaService.getTopLevelXBs(xa).toPromise();
this.filesTree = this.mapXBsToTree(xbs);
});
// this.nodeService.getFiles().then(files => (this.filesTree = files));
}

nodeSelect(event: { node: TreeNode }) {
this.treeService.changeNode(event.node);
}

async loadNode(event: { node: TreeNode }) {
const data = event.node.data.geia;
let ret = [];
if (data.xbChildren != null) {
const xbs = await this.geiaService.getXBs(this.endItemService.getFullEndItem().endItemAcronymCode, data.lcn).toPromise();
ret = this.mapXBsToTree(xbs);
}
if (data.caChildren != null) {
const cas = await this.geiaService.getCAs(this.endItemService.getFullEndItem().endItemAcronymCode, data.lcn).toPromise();
ret = ret.concat(this.mapCAsToTree(cas));
}
event.node.children = ret;
}

ngOnDestroy() {
this.dropdownSubscription.unsubscribe();
}

mapXBsToTree(xbs: any[]): TreeNode[] {
const lcnStructure = this.endItemService.getFullEndItem().lcnStructure;
return xbs.map(xb => ({
label: `${this.lcnToString(xb.lcn, lcnStructure)} ${xb.lcnNomenclature}`,
leaf: this.isXBLeaf(xb),
data: {
geia: xb,
pdfName: 'radar.pdf'
}
}));
}

mapCAsToTree(cas: any[]): TreeNode[] {
return cas.map(ca => ({
label: `${ca.taskCode} ${ca.taskIdentification}`,
leaf: true,
data: {
geia: ca,
pdfName: 'radar2.pdf'
}
}));
}

isXBLeaf(xb: any): boolean {
return xb.xbChildren == null && xb.caChildren == null;
}

lcnToString(lcn: string, lcnStructure: string) {
const acc = [];
let processed = 0;
for (let i = 0; i < lcnStructure.length; i++) {
const len = Number(lcnStructure.charAt(i));
if (processed < lcn.length) {
acc.push(lcn.substr(processed, processed + len));
processed += len;
} else {
break;
}
}
return acc.join('-');
}

}
11 changes: 9 additions & 2 deletions src/app/services/enditem.service.ts
Expand Up @@ -9,8 +9,15 @@ export class EndItemService {
private selectedEndItem = new BehaviorSubject<string>(null);
endItem$ = this.selectedEndItem.asObservable();

setEndItem(endItem: string) {
this.selectedEndItem.next(endItem);
private fullEndItem: any;

setEndItem(endItem: any) {
this.fullEndItem = endItem;
this.selectedEndItem.next(endItem.endItemAcronymCode);
}

getFullEndItem(): any {
return this.fullEndItem;
}

}
3 changes: 2 additions & 1 deletion src/styles.css
Expand Up @@ -7,5 +7,6 @@ html, body, app-root {

app-tree .ui-tree {
height: 100%;
width: 100%;
width: 25rem;
max-width: 25rem;
}

0 comments on commit 74000d7

Please sign in to comment.