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