import {
Component,
Input,
OnChanges,
SimpleChanges,
inject,
} from "@angular/core";
import { Entity } from "../../../core/entity/model/entity";
import { FileService } from "../file.service";
import { FaDynamicIconComponent } from "../../../core/common-components/fa-dynamic-icon/fa-dynamic-icon.component";
@Component({
selector: "app-display-img",
templateUrl: "./display-img.component.html",
styleUrls: ["./display-img.component.scss"],
imports: [FaDynamicIconComponent],
})
export class DisplayImgComponent implements OnChanges {
private fileService = inject(FileService);
@Input() defaultImage: string;
@Input() defaultIcon: string;
@Input() entity: Entity;
@Input() imgProperty: string;
imgSrc: string;
ngOnChanges(changes: SimpleChanges) {
if (
changes.hasOwnProperty("entity") ||
changes.hasOwnProperty("property")
) {
delete this.imgSrc;
if (this.entity[this.imgProperty]) {
this.fileService
.loadFile(this.entity, this.imgProperty)
.subscribe((res) => {
// doesn't work with safeUrl
this.imgSrc = Object.values(res)[0];
});
}
}
}
}
@if (imgSrc) {
<img [src]="imgSrc" />
} @else if (!imgSrc && defaultImage) {
<img [src]="defaultImage" />
} @else if (!imgSrc && !defaultImage && defaultIcon) {
<app-fa-dynamic-icon [icon]="defaultIcon"></app-fa-dynamic-icon>
}
/* Inherit the styles from the surrounding component. Otherwise the img is overflowing.
Remove padding and margin so they are not applied a second time. */
img {
all: inherit;
padding: 0;
margin: 0;
}
Legend
Html element with directive