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

48.27% Statements 42/87
27.94% Branches 19/68
46.15% Functions 12/26
50% Lines 36/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 18069x                             69x   69x   69x 69x 69x                 69x 13x           13x       13x   13x 13x     13x 13x   13x             12x 11x           12x 1x 11x 11x                 12x 12x           13x           12x 12x         13x 13x       60x       47x       84x       37x                                                                   14x                                 5x 5x     5x                        
import {
  AfterViewInit,
  Component,
  EventEmitter,
  Input,
  OnChanges,
  Output,
  QueryList,
  SimpleChanges,
  ViewChildren
} from '@angular/core';
import { ReactiveFormsModule, UntypedFormArray, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
import { defer } from 'lodash-es';
import { toJS } from 'mobx';
import { Subject, takeUntil } from 'rxjs';
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' },
  imports: [ReactiveFormsModule, TitleDirective, GenFormComponent]
})
export class PluginsFormComponent implements OnChanges, AfterViewInit {
  private destroy$ = new Subject<void>();
 
  @ViewChildren('gen')
  gens?: QueryList<GenFormComponent>;
 
  @Input()
  fieldName = 'plugins';
  @Input()
  group: UntypedFormGroup;
  @Output()
  togglePlugin = new EventEmitter<string>();
 
  icons: Icon[] = [];
  forms: Plugin[] = [];
 
  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) {
        if (!this.plugins.contains(t)) {
          const form = pluginForm(this.fb, this.admin, t);
          if (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(
      takeUntil(this.destroy$),
    ).subscribe(() => this.init());
  }
 
  ngOnChanges(changes: SimpleChanges) {
    Eif (changes.group) {
      this.init();
    }
  }
 
  ngOnDestroy(): void {
    this.destroy$.next();
    this.destroy$.complete();
  }
 
  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))
    });
  }
 
  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) {
  if (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;
}