File

src/app/features/default-value-inherited/inherited-value-button/inherited-value-button.component.ts

Description

Display an indicator for form fields explaining the status of the inherited-value config of that field and allowing users to re-sync the inherited value manually.

Implements

OnChanges

Metadata

Index

Properties
Inputs

Constructor

constructor(defaultValueService: DefaultValueService)
Parameters :
Name Type Optional
defaultValueService DefaultValueService No

Inputs

entity
Type : Entity
field
Type : FormFieldConfig
form
Type : EntityForm<any>

Properties

defaultValueHint
Type : DefaultValueHint | undefined
import { Component, Input, OnChanges, SimpleChanges } from "@angular/core";
import { EntityForm } from "../../../core/common-components/entity-form/entity-form.service";
import { EntityFieldLabelComponent } from "../../../core/common-components/entity-field-label/entity-field-label.component";
import { FaIconComponent } from "@fortawesome/angular-fontawesome";
import { TemplateTooltipDirective } from "../../../core/common-components/template-tooltip/template-tooltip.directive";
import { Entity } from "../../../core/entity/model/entity";
import {
  DefaultValueHint,
  DefaultValueService,
} from "../../../core/default-values/default-value-service/default-value.service";
import { FormFieldConfig } from "../../../core/common-components/entity-form/FormConfig";
import { MatIconButton } from "@angular/material/button";

/**
 * Display an indicator for form fields explaining the status of the inherited-value config of that field
 * and allowing users to re-sync the inherited value manually.
 */
@Component({
  selector: "app-inherited-value-button",
  imports: [
    EntityFieldLabelComponent,
    FaIconComponent,
    TemplateTooltipDirective,
    MatIconButton,
  ],
  templateUrl: "./inherited-value-button.component.html",
  styleUrl: "./inherited-value-button.component.scss",
})
export class InheritedValueButtonComponent implements OnChanges {
  @Input() form: EntityForm<any>;
  @Input() field: FormFieldConfig;
  @Input() entity: Entity;

  defaultValueHint: DefaultValueHint | undefined;

  constructor(private defaultValueService: DefaultValueService) {}

  ngOnChanges(changes: SimpleChanges): void {
    this.defaultValueHint = this.defaultValueService.getDefaultValueUiHint(
      this.form,
      this.field?.id,
    );

    if (changes.form && changes.form.firstChange) {
      this.form?.formGroup.valueChanges.subscribe((value) =>
        // ensure this is only called after the other changes handler
        setTimeout(
          () =>
            (this.defaultValueHint =
              this.defaultValueService.getDefaultValueUiHint(
                this.form,
                this.field?.id,
              )),
        ),
      );
    }
  }
}
@if (defaultValueHint) {
  <div [appTemplateTooltip]="tooltip" class="inline-box">
    <button
      mat-icon-button
      type="button"
      [disabled]="
        form.formGroup.disabled ||
        defaultValueHint.isInSync ||
        defaultValueHint.isEmpty
      "
      (click)="defaultValueHint.syncFromParentField()"
    >
      <fa-icon
        [icon]="defaultValueHint.isInSync ? 'link' : 'link-slash'"
      ></fa-icon>
    </button>
  </div>
}

<ng-template #tooltip>
  <div class="field-hint">
    <div i18n>
      Inherited value from parent record selected in field
      "<app-entity-field-label
        [field]="defaultValueHint.inheritedFromField"
        [entityType]="entity.getConstructor()"
      ></app-entity-field-label
      >"
    </div>

    <br />

    @if (defaultValueHint.isEmpty) {
      <div i18n>
        Select exactly one record in the parent field to automatically inherit
        its value here.
      </div>
    } @else if (defaultValueHint.isInSync) {
      <div i18n>(up-to-date, inherited from parent record)</div>
    } @else {
      <div i18n>
        (manually overwritten): click to reset value to inherited from parent
        record
        @if (!form.formGroup.enabled) {
          (requires the form to be in "Edit" mode)
        }
      </div>
      <div i18n></div>
    }
  </div>
</ng-template>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""