1
- // Directives
2
-
3
1
import { noop , clickNode , cloneObj } from './utilities'
4
- import { CONFIRM_TYPES } from './constants'
2
+ import { CONFIRM_TYPES , DIRECTIVE_ATTRIBUTE_KEY } from './constants'
3
+
5
4
6
5
const DirectiveDialog = function ( app ) {
7
6
Object . defineProperties ( this , {
8
- Vue : { get : ( ) => app } ,
9
- confirmDefinition : {
10
- get : this . defineConfirm
11
- }
7
+ app : { get : ( ) => app } ,
12
8
} )
13
9
}
14
10
11
+ DirectiveDialog . prototype . shouldIgnoreClick = false
12
+
15
13
DirectiveDialog . prototype . getConfirmMessage = function ( binding ) {
16
14
if ( binding . value && binding . value . message ) {
17
15
return binding . value . message
@@ -40,15 +38,9 @@ DirectiveDialog.prototype.getThenCallback = function (binding, el) {
40
38
// If we got here, it means the default action is to be executed
41
39
// We'll then stop the loader if it's enabled and close the dialog
42
40
dialog . loading && dialog . close ( )
43
-
44
- // Unbind to allow original event
45
- el . removeEventListener ( 'click' , el . VuejsDialog . clickHandler , true )
46
-
47
- // Trigger original event
41
+ this . shouldIgnoreClick = true
48
42
clickNode ( el )
49
-
50
- // Re-bind listener
51
- el . addEventListener ( 'click' , el . VuejsDialog . clickHandler , true )
43
+ this . shouldIgnoreClick = false
52
44
}
53
45
}
54
46
}
@@ -61,6 +53,7 @@ DirectiveDialog.prototype.getCatchCallback = function (binding) {
61
53
}
62
54
63
55
DirectiveDialog . prototype . clickHandler = function ( event , el , binding ) {
56
+ if ( this . shouldIgnoreClick ) return
64
57
event . preventDefault ( )
65
58
event . stopImmediatePropagation ( )
66
59
@@ -69,26 +62,26 @@ DirectiveDialog.prototype.clickHandler = function (event, el, binding) {
69
62
const thenCallback = this . getThenCallback ( binding , el )
70
63
const catchCallback = this . getCatchCallback ( binding )
71
64
72
- this . Vue . dialog
65
+ this . app . config . globalProperties . $ dialog
73
66
. confirm ( confirmMessage , options )
74
67
. then ( thenCallback )
75
68
. catch ( catchCallback )
76
69
}
77
70
78
71
DirectiveDialog . prototype . defineConfirm = function ( ) {
79
72
type BindFn = ( el : unknown , binding : unknown ) => void
80
- const DirectiveDefinition : { bind : BindFn , unbind : BindFn } = { }
81
-
82
- DirectiveDefinition . bind = ( el , binding ) => {
83
- el . VuejsDialog = el . VuejsDialog || { }
73
+ const DirectiveDefinition : { mounted : BindFn , unmounted : BindFn } = {
74
+ mounted : ( el , binding ) => {
75
+ el [ DIRECTIVE_ATTRIBUTE_KEY ] = el [ DIRECTIVE_ATTRIBUTE_KEY ] || { }
84
76
85
- el . VuejsDialog . clickHandler = event => this . clickHandler ( event , el , binding )
77
+ el [ DIRECTIVE_ATTRIBUTE_KEY ] . clickHandler = event => this . clickHandler ( event , el , binding )
86
78
87
- el . addEventListener ( 'click' , el . VuejsDialog . clickHandler , true )
88
- }
89
-
90
- DirectiveDefinition . unbind = ( el ) => {
91
- el . removeEventListener ( 'click' , el . VuejsDialog . clickHandler , true )
79
+ el . addEventListener ( 'click' , el [ DIRECTIVE_ATTRIBUTE_KEY ] . clickHandler , true )
80
+ } ,
81
+ unmounted ( el ) {
82
+ el . removeEventListener ( 'click' , el [ DIRECTIVE_ATTRIBUTE_KEY ] . clickHandler , true )
83
+ delete el [ DIRECTIVE_ATTRIBUTE_KEY ]
84
+ }
92
85
}
93
86
94
87
return DirectiveDefinition
0 commit comments