1- import { Directive } from "vue" ;
1+ import type { Directive , DirectiveBinding } from "vue" ;
22import { noop , clickNode , cloneObj } from './utilities'
33import { CONFIRM_TYPES , DIRECTIVE_ATTRIBUTE_KEY } from './constants'
44import type { PromiseDialog } from './promise.dialog'
5- import type { DialogResolverPayload } from "./interface" ;
5+ import type { DialogOptions , DialogResolverPayload } from "./interface" ;
66
77
8+ type Binding = DirectiveBinding < DialogOptions | string >
9+
810export class ConfirmDirective {
911 shouldIgnoreClick = false
1012
1113 constructor ( private readonly dialog : PromiseDialog ) { }
1214
13- private getConfirmMessage ( binding : unknown ) {
15+ private getConfirmMessage ( binding : Binding ) {
1416 if ( binding . value && binding . value . message ) {
1517 return binding . value . message
1618 }
@@ -30,9 +32,12 @@ export class ConfirmDirective {
3032 return options
3133 }
3234
33- private getThenCallback ( binding , el ) : ( dialog : unknown ) => void {
35+ private getProceedCallback ( binding : Binding , el : HTMLElement ) : ( dialog : unknown ) => void {
3436 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+ }
3641 }
3742 return ( dialog : DialogResolverPayload ) => {
3843 // If we got here, it means the default action is to be executed
@@ -44,40 +49,40 @@ export class ConfirmDirective {
4449 }
4550 }
4651
47- private getCatchCallback ( binding ?: unknown ) {
52+ private getCancelCallback ( binding ?: Binding ) {
4853 if ( binding ?. value && binding . value . cancel ) {
4954 return binding . value . cancel
5055 }
5156 return noop
5257 }
5358
54- private clickHandler ( event , el , binding ) {
59+ private clickHandler ( event , el , binding : Binding ) {
5560 if ( this . shouldIgnoreClick ) return
5661 event . preventDefault ( )
5762 event . stopImmediatePropagation ( )
5863
5964 const options = this . getOptions ( binding )
6065 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 )
6368
6469 this . dialog . confirm ( confirmMessage , options )
6570 . 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 )
6873 } )
6974 }
7075
7176 public static createInstaller ( dialog : PromiseDialog ) : Directive {
7277 const directive = new ConfirmDirective ( dialog )
7378 return {
74- mounted : ( el , binding ) => {
79+ mounted : ( el , binding : Binding ) => {
7580 el [ DIRECTIVE_ATTRIBUTE_KEY ] = el [ DIRECTIVE_ATTRIBUTE_KEY ] || { }
7681 el [ DIRECTIVE_ATTRIBUTE_KEY ] . clickHandler = event => directive . clickHandler ( event , el , binding )
7782
7883 el . addEventListener ( 'click' , el [ DIRECTIVE_ATTRIBUTE_KEY ] . clickHandler , true )
7984 } ,
80- updated : ( el , binding ) => {
85+ updated : ( el , binding : Binding ) => {
8186 el . removeEventListener ( 'click' , el [ DIRECTIVE_ATTRIBUTE_KEY ] . clickHandler , true )
8287 el [ DIRECTIVE_ATTRIBUTE_KEY ] . clickHandler = event => directive . clickHandler ( event , el , binding )
8388 el . addEventListener ( 'click' , el [ DIRECTIVE_ATTRIBUTE_KEY ] . clickHandler , true )
0 commit comments