|
|
@ -4,9 +4,11 @@ import {
|
|
|
|
Component,
|
|
|
|
Component,
|
|
|
|
DoCheck,
|
|
|
|
DoCheck,
|
|
|
|
ElementRef,
|
|
|
|
ElementRef,
|
|
|
|
|
|
|
|
EventEmitter,
|
|
|
|
Input,
|
|
|
|
Input,
|
|
|
|
OnDestroy,
|
|
|
|
OnDestroy,
|
|
|
|
OnInit,
|
|
|
|
OnInit,
|
|
|
|
|
|
|
|
Output,
|
|
|
|
ViewChild
|
|
|
|
ViewChild
|
|
|
|
} from "@angular/core";
|
|
|
|
} from "@angular/core";
|
|
|
|
import {SafeResourceUrl} from "@angular/platform-browser";
|
|
|
|
import {SafeResourceUrl} from "@angular/platform-browser";
|
|
|
@ -23,11 +25,17 @@ export class ContentAwareImageComponent implements OnInit, DoCheck, OnDestroy {
|
|
|
|
@Input() maximizeWidth: boolean = true;
|
|
|
|
@Input() maximizeWidth: boolean = true;
|
|
|
|
@Input() borderRadius: string | undefined;
|
|
|
|
@Input() borderRadius: string | undefined;
|
|
|
|
@Input() decoding: "async" | "sync" | "auto" = "auto";
|
|
|
|
@Input() decoding: "async" | "sync" | "auto" = "auto";
|
|
|
|
@ViewChild("image") image?: ElementRef<HTMLImageElement>;
|
|
|
|
@Input() maxRetry = 3;
|
|
|
|
|
|
|
|
@Input() retryDelay = 200;
|
|
|
|
|
|
|
|
@ViewChild("image") imageElement?: ElementRef<HTMLImageElement>;
|
|
|
|
@ViewChild("imageContainer") imageContainer?: ElementRef<HTMLDivElement>;
|
|
|
|
@ViewChild("imageContainer") imageContainer?: ElementRef<HTMLDivElement>;
|
|
|
|
|
|
|
|
@Output() appLoadEnd = new EventEmitter<void>();
|
|
|
|
|
|
|
|
@Output() appLoadError = new EventEmitter<void>();
|
|
|
|
|
|
|
|
|
|
|
|
scaleWidth = false;
|
|
|
|
scaleWidth = false;
|
|
|
|
private previousHeight = 0;
|
|
|
|
private previousHeight = 0;
|
|
|
|
private previousWidth = 0;
|
|
|
|
private previousWidth = 0;
|
|
|
|
|
|
|
|
private retryCount = 0;
|
|
|
|
private readonly checkInterval?: number;
|
|
|
|
private readonly checkInterval?: number;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(private changeDetector: ChangeDetectorRef) {
|
|
|
|
constructor(private changeDetector: ChangeDetectorRef) {
|
|
|
@ -35,8 +43,8 @@ export class ContentAwareImageComponent implements OnInit, DoCheck, OnDestroy {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ngOnInit(): void {
|
|
|
|
public ngOnInit(): void {
|
|
|
|
if (this.image) {
|
|
|
|
if (this.imageElement) {
|
|
|
|
this.image.nativeElement.decoding = this.decoding;
|
|
|
|
this.imageElement.nativeElement.decoding = this.decoding;
|
|
|
|
this.changeDetector.detach();
|
|
|
|
this.changeDetector.detach();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -50,11 +58,16 @@ export class ContentAwareImageComponent implements OnInit, DoCheck, OnDestroy {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public checkSize(): void {
|
|
|
|
public checkSize(): void {
|
|
|
|
if (this.image?.nativeElement && this.imageContainer?.nativeElement) {
|
|
|
|
if (this.imageElement?.nativeElement && this.imageContainer?.nativeElement) {
|
|
|
|
this.adjustSize(this.image.nativeElement, this.imageContainer.nativeElement);
|
|
|
|
this.adjustSize(this.imageElement.nativeElement, this.imageContainer.nativeElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public onImageLoad(image: HTMLImageElement, imageContainer: HTMLDivElement): void {
|
|
|
|
|
|
|
|
this.adjustSize(image, imageContainer);
|
|
|
|
|
|
|
|
this.appLoadEnd.emit();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Fits the image into the container
|
|
|
|
* Fits the image into the container
|
|
|
|
* @param {HTMLImageElement} image
|
|
|
|
* @param {HTMLImageElement} image
|
|
|
@ -77,4 +90,17 @@ export class ContentAwareImageComponent implements OnInit, DoCheck, OnDestroy {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public onImageLoadError(error: ErrorEvent, image: HTMLImageElement): void {
|
|
|
|
|
|
|
|
const imageSrc = image.src;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.retryCount < this.maxRetry) {
|
|
|
|
|
|
|
|
this.retryCount++;
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
|
|
image.src = imageSrc;
|
|
|
|
|
|
|
|
}, this.retryDelay * this.retryCount);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.appLoadError.emit();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|