All files / app/component/select-plugin select-plugin.component.ts

56.41% Statements 22/39
64.28% Branches 18/28
33.33% Functions 4/12
68% Lines 17/25

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 7468x                             68x     14x   14x   14x   14x   14x         14x 14x 14x 14x     14x     14x 14x       13x                     1x 1x                                
import { Component, ElementRef, EventEmitter, Input, OnChanges, Output, SimpleChanges, ViewChild } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { defer, uniqBy } from 'lodash-es';
import { v4 as uuid } from 'uuid';
import { Plugin } from '../../model/plugin';
import { AdminService } from '../../service/admin.service';
import { AuthzService } from '../../service/authz.service';
 
@Component({
  selector: 'app-select-plugin',
  templateUrl: './select-plugin.component.html',
  styleUrls: ['./select-plugin.component.scss'],
  host: { 'class': 'select-plugin' },
  imports: [ReactiveFormsModule]
})
export class SelectPluginComponent implements OnChanges {
 
  @Input()
  id = 'plugin-' + uuid();
  @Input()
  add = false;
  @Input()
  text = false;
  @Input()
  settings = false;
  @Output()
  pluginChange = new EventEmitter<string>();
 
  @ViewChild('select')
  select?: ElementRef<HTMLSelectElement>;
 
  submitPlugins = this.admin.submit.filter(p => this.auth.canAddTag(p.tag));
  addPlugins = this.admin.add.filter(p => this.auth.canAddTag(p.tag));
  textPlugins = this.admin.submitText.filter(p => this.auth.canAddTag(p.tag));
  settingsPlugins = this.admin.submitSettings.filter(p => this.auth.canAddTag(p.tag));
 
  customPlugin?: Plugin;
  plugins: Plugin[] = [];
 
  constructor(
    private admin: AdminService,
    private auth: AuthzService,
  ) {  }
 
  ngOnChanges(changes: SimpleChanges) {
    this.plugins = uniqBy([
      ...(this.customPlugin ? [this.customPlugin] : []),
      ...(this.add ? this.addPlugins : []),
      ...(this.text ? this.textPlugins : []),
      ...(this.settings ? this.settingsPlugins : []),
      ...this.submitPlugins
    ], 'tag');
  }
 
  @Input()
  set plugin(value: string) {
    if (!this.select) {
      Iif (value) defer(() => this.plugin = value);
    } else E{
      if (!this.plugins.find(p => p?.tag === value)) {
        const plugin = this.admin.getPlugin(value);
        if (plugin) {
          this.customPlugin = plugin;
          this.plugins.unshift(plugin);
          defer(() => this.select!.nativeElement.selectedIndex = 1);
          return;
        }
      }
      this.select!.nativeElement.selectedIndex = this.plugins.map(p => p.tag).indexOf(value) + 1;
    }
  }
 
}