]>
Commit | Line | Data |
---|---|---|
df98563e | 1 | import { Component, HostListener, OnInit, ViewChild } from '@angular/core' |
5769e1db | 2 | |
df98563e | 3 | import { ModalDirective } from 'ngx-bootstrap/modal' |
5769e1db | 4 | |
df98563e | 5 | import { ConfirmService } from './confirm.service' |
b1d40cff | 6 | import { I18n } from '@ngx-translate/i18n-polyfill' |
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 { | |
df98563e | 14 | @ViewChild('confirmModal') confirmModal: ModalDirective |
5769e1db | 15 | |
df98563e C |
16 | title = '' |
17 | message = '' | |
1f30a185 C |
18 | expectedInputValue = '' |
19 | inputLabel = '' | |
20 | ||
21 | inputValue = '' | |
22b59e80 | 22 | confirmButtonText = '' |
5769e1db | 23 | |
b1d40cff C |
24 | constructor ( |
25 | private confirmService: ConfirmService, | |
26 | private i18n: I18n | |
27 | ) { | |
5769e1db C |
28 | // Empty |
29 | } | |
30 | ||
df98563e | 31 | ngOnInit () { |
5769e1db C |
32 | this.confirmModal.config = { |
33 | backdrop: 'static', | |
34 | keyboard: false | |
df98563e | 35 | } |
5769e1db C |
36 | |
37 | this.confirmService.showConfirm.subscribe( | |
22b59e80 | 38 | ({ title, message, expectedInputValue, inputLabel, confirmButtonText }) => { |
df98563e C |
39 | this.title = title |
40 | this.message = message | |
5769e1db | 41 | |
1f30a185 C |
42 | this.inputLabel = inputLabel |
43 | this.expectedInputValue = expectedInputValue | |
44 | ||
b1d40cff | 45 | this.confirmButtonText = confirmButtonText || this.i18n('Confirm') |
22b59e80 | 46 | |
df98563e | 47 | this.showModal() |
5769e1db | 48 | } |
df98563e | 49 | ) |
5769e1db C |
50 | } |
51 | ||
52 | @HostListener('keydown.enter') | |
df98563e C |
53 | confirm () { |
54 | this.confirmService.confirmResponse.next(true) | |
55 | this.hideModal() | |
5769e1db C |
56 | } |
57 | ||
58 | @HostListener('keydown.esc') | |
6599f096 | 59 | cancel () { |
df98563e C |
60 | this.confirmService.confirmResponse.next(false) |
61 | this.hideModal() | |
5769e1db C |
62 | } |
63 | ||
1f30a185 C |
64 | isConfirmationDisabled () { |
65 | // No input validation | |
66 | if (!this.inputLabel || !this.expectedInputValue) return false | |
67 | ||
68 | return this.expectedInputValue !== this.inputValue | |
69 | } | |
70 | ||
df98563e | 71 | showModal () { |
a7dbc7df | 72 | this.inputValue = '' |
df98563e | 73 | this.confirmModal.show() |
5769e1db C |
74 | } |
75 | ||
df98563e C |
76 | hideModal () { |
77 | this.confirmModal.hide() | |
5769e1db C |
78 | } |
79 | } |