All files / src/app/shared/formly/field-library field-library.ts

92% Statements 23/25
58.82% Branches 10/17
100% Functions 8/8
91.3% Lines 21/23

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 907x   7x   7x     7x           7x         7x               18x   6x 6x                             7x         7x 7x     4x 7x 1x       7x                           4x 4x     2x 2x               1x      
import { Inject, Injectable, InjectionToken } from '@angular/core';
import { FormlyFieldConfig } from '@ngx-formly/core';
import { mergeWith } from 'lodash-es';
 
import { InjectMultiple } from 'ish-core/utils/injection';
import { FieldLibraryConfiguration } from 'ish-shared/formly/field-library/configurations/field-library-configuration';
 
export const FIELD_LIBRARY_CONFIGURATION = new InjectionToken<FieldLibraryConfiguration>(
  'Reusable Formly Field Configuration'
);
 
type ConfigurationGroup = { id: string; shortcutFor: string[] };
 
export const FIELD_LIBRARY_CONFIGURATION_GROUP = new InjectionToken<ConfigurationGroup>(
  'Reusable Formly Field Configuration Group'
);
 
@Injectable()
export class FieldLibrary {
  constructor(
    @Inject(FIELD_LIBRARY_CONFIGURATION)
    fieldLibraryConfigurations: InjectMultiple<typeof FIELD_LIBRARY_CONFIGURATION>,
    @Inject(FIELD_LIBRARY_CONFIGURATION_GROUP)
    fieldLibraryConfigurationGroups: InjectMultiple<typeof FIELD_LIBRARY_CONFIGURATION_GROUP>
  ) {
    // create configuration dictionary from array
    this.configurations = fieldLibraryConfigurations?.reduce((acc, curr) => ({ ...acc, [curr.id]: curr }), {}) ?? {};
    // create shortcut dictionary from array
    this.shortcuts =
      fieldLibraryConfigurationGroups?.reduce((acc, curr) => ({ ...acc, [curr.id]: curr.shortcutFor }), {}) ?? {};
  }
 
  private configurations: Record<string, FieldLibraryConfiguration>;
  private shortcuts: Record<string, string[]>;
 
  /**
   * Method for getting a reusable configuration by its id.
   * Uses lodash merge to override properties
   *
   * @param id the id of the reusable configuration
   * @param override an object of modifications that will be made to the standard configuration
   * @returns a reusable formly field configuration that might be modified
   */
  getConfiguration(id: string, override?: Partial<FormlyFieldConfig>): FormlyFieldConfig {
    Iif (!this.configurations[id]) {
      throw new TypeError(
        `configuration ${id} does not exist. Check whether it's part of the reusable-configurations.module.ts`
      );
    }
    let config = { key: id, ...this.configurations[id].getFieldConfig() };
    if (override) {
      // modify the default lodash merge behavior:
      // if you are attempting to merge arrays, instead just overwrite with the new array
      config = mergeWith(config, override, (obj, src) => {
        if (Array.isArray(obj)) {
          return src;
        }
      });
    }
    return config;
  }
 
  /**
   * Utility method that makes it easier to get multiple field configurations at once
   *
   * @param group either a group id or an array of ids.
   * @returns an array containing the merged field configurations
   */
  getConfigurationGroup(
    group: string | string[],
    overrides?: Record<string, Partial<FormlyFieldConfig>>
  ): FormlyFieldConfig[] {
    // if group is an array of string, return a merged configuration
    if (Array.isArray(group)) {
      return group.map(id => this.getConfiguration(id, overrides?.[id]));
    }
    // if group is a string, extract the relevant shortcut and recursively call this function with an array of field ids
    if (this.shortcuts[group]) {
      return this.getConfigurationGroup(this.shortcuts[group], overrides);
    }
    throw new TypeError(
      `configuration group ${group} does not exist. Check whether it's part of the reusable-configurations.module.ts`
    );
  }
 
  getAvailableConfigurationIds() {
    return Object.keys(this.configurations);
  }
}