File

src/app/core/basic-datatypes/entity/entity-block/entity-block.component.ts

Description

Display an inline block representing an entity.

Implements

OnInit

Metadata

Index

Properties
Methods
Inputs

Constructor

constructor(entityMapper: EntityMapperService, router: Router)
Parameters :
Name Type Optional
entityMapper EntityMapperService No
router Router No

Inputs

entity
Type : Entity
entityId
Type : string

If entityToDisplay is not set, entityId with prefix required to load the entity If entityToDisplay is set, this input is ignored

linkDisabled
Type : boolean
Default value : false

Methods

showDetailsPage
showDetailsPage()
Returns : void

Properties

entityBlockConfig
Type : EntityBlockConfig
entityIcon
Type : string
import { Component, Input, OnInit } from "@angular/core";
import { Entity } from "../../../entity/model/entity";
import { EntityMapperService } from "../../../entity/entity-mapper/entity-mapper.service";
import { Router } from "@angular/router";
import { NgClass } from "@angular/common";
import { Logging } from "../../../logging/logging.service";
import { FaDynamicIconComponent } from "../../../common-components/fa-dynamic-icon/fa-dynamic-icon.component";
import { TemplateTooltipDirective } from "../../../common-components/template-tooltip/template-tooltip.directive";
import { DisplayImgComponent } from "../../../../features/file/display-img/display-img.component";
import { EntityBlockConfig } from "./entity-block-config";
import { EntityFieldViewComponent } from "../../../common-components/entity-field-view/entity-field-view.component";
import { DynamicComponent } from "../../../config/dynamic-components/dynamic-component.decorator";

/**
 * Display an inline block representing an entity.
 */
@DynamicComponent("EntityBlock")
@Component({
  selector: "app-entity-block",
  templateUrl: "./entity-block.component.html",
  styleUrls: ["./entity-block.component.scss"],
  imports: [
    NgClass,
    FaDynamicIconComponent,
    TemplateTooltipDirective,
    DisplayImgComponent,
    EntityFieldViewComponent,
  ],
})
export class EntityBlockComponent implements OnInit {
  @Input() entity: Entity;
  @Input() linkDisabled = false;

  /**
   * If `entityToDisplay` is not set, `entityId` with prefix required to load the entity
   * If `entityToDisplay` is set, this input is ignored
   */
  @Input() entityId: string;

  entityBlockConfig: EntityBlockConfig;
  entityIcon: string;

  constructor(
    private entityMapper: EntityMapperService,
    private router: Router,
  ) {}

  async ngOnInit() {
    if (!this.entity) {
      await this.loadEntity();
    }

    this.initDisplayDetails();
  }

  private async loadEntity() {
    if (!this.entityId) {
      return;
    }

    try {
      this.entity = await this.entityMapper.load(
        Entity.extractTypeFromId(this.entityId),
        this.entityId,
      );
    } catch (e) {
      // this may be caused by restrictive permissions and therefore shouldn't be treated as a technical issue
      Logging.debug(
        "[DISPLAY_ENTITY] Could not find entity.",
        this.entityId,
        e,
      );
    }
  }

  private initDisplayDetails() {
    if (!this.entity) {
      return;
    }

    const entityType = this.entity.getConstructor();

    this.entityBlockConfig = entityType.toBlockDetailsAttributes;
    this.entityIcon = entityType.icon;
  }

  showDetailsPage() {
    if (this.linkDisabled) {
      return;
    }

    this.router.navigate([
      this.entity.getConstructor().route,
      this.entity.getId(true),
    ]);
  }
}
<span
  class="block-height truncate-text"
  [ngClass]="{
    clickable: !linkDisabled,
    '.inactive': !entity?.isActive,
  }"
  (click)="showDetailsPage()"
  [appTemplateTooltip]="tooltip"
  [tooltipDisabled]="!entityBlockConfig"
>
  @if (entityIcon) {
    <app-fa-dynamic-icon
      [icon]="entityIcon"
      class="margin-right-small"
    ></app-fa-dynamic-icon>
  }

  {{ entity?.toString() }}
</span>

<!-- Tooltip on Hover -->
<ng-template #tooltip>
  <div class="tooltip-container">
    @if (entityBlockConfig?.image) {
      <app-display-img
        class="tooltip-photo"
        [entity]="entity"
        [imgProperty]="entityBlockConfig?.image"
      ></app-display-img>
    }

    <div>
      <!-- Font-weight is applied as style to override the default -->
      <h3 style="font-weight: bold">
        <app-entity-field-view
          [entity]="entity"
          [field]="entityBlockConfig?.title"
        ></app-entity-field-view>
      </h3>

      @for (field of entityBlockConfig?.fields; track field) {
        <app-entity-field-view
          [entity]="entity"
          [field]="field"
        ></app-entity-field-view>
      }
    </div>
  </div>
</ng-template>

./entity-block.component.scss

@use "variables/sizes";
@use "variables/colors";

.block-height {
  line-height: sizes.$icon-block;
}

.inactive {
  color: colors.$inactive;
}

.tooltip-container {
  padding: 0.5em;
  display: flex;
  flex-direction: row;
  gap: 2em;
  align-items: center;
}

.tooltip-photo ::ng-deep img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  overflow: hidden;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""