src/app/features/file/display-img/display-img.component.ts

Metadata

Relationships

Index

Properties
Inputs

Inputs

defaultIcon
Type : string
defaultImage
Type : string
entity
Type : Entity
imgProperty
Type : string

Properties

imgSrc
Type : unknown
Default value : resource({ params: () => { const entity = this.entity(); const imgProperty = this.imgProperty(); const value = entity?.[imgProperty]; if (!value) return undefined; return { entity: entity!, imgProperty, value }; }, loader: async ({ params: { entity, imgProperty, value } }) => { if (this.isRemoteUrl(value)) return value; // doesn't work with safeUrl return firstValueFrom( this.fileService .loadFile(entity, imgProperty) .pipe(map((res) => Object.values(res)[0] as string)), ); }, })
import {
  Component,
  inject,
  ChangeDetectionStrategy,
  input,
  resource,
} from "@angular/core";
import { firstValueFrom } from "rxjs";
import { map } from "rxjs/operators";
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({
  changeDetection: ChangeDetectionStrategy.OnPush,
  selector: "app-display-img",
  templateUrl: "./display-img.component.html",
  styleUrls: ["./display-img.component.scss"],
  imports: [FaDynamicIconComponent],
})
export class DisplayImgComponent {
  private fileService = inject(FileService);

  defaultImage = input<string>();
  defaultIcon = input<string>();
  entity = input<Entity>();
  imgProperty = input<string>();

  imgSrc = resource({
    params: () => {
      const entity = this.entity();
      const imgProperty = this.imgProperty();
      const value = entity?.[imgProperty];
      if (!value) return undefined;
      return { entity: entity!, imgProperty, value };
    },
    loader: async ({ params: { entity, imgProperty, value } }) => {
      if (this.isRemoteUrl(value)) return value;
      // doesn't work with safeUrl
      return firstValueFrom(
        this.fileService
          .loadFile(entity, imgProperty)
          .pipe(map((res) => Object.values(res)[0] as string)),
      );
    },
  });

  private isRemoteUrl(value: string): boolean {
    return value.startsWith("https://");
  }
}
@if (imgSrc.value()) {
  <img [src]="imgSrc.value()" />
} @else if (!imgSrc.value() && defaultImage()) {
  <img [src]="defaultImage()" />
} @else if (!imgSrc.value() && !defaultImage() && defaultIcon()) {
  <app-fa-dynamic-icon [icon]="defaultIcon()"></app-fa-dynamic-icon>
}

./display-img.component.scss

/* 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
Component
Html element with directive

results matching ""

    No results matching ""