All files / app/form/plugins/gen gen.component.ts

64.15% Statements 34/53
51.72% Branches 15/29
62.5% Functions 10/16
68.18% Lines 30/44

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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132            79x                 79x     2x   2x           2x               2x   2x   2x     2x               2x 2x         2x       1x       11x       79x 3x           3x 3x             79x 3x 3x 3x       79x 3x 3x                     2x 2x       2x   2x                                       2x    
import { Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild, ChangeDetectionStrategy } from '@angular/core';
import { ReactiveFormsModule, UntypedFormGroup } from '@angular/forms';
import { FormlyFieldConfig, FormlyForm, FormlyFormOptions } from '@ngx-formly/core';
import { cloneDeep } from 'lodash-es';
import { Plugin } from '../../../model/plugin';
import { AdminService } from '../../../service/admin.service';
import { memo, MemoCache } from '../../../util/memo';
 
@Component({
  selector: 'app-form-gen',
  templateUrl: './gen.component.html',
  styleUrls: ['./gen.component.scss'],
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [ReactiveFormsModule, FormlyForm]
})
export class GenFormComponentI implements OnInit, OnChanges {
 
  @Input()
  bulk = false;
  @Input()
  promoteAdvanced = false;
  @Input()
  plugins!: UntypedFormGroup;
  @Input()
  plugin!: Plugin;
  @Input()
  children: Plugin[] = [];
  /**
   * Initial plugin data by tag, used instead of the defaults when a plugin
   * is added. Read when the plugin form is created, which emits pendingUsed.
   */
  @Input()
  pending?: Record<string, any>;
  @Output()
  pendingUsed = new EventEmitter<string>();
  @Output()
  togglePlugin = new EventEmitter<string>();
  @Output()
  setPlugin = new EventEmitter<{ tag: string, value: any }>();
 
  model: any;
  options: FormlyFormOptions = {
    formState: {
      admin: this.admin,
      config: {},
      togglePlugin: (tag: string) => this.togglePlugin.next(tag),
      setPlugin: (tag: string, value: any) => this.setPlugin.next({ tag, value }),
    },
  };
  headerModel = {};
  headerOptions: FormlyFormOptions = {
    formState: this.options.formState,
  };
 
  constructor(
    private admin: AdminService,
  ) { }
 
  ngOnChanges(changes: SimpleChanges) {
    MemoCache.clear(this);
  }
 
  get group() {
    return this.plugins.get(this.plugin.tag) as UntypedFormGroup | undefined;
  }
 
  @memo
  get form() {
    Iif (this.bulk) {
      if (this.plugin.config?.bulkForm === true) {
        return cloneDeep(this.plugin.config?.form || this.plugin.config?.advancedForm);
      }
      return cloneDeep(this.plugin.config?.bulkForm);
    }
    const form = this.plugin.config?.form?.filter(f => !isHeader(f));
    return form?.length ? cloneDeep(form) : undefined;
  }
 
  /**
   * Fields shown in place of the plugin name, such as the child plugin select.
   */
  @memo
  get headerForm() {
    Iif (this.bulk) return undefined;
    const form = this.plugin.config?.form?.filter(isHeader);
    return form?.length ? cloneDeep(form) : undefined;
  }
 
  @memo
  get advancedForm() {
    Iif (this.bulk) return undefined;
    return cloneDeep(this.plugin.config?.advancedForm);
  }
 
  get childrenOn() {
    for (let i = this.children.length - 1; i >= 0; i--) {
      if (this.plugins.contains(this.children[i].tag)) return i;
    }
    return 0;
  }
 
  ngOnInit(): void {
    const pending = this.pending?.[this.plugin.tag];
    Iif (pending) {
      this.model = cloneDeep(pending);
      this.pendingUsed.next(this.plugin.tag);
    } else {
      this.group?.patchValue(this.plugin.defaults);
    }
    this.options.formState.config = this.plugin.defaults;
  }
 
  setValue(value: any) {
    this.model = value[this.plugin.tag];
  }
 
  cssClass(tag: string) {
    return tag.replace(/\//g, '_')
      .replace(/\./g, '-')
      .replace(/[^\w-_]/g, '');
  }
 
  toggleChild(tag: string) {
    this.togglePlugin.next(tag);
    if ('vibrate' in navigator) navigator.vibrate([2, 8, 8]);
  }
}
 
function isHeader(field: FormlyFieldConfig) {
  return field.type === 'child-plugin';
}