]>
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 | ||
22 | constructor (private confirmService: ConfirmService) { | |
23 | // Empty | |
24 | } | |
25 | ||
26 | ngOnInit () { | |
27 | this.confirmModal.config = { | |
28 | backdrop: 'static', | |
29 | keyboard: false | |
30 | } | |
31 | ||
32 | this.confirmService.showConfirm.subscribe( | |
33 | ({ title, message, expectedInputValue, inputLabel }) => { | |
34 | this.title = title | |
35 | this.message = message | |
36 | ||
37 | this.inputLabel = inputLabel | |
38 | this.expectedInputValue = expectedInputValue | |
39 | ||
40 | this.showModal() | |
41 | } | |
42 | ) | |
43 | } | |
44 | ||
45 | @HostListener('keydown.enter') | |
46 | confirm () { | |
47 | this.confirmService.confirmResponse.next(true) | |
48 | this.hideModal() | |
49 | } | |
50 | ||
51 | @HostListener('keydown.esc') | |
52 | cancel () { | |
53 | this.confirmService.confirmResponse.next(false) | |
54 | this.hideModal() | |
55 | } | |
56 | ||
57 | isConfirmationDisabled () { | |
58 | // No input validation | |
59 | if (!this.inputLabel || !this.expectedInputValue) return false | |
60 | ||
61 | return this.expectedInputValue !== this.inputValue | |
62 | } | |
63 | ||
64 | showModal () { | |
65 | this.confirmModal.show() | |
66 | } | |
67 | ||
68 | hideModal () { | |
69 | this.confirmModal.hide() | |
70 | } | |
71 | } |