Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement PDF rendering based on tree node data.
  • Loading branch information
dds14002 committed Feb 17, 2020
1 parent b3dc6df commit 20d2167
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 15 deletions.
17 changes: 13 additions & 4 deletions src/app/main/main-center/main-center.component.html
@@ -1,8 +1,17 @@

<ng-container *ngIf="this.currentNode != null; else noNode">
<p>Current node is {{this.currentNode.label}}</p>
<!-- Add pdf container here -->
<div class="d-flex h-100 flex-column">
<h1 class="ml-2">{{this.currentNode.label}}</h1>
<ng-container *ngIf="!this.notFound; else noPdf">
<embed *ngIf="this.blobUrl != null" [src]="blobUrl" type="application/pdf" width="100%" height="100%" />
</ng-container>
</div>
</ng-container>

<ng-template #noNode>
<p>No node selected</p>
</ng-template>
<h1 class="ml-2">No node selected</h1>
</ng-template>

<ng-template #noPdf>
<h1 class="ml-4 mt-4 text-danger">PDF not found</h1>
</ng-template>
17 changes: 10 additions & 7 deletions src/app/main/main-center/main-center.component.ts
Expand Up @@ -4,6 +4,7 @@ import { Subscription } from 'rxjs';
import { TreeNode } from 'primeng/api';
import { ResourceService } from 'src/app/services/api/resource.service';
import { HttpErrorResponse } from '@angular/common/http';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';

@Component({
selector: 'app-main-center',
Expand All @@ -14,32 +15,34 @@ export class MainCenterComponent implements OnInit, OnDestroy {

private treeSubscription: Subscription;
currentNode: TreeNode;
blobUrl: SafeResourceUrl;
notFound = false;

constructor(
private treeService: TreeService,
private resourceService: ResourceService
private resourceService: ResourceService,
private domSanitizer: DomSanitizer
) { }

ngOnInit() {
this.treeSubscription = this.treeService.node$.subscribe(async (node) => {
if (node == null) {
return;
}
this.notFound = false;
console.log('Selected node is', node);
this.currentNode = node;
const data = node.data as { pdfName: string };
try {
console.log('I will load', data.pdfName);
const pdfData = await this.resourceService.getResourcePDF(data.pdfName);
// render pdf to screen

const pdfData = await this.resourceService.getResourcePDF(data.pdfName).toPromise();
this.blobUrl = this.domSanitizer.bypassSecurityTrustResourceUrl(
URL.createObjectURL(new Blob([pdfData], { type: 'application/pdf' })));
} catch (err) {
console.log(err);
if ((err as HttpErrorResponse).status === 404) {
// handle not found
this.notFound = true;
}
}

});
}

Expand Down
6 changes: 4 additions & 2 deletions src/app/main/main.component.html
@@ -1,2 +1,4 @@
<app-tree></app-tree>
<app-main-center></app-main-center>
<div class="d-flex h-100 w-100">
<app-tree></app-tree>
<app-main-center class="w-100"></app-main-center>
</div>
5 changes: 5 additions & 0 deletions src/app/main/tree/tree.component.css
@@ -0,0 +1,5 @@
.tree {
height: 100%;
display: flex;
justify-content: space-between;
}
8 changes: 7 additions & 1 deletion src/app/main/tree/tree.component.html
@@ -1 +1,7 @@
<p-tree [value]="filesTree" selectionMode="single" (onNodeSelect)="nodeSelect($event)"></p-tree>
<p-tree [value]="filesTree" selectionMode="single" (onNodeSelect)="nodeSelect($event)">
<ng-template let-node pTemplate="default">
<div class="tree">
<div class="flex-fill">{{ node.label }}</div>
</div>
</ng-template>
</p-tree>
2 changes: 1 addition & 1 deletion src/assets/files.json
Expand Up @@ -12,7 +12,7 @@
{
"label": "32-11 Main Landing Gear",
"data": {
"pdfName": "radar.pdf"
"pdfName": "radar2.pdf"
},
"expandedIcon": "fa fa-folder-open",
"collapsedIcon": "fa fa-folder",
Expand Down
10 changes: 10 additions & 0 deletions src/styles.css
@@ -1 +1,11 @@
/* You can add global styles to this file, and also import other style files */
html, body, app-root {
height: 100%;
width: 100%;
margin: 0;
}

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

0 comments on commit 20d2167

Please sign in to comment.