blob: 823162e8de921861c4bb630a82cff73a1e030459 (
plain) (
blame)
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
|
import { Component, ElementRef, Input, OnInit, OnDestroy } from '@angular/core';
import { ModalService } from './modal.service';
@Component({
selector: 'oid-modal',
template:
`<div class="oid-modal">
<div class="oid-modal-body">
<ng-content></ng-content>
</div>
</div>
<div class="oid-modal-background"></div>`
})
export class ModalComponent implements OnInit, OnDestroy {
@Input() id: string;
private element: any;
constructor(private modalService: ModalService, private el: ElementRef) {
this.element = el.nativeElement;
}
ngOnInit(): void {
if (!this.id) {
console.error('modal must have an id');
return;
}
// move element to bottom of page (just before </body>) so it can be displayed above everything else
document.body.appendChild(this.element);
// close modal on background click -- doesn't work
this.element.addEventListener('click', function (e: any) {
if (e.target.className === 'oid-modal') {
this.close();
}
});
// add self (this modal instance) to the modal service so it's accessible from controllers
this.modalService.add(this);
}
// remove self from modal service when component is destroyed
ngOnDestroy(): void {
this.modalService.remove(this.id);
this.element.remove();
}
open(): void {
this.element.style.display = 'block';
document.body.classList.add('oid-modal-open');
}
close(): void {
this.element.style.display = 'none';
document.body.classList.remove('oid-modal-open');
}
}
|