forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClickOutside.js
More file actions
142 lines (117 loc) · 3.68 KB
/
ClickOutside.js
File metadata and controls
142 lines (117 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { client } from '../../plugins/Platform.js'
import { listenOpts } from '../../utils/event.js'
import { getVmOfNode, isVmChildOf } from '../../utils/vm.js'
let timer
const
{ notPassiveCapture, passiveCapture } = listenOpts,
handlers = {
click: [],
focus: []
}
function hasModalsAbove (node) {
while ((node = node.nextElementSibling) !== null) {
if (node.classList.contains('q-dialog--modal')) {
return true
}
}
return false
}
function execHandlers (list, evt) {
for (let i = list.length - 1; i >= 0; i--) {
if (list[i](evt) === void 0) {
return
}
}
}
function globalHandler (evt) {
clearTimeout(timer)
// prevent autofocus on body resulting from blur
if (
evt.type === 'focusin' && (
(client.is.ie === true && evt.target === document.body) ||
evt.target.hasAttribute('tabindex') === true
)
) {
timer = setTimeout(() => {
execHandlers(handlers.focus, evt)
}, client.is.ie === true ? 500 : 200)
}
else {
execHandlers(handlers.click, evt)
}
}
export default {
name: 'click-outside',
bind (el, { value, arg }, vnode) {
const vmEl = vnode.componentInstance || vnode.context
const ctx = {
trigger: value,
toggleEl: arg,
handler (evt) {
const target = evt.target
if (
evt.qClickOutside !== true &&
document.body.contains(target) === true &&
target.nodeType !== 8 &&
// directives that prevent click by using pointer-events none generate click on html element
target !== document.documentElement &&
target.classList.contains('no-pointer-events') === false &&
hasModalsAbove(el) !== true &&
(
ctx.toggleEl === void 0 ||
ctx.toggleEl.contains(target) === false
) &&
(
target === document.body ||
isVmChildOf(getVmOfNode(target), vmEl) === false
)
) {
// mark the event as being processed by clickOutside
// used to prevent refocus after menu close
evt.qClickOutside = true
return ctx.trigger(evt)
}
}
}
if (el.__qclickoutside) {
el.__qclickoutside_old = el.__qclickoutside
}
el.__qclickoutside = ctx
if (handlers.click.length === 0) {
document.addEventListener('mousedown', globalHandler, notPassiveCapture)
document.addEventListener('touchstart', globalHandler, notPassiveCapture)
document.addEventListener('focusin', globalHandler, passiveCapture)
}
handlers.click.push(ctx.handler)
ctx.timerFocusin = setTimeout(() => {
handlers.focus.push(ctx.handler)
}, 500)
},
update (el, { value, oldValue, arg }) {
const ctx = el.__qclickoutside
if (value !== oldValue) {
ctx.trigger = value
}
if (arg !== ctx.arg) {
ctx.toggleEl = arg
}
},
unbind (el) {
const ctx = el.__qclickoutside_old || el.__qclickoutside
if (ctx !== void 0) {
clearTimeout(ctx.timerFocusin)
const
indexClick = handlers.click.findIndex(h => h === ctx.handler),
indexFocus = handlers.focus.findIndex(h => h === ctx.handler)
indexClick > -1 && handlers.click.splice(indexClick, 1)
indexFocus > -1 && handlers.focus.splice(indexFocus, 1)
if (handlers.click.length === 0) {
clearTimeout(timer)
document.removeEventListener('mousedown', globalHandler, notPassiveCapture)
document.removeEventListener('touchstart', globalHandler, notPassiveCapture)
document.removeEventListener('focusin', globalHandler, passiveCapture)
}
delete el[el.__qclickoutside_old ? '__qclickoutside_old' : '__qclickoutside']
}
}
}