]>
Commit | Line | Data |
---|---|---|
1 | import { Component, HostListener, OnInit, ViewChild } from '@angular/core' | |
2 | ||
3 | import { ModalDirective } from 'ngx-bootstrap/modal' | |
4 | ||
5 | import { ConfirmService } from './confirm.service' | |
6 | ||
7 | @Component({ | |
8 | selector: 'my-confirm', | |
9 | templateUrl: './confirm.component.html', | |
10 | styleUrls: [ './confirm.component.scss' ] | |
11 | }) | |
12 | export class ConfirmComponent implements OnInit { | |
13 | @ViewChild('confirmModal') confirmModal: ModalDirective | |
14 | ||
15 | title = '' | |
16 | message = '' | |
17 | expectedInputValue = '' | |
18 | inputLabel = '' | |
19 | ||
20 | inputValue = '' | |
21 | confirmButtonText = '' | |
22 | ||
23 | constructor (private confirmService: ConfirmService) { | |
24 | // Empty | |
25 | } | |
26 | ||
27 | ngOnInit () { | |
28 | this.confirmModal.config = { | |
29 | backdrop: 'static', | |
30 | keyboard: false | |
31 | } | |
32 | ||
33 | this.confirmService.showConfirm.subscribe( | |
34 | ({ title, message, expectedInputValue, inputLabel, confirmButtonText }) => { | |
35 | this.title = title | |
36 | this.message = message | |
37 | ||
38 | this.inputLabel = inputLabel | |
39 | this.expectedInputValue = expectedInputValue | |
40 | ||
41 | this.confirmButtonText = confirmButtonText || 'Confirm' | |
42 | ||
43 | this.showModal() | |
44 | } | |
45 | ) | |
46 | } | |
47 | ||
48 | @HostListener('keydown.enter') | |
49 | confirm () { | |
50 | this.confirmService.confirmResponse.next(true) | |
51 | this.hideModal() | |
52 | } | |
53 | ||
54 | @HostListener('keydown.esc') | |
55 | cancel () { | |
56 | this.confirmService.confirmResponse.next(false) | |
57 | this.hideModal() | |
58 | } | |
59 | ||
60 | isConfirmationDisabled () { | |
61 | // No input validation | |
62 | if (!this.inputLabel || !this.expectedInputValue) return false | |
63 | ||
64 | return this.expectedInputValue !== this.inputValue | |
65 | } | |
66 | ||
67 | showModal () { | |
68 | this.confirmModal.show() | |
69 | } | |
70 | ||
71 | hideModal () { | |
72 | this.confirmModal.hide() | |
73 | } | |
74 | } |