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

52.94% Statements 45/85
48% Branches 24/50
50% Functions 13/26
56.94% Lines 41/72

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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 18878x                                   78x   78x   78x 78x 78x                   78x 16x           16x       16x   16x 16x       16x     16x 16x   16x             18x 17x           18x 1x 17x 17x 6x 6x 6x           18x 18x           16x   3x       15x 15x           87x       71x       126x       49x                                                                           17x                   6x     6x       5x 5x     5x                        
import {
  DestroyRef,
  inject,
  AfterViewInit,
  Component,
  EventEmitter,
  Input,
  OnChanges,
  Output,
  QueryList,
  SimpleChanges,
  ViewChildren,
  ChangeDetectionStrategy
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ReactiveFormsModule, UntypedFormArray, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
import { defer } from 'lodash-es';
import { toJS } from 'mobx';
import { TitleDirective } from '../../directive/title.directive';
import { Plugin } from '../../model/plugin';
import { active, Icon, ResponseAction, sortOrder, TagAction, Visibility, visible } from '../../model/tag';
import { AdminService } from '../../service/admin.service';
import { emptyObject, getScheme, patchObj, writeObj } from '../../util/http';
import { addAllHierarchicalTags, hasTag } from '../../util/tag';
import { GenFormComponent } from './gen/gen.component';
 
@Component({
  selector: 'app-form-plugins',
  templateUrl: './plugins.component.html',
  styleUrls: ['./plugins.component.scss'],
  host: { 'class': 'plugins-form' },
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [ReactiveFormsModule, TitleDirective, GenFormComponent]
})
export class PluginsFormComponent implements OnChanges, AfterViewInit {
  private destroyRef = inject(DestroyRef);
 
  @ViewChildren('gen')
  gens?: QueryList<GenFormComponent>;
 
  @Input()
  fieldName = 'plugins';
  @Input()
  group: UntypedFormGroup;
  @Output()
  togglePlugin = new EventEmitter<string>();
 
  icons: Icon[] = [];
  forms: Plugin[] = [];
  /**
   * Initial data for plugins about to be added.
   */
  pending: Record<string, any> = {};
 
  constructor(
    public admin: AdminService,
    private fb: UntypedFormBuilder,
  ) {
    this.group = fb.group({
      tags: fb.array([]),
      [this.fieldName]: pluginsForm(fb, admin, []),
    });
  }
 
  init() {
    if (this.plugins) {
      for (const p in this.plugins.value) {
        if (!this.allTags.includes(p)) {
          this.plugins.removeControl(p);
        }
      }
    }
    if (!this.plugins) {
      this.group.addControl(this.fieldName, pluginsForm(this.fb, this.admin, this.allTags));
    E} else if (this.allTags) {
      for (const t of this.allTags) {
        Eif (!this.plugins.contains(t)) {
          const form = pluginForm(this.fb, this.admin, t);
          Iif (form) {
            this.plugins.addControl(t, form);
          }
        }
      }
    }
    this.forms = this.admin.getPluginForms(this.allTags);
    this.icons = sortOrder(this.admin.getIcons(this.allTags, this.plugins.value, getScheme(this.group.value.url))
      .filter(i => !this.forms.find(p => p.tag === i.tag)))
      .filter(i => this.showIcon(i));
  }
 
  ngAfterViewInit() {
    this.tags.valueChanges.pipe(
      takeUntilDestroyed(this.destroyRef),
    ).subscribe(() => this.init());
  }
 
  ngOnChanges(changes: SimpleChanges) {
    Eif (changes.group) {
      this.init();
    }
  }
 
 
  get tags() {
    return this.group.get('tags') as UntypedFormArray;
  }
 
  get allTags() {
    return addAllHierarchicalTags(this.tags.value);
  }
 
  get plugins() {
    return this.group.get(this.fieldName) as UntypedFormGroup;
  }
 
  get empty() {
    return !this.icons.length && !Object.keys(this.plugins.controls).length;
  }
 
  setValue(value: any) {
    value = toJS(value);
    defer(() => {
      this.plugins.patchValue(value);
      this.gens!.forEach(g => g.setValue(value))
    });
  }
 
  usePending(tag: string) {
    delete this.pending[tag];
  }
 
  visible(v: Visibility) {
    return visible(this.group.value, v, true, false);
  }
 
  active(a: TagAction | ResponseAction | Icon) {
    return active(this.group.value, a);
  }
 
  showIcon(i: Icon) {
    return this.visible(i) && this.active(i);
  }
 
  hasForm(plugin?: Plugin) {
    if (!plugin) return false;
    if (plugin.config?.submitChild) return false;
    if (plugin.config?.form?.length) return true;
    if (plugin.config?.advancedForm?.length) return true;
    if (this.admin.getPluginSubForms(plugin.tag).length) return true;
    return false;
  }
}
 
export function pluginsForm(fb: UntypedFormBuilder, admin: AdminService, tags: string[]) {
  return fb.group(tags.reduce((plugins: any, tag: string) => {
    const form = pluginForm(fb, admin, tag);
    if (form) {
      plugins[tag] = form;
    }
    return plugins;
  }, {}));
}
 
function pluginForm(fb: UntypedFormBuilder, admin: AdminService, tag: string) {
  Iif (admin.getPlugin(tag)?.config?.form || admin.getPlugin(tag)?.config?.advancedForm) {
    return fb.group({});
  }
  return null;
}
 
export function writePlugins(tags: string[], plugins: Record<string, any>): Record<string, any> | undefined {
  const result: Record<string, any> = {};
  for (const p in plugins) {
    if (hasTag(p, tags)) result[p] = writeObj(plugins[p]);
  }
  Eif (emptyObject(result)) return undefined;
  return result;
}
 
export function patchPlugins(plugins: Record<string, any>): Record<string, any> | undefined {
  const result: Record<string, any> = {};
  for (const p in plugins) {
    result[p] = patchObj(plugins[p]);
  }
  if (emptyObject(result)) return {};
  return result;
}