]>
Commit | Line | Data |
---|---|---|
42b40636 | 1 | import { Component, ElementRef, Input, ViewChild } from '@angular/core' |
437e8e06 | 2 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' |
42b40636 | 3 | import { logger } from '@root-helpers/logger' |
437e8e06 K |
4 | |
5 | @Component({ | |
6 | selector: 'my-custom-modal', | |
7 | templateUrl: './custom-modal.component.html', | |
8 | styleUrls: [ './custom-modal.component.scss' ] | |
9 | }) | |
10 | export class CustomModalComponent { | |
11 | @ViewChild('modal', { static: true }) modal: ElementRef | |
12 | ||
13 | @Input() title: string | |
14 | @Input() content: string | |
15 | @Input() close?: boolean | |
16 | @Input() cancel?: { value: string, action?: () => void } | |
17 | @Input() confirm?: { value: string, action?: () => void } | |
18 | ||
19 | private modalRef: NgbModalRef | |
20 | ||
21 | constructor ( | |
22 | private modalService: NgbModal | |
23 | ) { } | |
24 | ||
25 | show (input: { | |
9df52d66 C |
26 | title: string |
27 | content: string | |
28 | close?: boolean | |
29 | cancel?: { value: string, action?: () => void } | |
437e8e06 K |
30 | confirm?: { value: string, action?: () => void } |
31 | }) { | |
32 | if (this.modalRef instanceof NgbModalRef && this.modalService.hasOpenModals()) { | |
42b40636 | 33 | logger.error('Cannot open another custom modal, one is already opened.') |
437e8e06 K |
34 | return |
35 | } | |
36 | ||
37 | const { title, content, close, cancel, confirm } = input | |
38 | ||
39 | this.title = title | |
40 | this.content = content | |
41 | this.close = close | |
42 | this.cancel = cancel | |
43 | this.confirm = confirm | |
44 | ||
45 | this.modalRef = this.modalService.open(this.modal, { | |
46 | centered: true, | |
47 | backdrop: 'static', | |
48 | keyboard: false, | |
49 | size: 'lg' | |
50 | }) | |
51 | } | |
52 | ||
53 | onCancelClick () { | |
54 | this.modalRef.close() | |
55 | ||
56 | if (typeof this.cancel.action === 'function') { | |
57 | this.cancel.action() | |
58 | } | |
59 | ||
60 | this.destroy() | |
61 | } | |
62 | ||
63 | onCloseClick () { | |
64 | this.modalRef.close() | |
65 | this.destroy() | |
66 | } | |
67 | ||
68 | onConfirmClick () { | |
69 | this.modalRef.close() | |
70 | ||
71 | if (typeof this.confirm.action === 'function') { | |
72 | this.confirm.action() | |
73 | } | |
74 | ||
75 | this.destroy() | |
76 | } | |
77 | ||
78 | hasCancel () { | |
79 | return typeof this.cancel !== 'undefined' | |
80 | } | |
81 | ||
82 | hasConfirm () { | |
83 | return typeof this.confirm !== 'undefined' | |
84 | } | |
85 | ||
86 | private destroy () { | |
87 | delete this.modalRef | |
88 | delete this.title | |
89 | delete this.content | |
90 | delete this.close | |
91 | delete this.cancel | |
92 | delete this.confirm | |
93 | } | |
94 | } |