Skip to content
This repository was archived by the owner on Mar 14, 2023. It is now read-only.

Commit 007ac7b

Browse files
author
clouless
committed
numbered headlines and router reuse strategy
1 parent e4c3d1a commit 007ac7b

File tree

9 files changed

+128
-17
lines changed

9 files changed

+128
-17
lines changed

src/app/app.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="container">
2-
<div class="logo">
2+
<a class="logo" routerLink="/">
33
<img [src]="logoSrc" class="logo-img" />
4-
</div>
4+
</a>
55
<router-outlet></router-outlet>
66
</div>

src/app/app.module.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { HttpClientModule } from '@angular/common/http';
55

66
import { AppComponent } from './app.component';
77
import { UploadPageComponent } from './upload-page/upload-page.component';
8-
import { RouterModule, Routes } from '@angular/router';
9-
import { BackendService } from './backend.service';
8+
import { RouterModule, Routes, RouteReuseStrategy } from '@angular/router';
9+
import { BackendService } from './services/backend.service';
1010
import { TerminalOutputComponent } from './terminal-output/terminal-output.component';
1111
import { SpinnerComponent } from './stateless/spinner.component';
12+
import { NumberedHeadlineComponent } from './stateless/numbered-headline.component';
13+
import { ForceRouteReuseStrategy } from './stateless/force-route-reuse-strategy';
1214

1315
const appRoutes: Routes = [
1416
{ path: 'upload', component: UploadPageComponent },
@@ -25,15 +27,22 @@ const appRoutes: Routes = [
2527
AppComponent,
2628
UploadPageComponent,
2729
TerminalOutputComponent,
28-
SpinnerComponent
30+
SpinnerComponent,
31+
NumberedHeadlineComponent,
2932
],
3033
imports: [
3134
RouterModule.forRoot(appRoutes, { useHash: true }),
3235
BrowserModule,
3336
FormsModule,
3437
HttpClientModule
3538
],
36-
providers: [BackendService],
39+
providers: [
40+
BackendService,
41+
{
42+
provide: RouteReuseStrategy,
43+
useClass: ForceRouteReuseStrategy
44+
},
45+
],
3746
bootstrap: [AppComponent]
3847
})
3948
export class AppModule { }
File renamed without changes.

src/app/backend.service.ts renamed to src/app/services/backend.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
KartoffelstampfImageUploadResponse,
55
KartoffelstampfImageUploadRequest,
66
KartoffelstampfCompressInstruction,
7-
} from './types/kartoffelstampf-server';
7+
} from '../types/kartoffelstampf-server';
88
import { Observable, Subject, throwError } from 'rxjs';
99
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
1010
import { catchError } from 'rxjs/operators';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle } from '@angular/router';
2+
3+
export class ForceRouteReuseStrategy implements RouteReuseStrategy {
4+
shouldDetach(route: ActivatedRouteSnapshot): boolean { return false; }
5+
store(route: ActivatedRouteSnapshot, detachedTree: DetachedRouteHandle): void {}
6+
shouldAttach(route: ActivatedRouteSnapshot): boolean { return false; }
7+
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle|null { return null; }
8+
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
9+
// Always re-render route!
10+
return false;
11+
}
12+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Component } from '@angular/core';
2+
import { Input } from '@angular/core';
3+
4+
@Component({
5+
selector: 'app-numbered-headline',
6+
template: `
7+
<div class="numbered-headline">
8+
<div
9+
class="numbered-headline-number"
10+
[class.numbered-headline-number---is-active]="isActive"
11+
><span>{{ number }}</span></div>
12+
<div
13+
class="numbered-headline-text"
14+
[class.numbered-headline-text---is-active]="isActive"
15+
><span>{{ headline }}</span></div>
16+
</div>`,
17+
styles: [
18+
`.numbered-headline {
19+
display:flex;
20+
flex-direction: row;
21+
justify-content: left;
22+
align-items: center;
23+
margin-bottom: 15px;
24+
margin-top: 15px;
25+
}`,
26+
`.numbered-headline-number {
27+
width: 25px;
28+
height: 25px;
29+
display: flex;
30+
align-items: center;
31+
justify-content: center;
32+
background-color: #777;
33+
color:#fff;
34+
border-radius: 100%;
35+
font-size:20px;
36+
}`,
37+
`.numbered-headline-number---is-active {
38+
background-color: #00A200;
39+
}`,
40+
`.numbered-headline-text {
41+
color: #ccc;
42+
margin-left:10px;
43+
display: flex;
44+
align-items: flex-start;
45+
justify-content: center;
46+
flex-direction: column;
47+
}`,
48+
`.numbered-headline-text---is-active {
49+
color: #777;
50+
}`,
51+
]
52+
})
53+
export class NumberedHeadlineComponent {
54+
55+
@Input()
56+
isActive: boolean;
57+
58+
@Input()
59+
number: number;
60+
61+
@Input()
62+
headline: string;
63+
}

