All files / src/lib/wvr-modal wvr-modal.component.ts

59.09% Statements 26/44
16.67% Branches 5/30
33.33% Functions 5/15
59.52% Lines 25/42

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                                  1x             2x                       2x                 2x   2x   2x   2x   2x   2x           2x   2x       2x   2x                   2x   2x               2x   2x   2x   2x             2x   2x                                                               2x   2x               4x                                     2x      
import { ChangeDetectionStrategy, Component, Injector, Input, OnDestroy, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { Subscription } from 'rxjs';
import { select } from '@ngrx/store';
import { filter } from 'rxjs/operators';
import * as ModalActions from '../core/modal/modal.actions';
import { selectModalState } from '../core/store';
import { ThemeVariantName } from '../shared/theme';
import { preserveContent, projectContent } from '../shared/utility/projection.utility';
import { WvrBaseComponent } from '../shared/wvr-base.component';
 
@Component({
  selector: 'wvr-modal-component',
  templateUrl: './wvr-modal.component.html',
  styleUrls: ['./wvr-modal.component.scss'],
  changeDetection: ChangeDetectionStrategy.Default
})
export class WvrModalComponent extends WvrBaseComponent implements OnInit, OnDestroy {
 
  @ViewChild('modalTemplateContent') modalTemplateContent: TemplateRef<Element>;
 
  modalRef: NgbModalRef;
 
  /** This allows for the modal button to be toggle able. */
  @Input() btnVisible: 'true' | 'false' = 'true';
 
  /** The header title value to be displayed as a modal title. */
  @Input() title: string;
 
  /** The text value to be displayed for the button launching the modal. */
  @Input() btnText: string;
 
  /** This defines the modal id. */
  @Input() modalId: string;
 
  /** Allows for the override of theme variant for modal component. */
  @Input() themeVariant: ThemeVariantName = 'primary';
 
  /** Allows for the override of theme variant for the button launching the modal component. */
  @Input() btnThemeVariant: ThemeVariantName;
 
  /** Allows for the override of theme variant for modal header. */
  @Input() modalHeaderThemeVariant: ThemeVariantName;
 
  /** Allows for the override of theme variant for modal footer. */
  @Input() modalFooterThemeVariant: ThemeVariantName = 'light';
 
  @Input() size: 'sm' | 'lg' | 'xl' = 'lg';
 
  @Input() backdrop: 'static' | boolean = 'static';
 
  @Input() centered = false;
 
  @Input() animation = true;
 
  @Input() keyboard = false;
 
  subscription: Subscription;
 
  constructor(
    injector: Injector,
    private readonly modalService: NgbModal
  ) {
    super(injector);
  }
 
  ngOnInit(): void {
    super.ngOnInit();
 
    this.modalService.activeInstances.subscribe((modalRefs: Array<NgbModalRef>) => {
      setTimeout(() => {
        if (!!this.modalRef && modalRefs.length > 0) {
          ['body', 'footer'].forEach(content => {
            projectContent(this.eRef, `template[modal-${content}]`, `div[modal-${content}]`);
          });
        }
      });
    });
 
    const defaultName = 'Weaver Modal';
 
    this.modalId = !!this.modalId
      ? this.modalId
      : !!this.title
      ? this.title
      : `${defaultName
        .split(' ')
        .join('')}-${this.id}`;
 
    this.title = !this.title ? defaultName : this.title;
 
    this.btnText = this.btnText ? this.btnText : this.title;
 
    this.btnThemeVariant = this.btnThemeVariant ? this.btnThemeVariant : this.themeVariant;
 
    this.store.dispatch(ModalActions.addModal({
      modal: {
        name: this.modalId,
        open: false
      }
    }));
 
    this.subscription = this.store.pipe(
      select(selectModalState),
      filter(modalState => !!modalState)
    )
    .subscribe(modalState => {
      const modal = modalState.entities[this.modalId];
      if (modal.open) {
        this.modalRef = this.modalService.open(this.modalTemplateContent, {
          ariaLabelledBy: 'modal-basic-title',
          container: this.eRef.nativeElement,
          size: this.size,
          backdrop: this.backdrop,
          centered: this.centered,
          animation: this.animation,
          keyboard: this.keyboard,
          modalDialogClass: 'modal-dialog',
          beforeDismiss: () => {
            this.store.dispatch(ModalActions.closeModal({ id: this.modalId }));
 
            return false;
          }
        });
 
      } else if (this.modalRef) {
        ['body', 'footer'].forEach(content => {
          preserveContent(this.eRef, `template[modal-${content}]`, `div[modal-${content}]`);
        });
        this.modalRef.close();
        delete this.modalRef;
      }
    });
  }
 
  ngOnDestroy(): void {
    this.modalService.activeInstances.unsubscribe();
 
    this.subscription.unsubscribe();
  }
 
  openModal(): void {
    this.store.dispatch(ModalActions.openModal({ id: this.modalId }));
  }
 
  get openProps(): string {
    return `{ id: '${this.modalId}'}`;
  }
 
  get additionalHeaderClasses(): string {
    return this.modalHeaderThemeVariant ?
      `bg-${this.modalHeaderThemeVariant} border-${this.modalHeaderThemeVariant} ${this.getTextColor(this.modalHeaderThemeVariant)}` :
      this.themeVariant ?
        `bg-${this.themeVariant} border-${this.themeVariant} ${this.getTextColor(this.themeVariant)}` :
        'bg-light text-dark';
  }
 
  get additionalFooterClasses(): string {
    return this.modalFooterThemeVariant ?
      `bg-${this.modalFooterThemeVariant} border-${this.modalFooterThemeVariant} ${this.getTextColor(this.modalFooterThemeVariant)}` :
      this.themeVariant ?
        `bg-${this.themeVariant} border-${this.themeVariant} ${this.getTextColor(this.themeVariant)}` :
        'bg-light text-dark';
  }
 
  getTextColor = (themeVariant): string => ((themeVariant === 'warning') || (themeVariant === 'light')) ? 'text-dark' : 'text-white';
 
}