All files / app/formly geometries.type.ts

7.81% Statements 5/64
15.78% Branches 6/38
0% Functions 0/19
7.31% Lines 3/41

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 13311x       11x                                                                                                               11x                                                                                                                                                
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FieldArrayType, FormlyField, FormlyFieldConfig } from '@ngx-formly/core';
import { cloneDeep } from 'lodash-es';
import { Plugin } from '../model/plugin';
import { directChild } from '../util/tag';
 
/**
 * List of features in a GeoJSON FeatureCollection. Each child plugin of
 * props.parent with a single geometry type can be added to the list, and is
 * edited with the coordinates form of that plugin. Each feature has its own
 * style.
 */
@Component({
  selector: 'formly-field-geometries',
  host: { 'class': 'field geometries-field' },
  template: `
    <label>{{ props.label || '' }}</label>
    <div class="form-group">
      <select class="geometry-add"
              [title]="props.addText || ''"
              [attr.aria-label]="props.addText || ''"
              [disabled]="formControl.disabled"
              (input)="addGeometry($any($event.target))">
        <option value="" selected>{{ props.addText || '' }}</option>
        @for (p of geometryPlugins; track p.tag) {
          <option [value]="p.tag">{{ p.name || '#' + p.tag }}</option>
        }
      </select>
      @for (f of field.fieldGroup; track f.id; let i = $index) {
        <div class="geometry-item">
          <div class="form-array geometry-header">
            <span class="geometry-name">{{ name(geometryType(model?.[i])) }}</span>
            <button type="button" class="geometry-remove" (click)="remove(i)" i18n>&ndash;</button>
          </div>
          <formly-field [field]="f"></formly-field>
        </div>
      }
    </div>
  `,
  styles: `
    .geometry-item {
      display: flex;
      flex-direction: column;
      min-width: 0;
    }
    .geometry-item + .geometry-item {
      border-top: 0.5px dashed var(--border);
      padding-top: 4px;
    }
    .geometry-name {
      flex: 1;
      align-self: center;
    }
    .geometry-remove {
      flex: 0 0 auto;
    }
  `,
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [FormlyField],
})
export class FormlyFieldGeometriesI extends FieldArrayType {
 
  /**
   * Build each geometry with the form of its plugin. Set here since
   * functions are not stored in plugin configs.
   */
  prePopulate(field: FormlyFieldConfig) {
    field.fieldArray = (f: FormlyFieldConfig) => {
      // Called once for each new item, in order
      const index = f.fieldGroup?.length || 0;
      const type = geometryType((Array.isArray(f.model) ? f.model : [])[index]);
      const field = geometryField(geometryPlugins(f).find(p => p.defaults?.geometry?.type === type));
      return {
        fieldGroup: [
          { key: 'type', defaultValue: 'Feature' },
          { key: 'properties', type: 'geo-style', props: { fill: type === 'Polygon' || type === 'MultiPolygon' } },
          { ...field, key: 'geometry' },
        ],
      };
    };
  }
 
  get geometryPlugins() {
    return geometryPlugins(this.field);
  }
 
  geometryType(item: any) {
    return geometryType(item);
  }
 
  name(type?: string) {
    const plugin = this.geometryPlugins.find(p => p.defaults?.geometry?.type === type);
    return plugin?.name || type || '';
  }
 
  addGeometry(select: HTMLSelectElement) {
    const plugin = this.geometryPlugins.find(p => p.tag === select.value);
    select.value = '';
    if (!plugin) return;
    const geometry = cloneDeep(plugin.defaults!.geometry);
    this.add(undefined, { type: 'Feature', geometry });
  }
}
 
function geometryType(item: any): string | undefined {
  return item?.geometry?.type;
}
 
/**
 * Child plugins of props.parent storing a single geometry.
 */
function geometryPlugins(field: FormlyFieldConfig): Plugin[] {
  const parent = field.props?.parent;
  const plugins: Record<string, Plugin | undefined> = field.options?.formState?.admin?.status?.plugins || {};
  return (Object.values(plugins) as Plugin[])
    .filter(p => !!p && directChild(p.tag, parent))
    .filter(p => p.defaults?.geometry?.type && geometryCoordinates(p));
}
 
function geometryCoordinates(plugin?: Plugin): FormlyFieldConfig | undefined {
  return plugin?.config?.form?.find((f: FormlyFieldConfig) => f.key === 'geometry.coordinates');
}
 
function geometryField(plugin?: Plugin): FormlyFieldConfig {
  const coordinates = geometryCoordinates(plugin);
  return {
    fieldGroup: [
      { key: 'type' },
      ...coordinates ? [{ ...cloneDeep(coordinates), key: 'coordinates' }] : [],
    ],
  };
}