]>
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' | |
5769e1db | 6 | |
5769e1db C |
7 | @Component({ |
8 | selector: 'my-confirm', | |
59aa1e5e | 9 | templateUrl: './confirm.component.html', |
1f30a185 | 10 | styleUrls: [ './confirm.component.scss' ] |
5769e1db C |
11 | }) |
12 | export class ConfirmComponent implements OnInit { | |
63347a0f | 13 | @ViewChild('confirmModal') confirmModal: ElementRef |
5769e1db | 14 | |
df98563e C |
15 | title = '' |
16 | message = '' | |
1f30a185 C |
17 | expectedInputValue = '' |
18 | inputLabel = '' | |
19 | ||
20 | inputValue = '' | |
22b59e80 | 21 | confirmButtonText = '' |
5769e1db | 22 | |
63347a0f C |
23 | private openedModal: NgbModalRef |
24 | ||
b1d40cff | 25 | constructor ( |
63347a0f | 26 | private modalService: NgbModal, |
b1d40cff C |
27 | private confirmService: ConfirmService, |
28 | private i18n: I18n | |
23db998f | 29 | ) { } |
5769e1db | 30 | |
df98563e | 31 | ngOnInit () { |
5769e1db | 32 | this.confirmService.showConfirm.subscribe( |
22b59e80 | 33 | ({ title, message, expectedInputValue, inputLabel, confirmButtonText }) => { |
df98563e C |
34 | this.title = title |
35 | this.message = message | |
5769e1db | 36 | |
1f30a185 C |
37 | this.inputLabel = inputLabel |
38 | this.expectedInputValue = expectedInputValue | |
39 | ||
b1d40cff | 40 | this.confirmButtonText = confirmButtonText || this.i18n('Confirm') |
22b59e80 | 41 | |
df98563e | 42 | this.showModal() |
5769e1db | 43 | } |
df98563e | 44 | ) |
5769e1db C |
45 | } |
46 | ||
63347a0f | 47 | @HostListener('document:keydown.enter') |
df98563e | 48 | confirm () { |
63347a0f | 49 | if (this.openedModal) this.openedModal.close() |
5769e1db C |
50 | } |
51 | ||
1f30a185 C |
52 | isConfirmationDisabled () { |
53 | // No input validation | |
54 | if (!this.inputLabel || !this.expectedInputValue) return false | |
55 | ||
56 | return this.expectedInputValue !== this.inputValue | |
57 | } | |
58 | ||
df98563e | 59 | showModal () { |
a7dbc7df | 60 | this.inputValue = '' |
5769e1db | 61 | |
63347a0f C |
62 | this.openedModal = this.modalService.open(this.confirmModal) |
63 | ||
64 | this.openedModal.result | |
65 | .then(() => this.confirmService.confirmResponse.next(true)) | |
66 | .catch(() => this.confirmService.confirmResponse.next(false)) | |
5769e1db C |
67 | } |
68 | } |