src/app/core/common-components/condition-editor-dialog/condition-editor-dialog.component.ts

Description

Dialog around the ConditionsEditorComponent: it says what the condition applies to and hands back the edited Mango-query condition.

Configured via ConditionEditorDialogData

Closes with the new condition, null to remove it, or undefined when cancelled.

Example

Metadata

Relationships

Used by

No results matching.

Depends on

Index

Properties
Methods

Methods

apply
apply()
Returns : void
cancel
cancel()
Returns : void
onConditionsChange
onConditionsChange(conditions: any)
Parameters :
Name Type Optional
conditions any No
Returns : void
removeCondition
removeCondition()
Returns : void

Properties

Readonly data
Type : ConditionEditorDialogData
Default value : inject(MAT_DIALOG_DATA)
Readonly hadCondition
Type : unknown
Default value : !!( this.data.conditions && Object.keys(this.data.conditions).length > 0 )
import { ChangeDetectionStrategy, Component, inject } from "@angular/core";
import { MatButtonModule } from "@angular/material/button";
import {
  MAT_DIALOG_DATA,
  MatDialogModule,
  MatDialogRef,
} from "@angular/material/dialog";

import { ConditionsEditorComponent } from "../conditions-editor/conditions-editor.component";
import { normalizeConditions } from "../conditions-editor/conditions-combinator";
import { DialogCloseComponent } from "../dialog-close/dialog-close.component";
import { EntityConstructor } from "../../entity/model/entity";

export interface ConditionEditorDialogData {
  /** entity type whose fields can be selected in condition rows */
  entityConstructor: EntityConstructor | undefined;

  /** existing condition to edit, if any */
  conditions?: any;

  /** fully localized sentence shown above the conditions editor, explaining what the condition applies to */
  explanation: string;

  /** also offer the internal "_id" field in the field dropdown (e.g. for permission conditions) */
  showInternalIdField?: boolean;
}

/**
 * Dialog around the {@link ConditionsEditorComponent}: it says what the condition applies to and
 * hands back the edited Mango-query condition.
 *
 * Configured via {@link ConditionEditorDialogData}
 *
 * Closes with the new condition, `null` to remove it, or `undefined` when cancelled.
 */
@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  selector: "app-condition-editor-dialog",
  imports: [
    MatDialogModule,
    MatButtonModule,
    ConditionsEditorComponent,
    DialogCloseComponent,
  ],
  templateUrl: "./condition-editor-dialog.component.html",
})
export class ConditionEditorDialogComponent {
  private readonly dialogRef = inject(
    MatDialogRef<ConditionEditorDialogComponent>,
  );

  readonly data: ConditionEditorDialogData = inject(MAT_DIALOG_DATA);

  readonly hadCondition = !!(
    this.data.conditions && Object.keys(this.data.conditions).length > 0
  );

  /** the condition as the editor last reported it, initially the existing one */
  private conditions: any = normalizeConditions(this.data.conditions);

  onConditionsChange(conditions: any) {
    this.conditions = conditions;
  }

  apply() {
    this.dialogRef.close(
      Object.keys(this.conditions).length > 0 ? this.conditions : null,
    );
  }

  removeCondition() {
    this.dialogRef.close(null);
  }

  cancel() {
    this.dialogRef.close(undefined);
  }
}
<h2 mat-dialog-title>
  @if (hadCondition) {
    <span i18n>Edit Condition</span>
  } @else {
    <span i18n>Add Condition</span>
  }
  <app-dialog-close mat-dialog-close></app-dialog-close>
</h2>

<mat-dialog-content>
  <p>{{ data.explanation }}</p>

  <app-conditions-editor
    [conditions]="data.conditions"
    [entityConstructor]="data.entityConstructor"
    [showInternalIdField]="!!data.showInternalIdField"
    (conditionsChange)="onConditionsChange($event)"
  ></app-conditions-editor>
</mat-dialog-content>

<mat-dialog-actions align="end">
  @if (hadCondition) {
    <button mat-button color="warn" (click)="removeCondition()" i18n>
      Remove condition
    </button>
  }
  <button mat-stroked-button (click)="cancel()" i18n>Cancel</button>
  <button mat-raised-button color="accent" (click)="apply()" i18n>
    Apply Condition
  </button>
</mat-dialog-actions>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""