From 8768c26e5a2a8152fec42c0e0e70ad07f1d5e15f Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 18 Jan 2022 20:43:11 +0100 Subject: [PATCH] Change change detection of filecard component Signed-off-by: trivernis --- .../file/file-card/file-card.component.ts | 35 +++++++++++++------ .../file-thumbnail.component.html | 10 +++--- .../file-thumbnail.component.ts | 8 ++++- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/mediarepo-ui/src/app/components/shared/file/file-card/file-card.component.ts b/mediarepo-ui/src/app/components/shared/file/file-card/file-card.component.ts index e8064c8..90bb492 100644 --- a/mediarepo-ui/src/app/components/shared/file/file-card/file-card.component.ts +++ b/mediarepo-ui/src/app/components/shared/file/file-card/file-card.component.ts @@ -1,4 +1,7 @@ import { + AfterViewChecked, + ChangeDetectionStrategy, + ChangeDetectorRef, Component, ElementRef, EventEmitter, @@ -12,42 +15,49 @@ import { } from "@angular/core"; import {File} from "../../../../../api/models/File"; import {Selectable} from "../../../../models/Selectable"; -import { - SchedulingService -} from "../../../../services/scheduling/scheduling.service"; +import {SchedulingService} from "../../../../services/scheduling/scheduling.service"; const LOADING_WORK_KEY = "FILE_THUMBNAIL_LOADING"; @Component({ selector: "app-file-card", templateUrl: "./file-card.component.html", - styleUrls: ["./file-card.component.scss"] + styleUrls: ["./file-card.component.scss"], + changeDetection: ChangeDetectionStrategy.OnPush }) -export class FileCardComponent implements OnInit, OnChanges, OnDestroy { +export class FileCardComponent implements OnInit, OnChanges, OnDestroy, AfterViewChecked { @ViewChild("card") card!: ElementRef; @Input() public entry!: Selectable; @Output() clickEvent = new EventEmitter(); @Output() dblClickEvent = new EventEmitter(); - + public loading = false; private cachedId: number | undefined; private workId: number | undefined; - public loading = false; + private selectedPrevious = false; - constructor(private schedulingService: SchedulingService) { + constructor(private changeDetector: ChangeDetectorRef, private schedulingService: SchedulingService) { } async ngOnInit() { this.cachedId = this.entry.data.id; + this.selectedPrevious = this.entry.selected; this.setImageDelayed(); } async ngOnChanges(changes: SimpleChanges) { - if (changes["file"] && (this.cachedId === undefined || this.entry.data.id !== this.cachedId)) { + if (changes["entry"] && (this.cachedId === undefined || this.entry.data.id !== this.cachedId)) { this.cachedId = this.entry.data.id; this.setImageDelayed(); } } + public ngAfterViewChecked(): void { + if (this.entry.selected != this.selectedPrevious) { + this.selectedPrevious = this.entry.selected; + this.changeDetector.markForCheck(); + } + } + public ngOnDestroy(): void { if (this.workId) { this.schedulingService.cancelWork(LOADING_WORK_KEY, this.workId); @@ -59,10 +69,13 @@ export class FileCardComponent implements OnInit, OnChanges, OnDestroy { this.schedulingService.cancelWork(LOADING_WORK_KEY, this.workId); } this.loading = true; - this.workId = this.schedulingService.addWork(LOADING_WORK_KEY, + this.workId = this.schedulingService.addWork( + LOADING_WORK_KEY, async () => { await this.schedulingService.delay(1); this.loading = false; - }); + this.changeDetector.markForCheck(); + } + ); } } diff --git a/mediarepo-ui/src/app/components/shared/file/file-thumbnail/file-thumbnail.component.html b/mediarepo-ui/src/app/components/shared/file/file-thumbnail/file-thumbnail.component.html index b171b71..3a0e1f9 100644 --- a/mediarepo-ui/src/app/components/shared/file/file-thumbnail/file-thumbnail.component.html +++ b/mediarepo-ui/src/app/components/shared/file/file-thumbnail/file-thumbnail.component.html @@ -1,14 +1,14 @@
- +
- - - - + + + +
diff --git a/mediarepo-ui/src/app/components/shared/file/file-thumbnail/file-thumbnail.component.ts b/mediarepo-ui/src/app/components/shared/file/file-thumbnail/file-thumbnail.component.ts index dbf6147..74d7808 100644 --- a/mediarepo-ui/src/app/components/shared/file/file-thumbnail/file-thumbnail.component.ts +++ b/mediarepo-ui/src/app/components/shared/file/file-thumbnail/file-thumbnail.component.ts @@ -14,6 +14,8 @@ export class FileThumbnailComponent implements OnChanges, AfterViewInit { @Input() file!: File; public thumbUrl: SafeResourceUrl | undefined; + public fileType!: string; + public thumbnailSupported: boolean = false; private supportedThumbnailTypes = ["image", "video"]; @@ -21,7 +23,9 @@ export class FileThumbnailComponent implements OnChanges, AfterViewInit { } public async ngAfterViewInit() { - this.thumbUrl = this.fileService.buildThumbnailUrl(this.file, 250, 250); + if (this.thumbnailSupported) { + this.thumbUrl = this.fileService.buildThumbnailUrl(this.file, 250, 250); + } } public async ngOnChanges(changes: SimpleChanges) { @@ -29,6 +33,8 @@ export class FileThumbnailComponent implements OnChanges, AfterViewInit { this.thumbUrl = this.fileService.buildThumbnailUrl(this.file, 250, 250 ); + this.fileType = this.getFileType(); + this.thumbnailSupported = this.getThumbnailSupported(); } }