All files / lib/dynamic-form-element/dynamic-form-modal dynamic-form-modal.ts

100% Statements 29/29
100% Branches 15/15
100% Functions 19/19
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97                                      14x 14x                 14x 14x 14x 14x 5x 10x       4x   4x 5x   21x 2x     7x 7x 7x 7x       2x       2x       6x       2x       2x       8x 8x 4x         7x       7x       7x       7x      
import { BehaviorSubject, Observable } from 'rxjs';
import { DynamicFormAction } from '../../dynamic-form-action/dynamic-form-action';
import { DynamicForm } from '../../dynamic-form/dynamic-form';
import { DynamicFormBuilder } from '../../dynamic-form/dynamic-form.builder';
import { DynamicFormElement } from '../dynamic-form-element';
import { DynamicFormElementType } from '../dynamic-form-element-type';
import { DynamicFormModalDefinition } from './dynamic-form-modal-definition';
import { DynamicFormModalTemplate } from './dynamic-form-modal-template';
 
export class DynamicFormModal<
  Template extends DynamicFormModalTemplate = DynamicFormModalTemplate,
  Definition extends DynamicFormModalDefinition<Template> = DynamicFormModalDefinition<Template>
> extends DynamicFormElement<Template, Definition> {
 
  private readonly _isOpenSubject: BehaviorSubject<boolean>;
  private readonly _isOpenChanges: Observable<boolean>;
 
  protected _trigger: DynamicFormAction;
 
  protected _headerActions: DynamicFormAction[] = [];
  protected _footerActions: DynamicFormAction[] = [];
 
  constructor(
    builder: DynamicFormBuilder,
    root: DynamicForm,
    parent: DynamicFormElement,
    definition: Definition,
    type: DynamicFormElementType,
  ) {
    super(builder, root, parent, definition, type);
    this._isOpenSubject = new BehaviorSubject(false);
    this._isOpenChanges = this._isOpenSubject.asObservable();
    this.extendExpressionData({
      isOpen: () => this.isOpen,
      maximized: () => this.template.maximized,
    });
  }
 
  get trigger(): DynamicFormAction { return this._trigger; }
 
  get headerActions(): DynamicFormAction[] { return this._headerActions; }
  get footerActions(): DynamicFormAction[] { return this._footerActions; }
 
  get isOpen(): boolean { return this._isOpenSubject.value; }
  get isOpenChanges(): Observable<boolean> { return this._isOpenChanges; }
 
  override init(): void {
    super.init();
    this.initTrigger();
    this.initHeaderActions();
    this.initFooterActions();
  }
 
  open(): void {
    return !this.isOpen && this.toggle();
  }
 
  close(): void {
    return this.isOpen && this.toggle();
  }
 
  toggle(): void {
    this._isOpenSubject.next(!this.isOpen);
  }
 
  maximize(): void {
    return !this.template.maximized && this.toggleSize();
  }
 
  minimize(): void {
    return this.template.maximized && this.toggleSize();
  }
 
  toggleSize(): void {
    const descriptor = Object.getOwnPropertyDescriptor(this.template, 'maximized');
    if (!descriptor || descriptor.writable) {
      this.template.maximized = !this.template.maximized;
    }
  }
 
  protected override getChildren(): DynamicFormElement[] {
    return this._builder.createFormElements(this.root, this.parent, this.definition.children);
  }
 
  protected initTrigger(): void {
    this._trigger = this._builder.createFormAction(this.root, this, this.definition.trigger);
  }
 
  protected initHeaderActions(): void {
    this._headerActions = this._builder.createFormActions(this.root, this, this.definition.headerActions) || [];
  }
 
  protected initFooterActions(): void {
    this._footerActions = this._builder.createFormActions(this.root, this, this.definition.footerActions) || [];
  }
}