]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, OnInit, ViewChild } from '@angular/core' | |
2 | import { ConfirmService } from '@app/core/confirm/confirm.service' | |
3 | import { POP_STATE_MODAL_DISMISS } from '@app/helpers' | |
4 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
5 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
6 | ||
7 | @Component({ | |
8 | selector: 'my-confirm', | |
9 | templateUrl: './confirm.component.html', | |
10 | styleUrls: [ './confirm.component.scss' ] | |
11 | }) | |
12 | export class ConfirmComponent implements OnInit { | |
13 | @ViewChild('confirmModal', { static: true }) confirmModal: ElementRef | |
14 | ||
15 | title = '' | |
16 | message = '' | |
17 | expectedInputValue = '' | |
18 | inputLabel = '' | |
19 | ||
20 | inputValue = '' | |
21 | confirmButtonText = '' | |
22 | ||
23 | private openedModal: NgbModalRef | |
24 | ||
25 | constructor ( | |
26 | private modalService: NgbModal, | |
27 | private confirmService: ConfirmService | |
28 | ) { } | |
29 | ||
30 | ngOnInit () { | |
31 | this.confirmService.showConfirm.subscribe( | |
32 | ({ title, message, expectedInputValue, inputLabel, confirmButtonText }) => { | |
33 | this.title = title | |
34 | this.message = message | |
35 | ||
36 | this.inputLabel = inputLabel | |
37 | this.expectedInputValue = expectedInputValue | |
38 | ||
39 | this.confirmButtonText = confirmButtonText || $localize`Confirm` | |
40 | ||
41 | this.showModal() | |
42 | } | |
43 | ) | |
44 | } | |
45 | ||
46 | confirm () { | |
47 | if (this.openedModal) this.openedModal.close() | |
48 | } | |
49 | ||
50 | isConfirmationDisabled () { | |
51 | // No input validation | |
52 | if (!this.inputLabel || !this.expectedInputValue) return false | |
53 | ||
54 | return this.expectedInputValue !== this.inputValue | |
55 | } | |
56 | ||
57 | showModal () { | |
58 | this.inputValue = '' | |
59 | ||
60 | this.openedModal = this.modalService.open(this.confirmModal, { centered: true }) | |
61 | ||
62 | this.openedModal.result | |
63 | .then(() => this.confirmService.confirmResponse.next(true)) | |
64 | .catch((reason: string) => { | |
65 | // If the reason was that the user used the back button, we don't care about the confirm dialog result | |
66 | if (!reason || reason !== POP_STATE_MODAL_DISMISS) { | |
67 | this.confirmService.confirmResponse.next(false) | |
68 | } | |
69 | }) | |
70 | } | |
71 | } |