Project

General

Profile

1
import {Component, ElementRef, EventEmitter, Input, OnInit, Output} from "@angular/core";
2
import {fromEvent, Subscription} from 'rxjs';
3
import {delay} from "rxjs/operators";
4

    
5
@Component({
6
  selector: 'fs-modal',
7
  template: `
8
    <div class="fs-modal">
9
      <div class="fs-modal-bg" (click)="close()"></div>
10
      <div id="fs-modal" class="fs-modal-dialog">
11
        <!--<a class="close" (click)="close()">
12
          <icon name="close"></icon>
13
        </a>-->
14
        <div class="header">
15
          <div class="uk-flex-middle uk-grid" uk-grid>
16
            <h4 *ngIf="title" class="uk-margin-remove uk-width-expand uk-text-truncate">{{title}}</h4>
17
            <div *ngIf="cancelButton || okButton" class="uk-flex uk-flex-right">
18
              <div>
19
                <ng-content select="[actions]"></ng-content>
20
              </div>
21
              <button *ngIf="cancelButton" class="uk-button uk-button-secondary outlined uk-margin-small-left" [disabled]="cancelButtonDisabled" (click)="close()">{{cancelButtonText}}</button>
22
              <button *ngIf="okButton" class="uk-button uk-button-secondary uk-margin-small-left" [disabled]="okButtonDisabled" (click)="ok()">{{okButtonText}}</button>
23
            </div>
24
          </div>
25
        </div>
26
        <div class="content">
27
          <ng-content></ng-content>
28
        </div>
29
      </div>
30
    </div>
31
  `
32
})
33
export class FullScreenModalComponent implements OnInit {
34
  okButtonText = 'OK';
35
  cancelButtonText = 'Cancel';
36
  cancelButton: boolean = false;
37
  okButton: boolean = false;
38
  title: string = null;
39
  @Input()
40
  okButtonDisabled = false;
41
  @Input()
42
  cancelButtonDisabled = false;
43
  @Output()
44
  okEmitter: EventEmitter<boolean> = new EventEmitter<boolean>();
45
  @Output()
46
  cancelEmitter: EventEmitter<boolean> = new EventEmitter<boolean>();
47
  opened: boolean = false;
48
  private readonly element: any;
49
  private subscriptions: any[] = [];
50
  private clickedInside: boolean;
51
  
52
  constructor(private el: ElementRef) {
53
    this.element = el.nativeElement;
54
  }
55
  
56
  ngOnInit() {
57
    if (typeof document !== "undefined") {
58
      document.body.appendChild(this.element);
59
      this.subscriptions.push(fromEvent(document, 'keydown').pipe(delay(1)).subscribe((event: KeyboardEvent) => {
60
        if(event.keyCode === 27) {
61
          this.close();
62
        }
63
      }));
64
    }
65
  }
66
  
67
  ngOnDestroy(): void {
68
    this.element.remove();
69
    this.subscriptions.forEach(subscription => {
70
      if (subscription instanceof Subscription) {
71
        subscription.unsubscribe();
72
      }
73
    })
74
  }
75
  
76
  open() {
77
    if (typeof document !== "undefined") {
78
      this.opened = true;
79
      document.getElementsByTagName('html')[0].classList.add('fs-modal-open');
80
    }
81
  }
82
  
83
  close() {
84
    if (typeof document !== "undefined" && this.opened) {
85
        document.getElementsByTagName('html')[0].classList.remove('fs-modal-open');
86
        this.opened = false;
87
        this.cancelEmitter.emit();
88
    }
89
  }
90
  
91
  ok() {
92
    this.close();
93
    this.okEmitter.emit(true);
94
  }
95
}
(1-1/2)