]>
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' |
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 { | |
df98563e | 13 | @ViewChild('confirmModal') confirmModal: ModalDirective |
5769e1db | 14 | |
df98563e C |
15 | title = '' |
16 | message = '' | |
1f30a185 C |
17 | expectedInputValue = '' |
18 | inputLabel = '' | |
19 | ||
20 | inputValue = '' | |
22b59e80 | 21 | confirmButtonText = '' |
5769e1db C |
22 | |
23 | constructor (private confirmService: ConfirmService) { | |
24 | // Empty | |
25 | } | |
26 | ||
df98563e | 27 | ngOnInit () { |
5769e1db C |
28 | this.confirmModal.config = { |
29 | backdrop: 'static', | |
30 | keyboard: false | |
df98563e | 31 | } |
5769e1db C |
32 | |
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 | ||
22b59e80 C |
41 | this.confirmButtonText = confirmButtonText || 'Confirm' |
42 | ||
df98563e | 43 | this.showModal() |
5769e1db | 44 | } |
df98563e | 45 | ) |
5769e1db C |
46 | } |
47 | ||
48 | @HostListener('keydown.enter') | |
df98563e C |
49 | confirm () { |
50 | this.confirmService.confirmResponse.next(true) | |
51 | this.hideModal() | |
5769e1db C |
52 | } |
53 | ||
54 | @HostListener('keydown.esc') | |
6599f096 | 55 | cancel () { |
df98563e C |
56 | this.confirmService.confirmResponse.next(false) |
57 | this.hideModal() | |
5769e1db C |
58 | } |
59 | ||
1f30a185 C |
60 | isConfirmationDisabled () { |
61 | // No input validation | |
62 | if (!this.inputLabel || !this.expectedInputValue) return false | |
63 | ||
64 | return this.expectedInputValue !== this.inputValue | |
65 | } | |
66 | ||
df98563e C |
67 | showModal () { |
68 | this.confirmModal.show() | |
5769e1db C |
69 | } |
70 | ||
df98563e C |
71 | hideModal () { |
72 | this.confirmModal.hide() | |
5769e1db C |
73 | } |
74 | } |