All files / app/directive md-post.directive.ts

81.48% Statements 22/27
90.9% Branches 10/11
66.66% Functions 6/9
78.94% Lines 15/19

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 5067x         67x         6x   6x   6x 6x     6x 6x       5x 14x             9x 9x                 14x 4x 4x            
import { Directive, Inject, Input, OnDestroy, OnInit, ViewContainerRef } from '@angular/core';
import { Subject } from 'rxjs';
import { EmbedService } from '../service/embed.service';
 
@Directive({ selector: '[appMdPost]' })
export class MdPostDirective implements OnInit, OnDestroy {
 
  @Input('appMdPost')
  load?: Subject<void> | string;
  @Input()
  data? = '';
  @Input()
  origin? = '';
 
  private subscriptions: (() => void)[] = [];
  private lastData = '';
 
  constructor(
    private embeds: EmbedService,
    @Inject(ViewContainerRef) private viewContainerRef: ViewContainerRef,
  ) { }
 
  ngOnInit(): void {
    if (this.load && typeof this.load !== 'string') {
      this.load.subscribe(() => this.postProcess())
    } else E{
      this.postProcess();
    }
  }
 
  ngOnDestroy() {
    this.subscriptions.forEach(fn => fn());
    this.subscriptions.length = 0;
  }
 
  event(type: string, el: Element, fn: any) {
    el.addEventListener(type, fn);
    this.subscriptions.push(() => el.removeEventListener(type, fn));
  }
 
  postProcess() {
    if (this.data === this.lastData) return;
    this.ngOnDestroy();
    this.subscriptions.push(this.embeds.postProcess(
      this.viewContainerRef,
      (type, el, fn) => this.event(type, el, fn),
      this.origin));
  }
}