mirror of
https://dev.azure.com/hugendubel/ISA/_git/ISA-Frontend
synced 2025-12-31 09:37:15 +01:00
64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { Component, ElementRef, Input, OnInit, OnDestroy } from '@angular/core';
|
|
import { ModalService } from '../../../core/services/modal.service';
|
|
|
|
@Component({
|
|
selector: 'app-modal',
|
|
template:
|
|
`<div class="app-modal">
|
|
<div class="app-modal-body">
|
|
<ng-content></ng-content>
|
|
</div>
|
|
<div class="app-modal-background"></div>
|
|
</div>`,
|
|
styleUrls: ['./modal.component.scss']
|
|
})
|
|
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 {
|
|
const modal = this;
|
|
|
|
// ensure id attribute exists
|
|
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
|
|
this.element.addEventListener('click', function (e: any) {
|
|
if (e.target.className === 'app-modal') {
|
|
modal.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 directive is destroyed
|
|
ngOnDestroy(): void {
|
|
this.modalService.remove(this.id);
|
|
this.element.remove();
|
|
}
|
|
|
|
// open modal
|
|
open(): void {
|
|
this.element.children[0].style.display = 'block';
|
|
document.body.classList.add('app-modal-open');
|
|
}
|
|
|
|
// close modal
|
|
close(): void {
|
|
this.element.children[0].style.display = 'none';
|
|
document.body.classList.remove('app-modal-open');
|
|
}
|
|
}
|