src/app/stateless/spinner.component.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
import { Component } from '@angular/core';
42

53
@Component({

src/app/upload-page/upload-page.component.html

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<div class="container">
2+
<app-numbered-headline
3+
number="1"
4+
headline="Drop Images and Upload"
5+
[isActive]="activeStep == 1"
6+
></app-numbered-headline>
27
<div
8+
*ngIf="activeStep == 1"
39
class="drop-container"
410
[class.drop-container---drag-over]="uiStateDragOver"
511
[class.drop-container---drag-leave]="uiStateDragLeave"
@@ -8,13 +14,18 @@
814
(dragover)="handleDragOver($event)"
915
(dragleave)="handleDragLeave($event)"
1016
>
11-
1217
<input
1318
type="file"
1419
multiple="multiple"
1520
(change)="handleFileChange($event)"
1621
>
1722
</div>
23+
<!-- ========== -->
24+
<app-numbered-headline
25+
number="2"
26+
headline="Compress and Download"
27+
[isActive]="activeStep == 2"
28+
></app-numbered-headline>
1829
<app-spinner *ngIf="originalFileName && !compressDone!"></app-spinner>
1930
<div *ngIf="originalFileName">
2031
<h3>{{originalFileName}}</h3>

src/app/upload-page/upload-page.component.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Component, OnInit } from '@angular/core';
2-
import { BackendService } from '../backend.service';
1+
import { Component, OnInit, OnDestroy } from '@angular/core';
2+
import { BackendService } from '../services/backend.service';
33
import { KartoffelstampfTerminalOutputEntry, KartoffelstampfCompressInstruction } from '../types/kartoffelstampf-server';
44
import { TerminalLine } from '../types/kartoffelstampf-client';
5-
import { finalize } from 'rxjs/operators';
5+
import { finalize, takeUntil } from 'rxjs/operators';
6+
import { Subject } from 'rxjs';
67

78
@Component({
89
selector: 'app-upload-page',
@@ -22,8 +23,9 @@ import { finalize } from 'rxjs/operators';
2223
],
2324
providers: [BackendService]
2425
})
25-
export class UploadPageComponent implements OnInit {
26+
export class UploadPageComponent implements OnInit, OnDestroy {
2627

28+
preDestroy = new Subject<boolean>();
2729
terminalLines: TerminalLine[] = [];
2830
uploadedFileBase64URI: string;
2931
originalFileName: string;
@@ -34,9 +36,19 @@ export class UploadPageComponent implements OnInit {
3436
uiStateDragOver = false;
3537
uiStateDragLeave = true;
3638

39+
activeStep = 1;
40+
3741
constructor(private backendService: BackendService) { }
3842

39-
ngOnInit() { }
43+
ngOnInit() {
44+
// since router strategy is to forcefully NOT reuse page components.
45+
// The Component is reiniated automatically on browse to /upload
46+
}
47+
48+
ngOnDestroy() {
49+
this.preDestroy.next(true);
50+
this.preDestroy.complete();
51+
}
4052

4153
handleDrop(event: any) {
4254
this.uiStateDragOver = false;
@@ -77,14 +89,19 @@ export class UploadPageComponent implements OnInit {
7789
fileReader.addEventListener('load', function(loadedEvent: any) {
7890
self.uploadedFileBase64URI = loadedEvent.target.result;
7991
// Upload via backend
80-
self.backendService.uploadImage(self.uploadedFileBase64URI, 'PNG')
92+
self.backendService
93+
.uploadImage(self.uploadedFileBase64URI, 'PNG')
94+
.pipe(
95+
takeUntil(self.preDestroy)
96+
)
8197
.subscribe(uploadResponse => {
8298
console.log(uploadResponse.fileName);
8399
self.temporaryFileName = uploadResponse.fileName;
84100
self.runCompressCommand();
85101
});
86102
});
87103
fileReader.readAsDataURL(files[0]);
104+
self.activeStep = 2;
88105
}
89106
}
90107

@@ -102,7 +119,8 @@ export class UploadPageComponent implements OnInit {
102119
finalize(() => {
103120
console.log('compress-done!');
104121
self.compressDone = true;
105-
})
122+
}),
123+
takeUntil(self.preDestroy)
106124
)
107125
.subscribe(data => {
108126
const terminalLine = new TerminalLine(data);

0 commit comments

Comments
 (0)