]>
Commit | Line | Data |
---|---|---|
63347a0f | 1 | import { Component, ElementRef, HostListener, OnInit, ViewChild } from '@angular/core' |
457bb213 | 2 | import { ConfirmService } from '@app/core/confirm/confirm.service' |
b1d40cff | 3 | import { I18n } from '@ngx-translate/i18n-polyfill' |
63347a0f C |
4 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' |
5 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
60c2bc80 | 6 | import { POP_STATE_MODAL_DISMISS } from '@app/shared/misc/constants' |
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, |
b1d40cff C |
28 | private confirmService: ConfirmService, |
29 | private i18n: I18n | |
23db998f | 30 | ) { } |
5769e1db | 31 | |
df98563e | 32 | ngOnInit () { |
5769e1db | 33 | this.confirmService.showConfirm.subscribe( |
22b59e80 | 34 | ({ title, message, expectedInputValue, inputLabel, confirmButtonText }) => { |
df98563e C |
35 | this.title = title |
36 | this.message = message | |
5769e1db | 37 | |
1f30a185 C |
38 | this.inputLabel = inputLabel |
39 | this.expectedInputValue = expectedInputValue | |
40 | ||
b1d40cff | 41 | this.confirmButtonText = confirmButtonText || this.i18n('Confirm') |
22b59e80 | 42 | |
df98563e | 43 | this.showModal() |
5769e1db | 44 | } |
df98563e | 45 | ) |
5769e1db C |
46 | } |
47 | ||
63347a0f | 48 | @HostListener('document:keydown.enter') |
df98563e | 49 | confirm () { |
63347a0f | 50 | if (this.openedModal) this.openedModal.close() |
5769e1db C |
51 | } |
52 | ||
1f30a185 C |
53 | isConfirmationDisabled () { |
54 | // No input validation | |
55 | if (!this.inputLabel || !this.expectedInputValue) return false | |
56 | ||
57 | return this.expectedInputValue !== this.inputValue | |
58 | } | |
59 | ||
df98563e | 60 | showModal () { |
a7dbc7df | 61 | this.inputValue = '' |
5769e1db | 62 | |
54e78847 | 63 | this.openedModal = this.modalService.open(this.confirmModal, { centered: true }) |
63347a0f C |
64 | |
65 | this.openedModal.result | |
66 | .then(() => this.confirmService.confirmResponse.next(true)) | |
60c2bc80 C |
67 | .catch((reason: string) => { |
68 | // If the reason was that the user used the back button, we don't care about the confirm dialog result | |
69 | if (!reason || reason !== POP_STATE_MODAL_DISMISS) { | |
70 | this.confirmService.confirmResponse.next(false) | |
71 | } | |
72 | }) | |
5769e1db C |
73 | } |
74 | } |