1
- import { Directive } from "vue" ;
1
+ import type { Directive , DirectiveBinding } from "vue" ;
2
2
import { noop , clickNode , cloneObj } from './utilities'
3
3
import { CONFIRM_TYPES , DIRECTIVE_ATTRIBUTE_KEY } from './constants'
4
4
import type { PromiseDialog } from './promise.dialog'
5
- import type { DialogResolverPayload } from "./interface" ;
5
+ import type { DialogOptions , DialogResolverPayload } from "./interface" ;
6
6
7
7
8
+ type Binding = DirectiveBinding < DialogOptions | string >
9
+
8
10
export class ConfirmDirective {
9
11
shouldIgnoreClick = false
10
12
11
13
constructor ( private readonly dialog : PromiseDialog ) { }
12
14
13
- private getConfirmMessage ( binding : unknown ) {
15
+ private getConfirmMessage ( binding : Binding ) {
14
16
if ( binding . value && binding . value . message ) {
15
17
return binding . value . message
16
18
}
@@ -30,9 +32,12 @@ export class ConfirmDirective {
30
32
return options
31
33
}
32
34
33
- private getThenCallback ( binding , el ) : ( dialog : unknown ) => void {
35
+ private getProceedCallback ( binding : Binding , el : HTMLElement ) : ( dialog : unknown ) => void {
34
36
if ( binding ?. value && binding . value . ok ) {
35
- return dialog => binding . value . ok ( { ...dialog , node : el } )
37
+ return ( dialog : DialogResolverPayload ) => {
38
+ const okPayload : DialogResolverPayload = { ...dialog , node : el }
39
+ binding . value . ok ( okPayload )
40
+ }
36
41
}
37
42
return ( dialog : DialogResolverPayload ) => {
38
43
// If we got here, it means the default action is to be executed
@@ -44,40 +49,40 @@ export class ConfirmDirective {
44
49
}
45
50
}
46
51
47
- private getCatchCallback ( binding ?: unknown ) {
52
+ private getCancelCallback ( binding ?: Binding ) {
48
53
if ( binding ?. value && binding . value . cancel ) {
49
54
return binding . value . cancel
50
55
}
51
56
return noop
52
57
}
53
58
54
- private clickHandler ( event , el , binding ) {
59
+ private clickHandler ( event , el , binding : Binding ) {
55
60
if ( this . shouldIgnoreClick ) return
56
61
event . preventDefault ( )
57
62
event . stopImmediatePropagation ( )
58
63
59
64
const options = this . getOptions ( binding )
60
65
const confirmMessage = this . getConfirmMessage ( binding )
61
- const thenCallback = this . getThenCallback ( binding , el )
62
- const catchCallback = this . getCatchCallback ( binding )
66
+ const proceedCallback = this . getProceedCallback ( binding , el )
67
+ const cancelCallback = this . getCancelCallback ( binding )
63
68
64
69
this . dialog . confirm ( confirmMessage , options )
65
70
. then ( ( payload ) => {
66
- if ( payload . canceled ) return catchCallback . call ( catchCallback , payload )
67
- ; thenCallback . call ( thenCallback , payload )
71
+ if ( payload . canceled ) return cancelCallback . call ( cancelCallback , payload )
72
+ ; proceedCallback . call ( proceedCallback , payload )
68
73
} )
69
74
}
70
75
71
76
public static createInstaller ( dialog : PromiseDialog ) : Directive {
72
77
const directive = new ConfirmDirective ( dialog )
73
78
return {
74
- mounted : ( el , binding ) => {
79
+ mounted : ( el , binding : Binding ) => {
75
80
el [ DIRECTIVE_ATTRIBUTE_KEY ] = el [ DIRECTIVE_ATTRIBUTE_KEY ] || { }
76
81
el [ DIRECTIVE_ATTRIBUTE_KEY ] . clickHandler = event => directive . clickHandler ( event , el , binding )
77
82
78
83
el . addEventListener ( 'click' , el [ DIRECTIVE_ATTRIBUTE_KEY ] . clickHandler , true )
79
84
} ,
80
- updated : ( el , binding ) => {
85
+ updated : ( el , binding : Binding ) => {
81
86
el . removeEventListener ( 'click' , el [ DIRECTIVE_ATTRIBUTE_KEY ] . clickHandler , true )
82
87
el [ DIRECTIVE_ATTRIBUTE_KEY ] . clickHandler = event => directive . clickHandler ( event , el , binding )
83
88
el . addEventListener ( 'click' , el [ DIRECTIVE_ATTRIBUTE_KEY ] . clickHandler , true )
0 commit comments