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

45.45% Statements 15/33
48% Branches 12/25
30% Functions 3/10
50% Lines 10/20

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 552x           2x                 7x     2x         2x   2x     2x 2x         1x 1x                                      
import { Component, ElementRef, EventEmitter, Input, Output, ViewChild } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { defer } from 'lodash-es';
import { Template } from '../../model/template';
import { AdminService } from '../../service/admin.service';
import { AuthzService } from '../../service/authz.service';
import { access } from '../../util/tag';
 
@Component({
  selector: 'app-select-template',
  templateUrl: './select-template.component.html',
  styleUrls: ['./select-template.component.scss'],
  host: { 'class': 'select-template' },
  imports: [ReactiveFormsModule]
})
export class SelectTemplateComponent {
 
  @Output()
  templateChange = new EventEmitter<string>();
 
  @ViewChild('select')
  select?: ElementRef<HTMLSelectElement>;
 
  submitTemplates = this.admin.tmplSubmit.filter(p => this.auth.canAddTag(p.tag));
 
  templates: Template[] = [...this.submitTemplates];
 
  constructor(
    private admin: AdminService,
    private auth: AuthzService,
  ) {  }
 
  @Input()
  set template(value: string) {
    if (!this.select) {
      Iif (value) defer(() => this.template = value);
    } else E{
      let hit = this.templates.map(t => t.tag).indexOf(value) + 1;
      if (!hit) {
        hit = this.templates.map(t => t.tag).indexOf(value.substring(access(value).length)) + 1;
      }
      if (!hit && value && !this.templates.find(p => (p?.tag) === value)) {
        const template = this.admin.getTemplate(value);
        if (template) {
          this.templates.unshift(template);
          defer(() => this.select!.nativeElement.selectedIndex = 1);
          return;
        }
      }
      defer(() => this.select!.nativeElement.selectedIndex = hit);
    }
  }
 
}