]>
Commit | Line | Data |
---|---|---|
63347a0f | 1 | import { Component, ElementRef, HostListener, OnInit, ViewChild } from '@angular/core' |
df98563e | 2 | import { ConfirmService } from './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 | |
29 | ) { | |
5769e1db C |
30 | // Empty |
31 | } | |
32 | ||
df98563e | 33 | ngOnInit () { |
5769e1db | 34 | this.confirmService.showConfirm.subscribe( |
22b59e80 | 35 | ({ title, message, expectedInputValue, inputLabel, confirmButtonText }) => { |
df98563e C |
36 | this.title = title |
37 | this.message = message | |
5769e1db | 38 | |
1f30a185 C |
39 | this.inputLabel = inputLabel |
40 | this.expectedInputValue = expectedInputValue | |
41 | ||
b1d40cff | 42 | this.confirmButtonText = confirmButtonText || this.i18n('Confirm') |
22b59e80 | 43 | |
df98563e | 44 | this.showModal() |
5769e1db | 45 | } |
df98563e | 46 | ) |
5769e1db C |
47 | } |
48 | ||
63347a0f | 49 | @HostListener('document:keydown.enter') |
df98563e | 50 | confirm () { |
63347a0f | 51 | if (this.openedModal) this.openedModal.close() |
5769e1db C |
52 | } |
53 | ||
1f30a185 C |
54 | isConfirmationDisabled () { |
55 | // No input validation | |
56 | if (!this.inputLabel || !this.expectedInputValue) return false | |
57 | ||
58 | return this.expectedInputValue !== this.inputValue | |
59 | } | |
60 | ||
df98563e | 61 | showModal () { |
a7dbc7df | 62 | this.inputValue = '' |
5769e1db | 63 | |
63347a0f C |
64 | this.openedModal = this.modalService.open(this.confirmModal) |
65 | ||
66 | this.openedModal.result | |
67 | .then(() => this.confirmService.confirmResponse.next(true)) | |
68 | .catch(() => this.confirmService.confirmResponse.next(false)) | |
5769e1db C |
69 | } |
70 | } |