-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathevent.js
170 lines (140 loc) · 3.26 KB
/
event.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
export const listenOpts = {
hasPassive: false,
passiveCapture: true,
notPassiveCapture: true
}
try {
const opts = Object.defineProperty({}, 'passive', {
get () {
Object.assign(listenOpts, {
hasPassive: true,
passive: { passive: true },
notPassive: { passive: false },
passiveCapture: { passive: true, capture: true },
notPassiveCapture: { passive: false, capture: true }
})
}
})
window.addEventListener('qtest', null, opts)
window.removeEventListener('qtest', null, opts)
}
catch (_) {}
export function noop () {}
export function leftClick (e) {
return e.button === 0
}
export function middleClick (e) {
return e.button === 1
}
export function rightClick (e) {
return e.button === 2
}
export function position (e) {
if (e.touches && e.touches[ 0 ]) {
e = e.touches[ 0 ]
}
else if (e.changedTouches && e.changedTouches[ 0 ]) {
e = e.changedTouches[ 0 ]
}
else if (e.targetTouches && e.targetTouches[ 0 ]) {
e = e.targetTouches[ 0 ]
}
return {
top: e.clientY,
left: e.clientX
}
}
export function getEventPath (e) {
if (e.path) {
return e.path
}
if (e.composedPath) {
return e.composedPath()
}
const path = []
let el = e.target
while (el) {
path.push(el)
if (el.tagName === 'HTML') {
path.push(document)
path.push(window)
return path
}
el = el.parentElement
}
}
// Reasonable defaults
const
LINE_HEIGHT = 40,
PAGE_HEIGHT = 800
export function getMouseWheelDistance (e) {
let x = e.deltaX, y = e.deltaY
if ((x || y) && e.deltaMode) {
const multiplier = e.deltaMode === 1 ? LINE_HEIGHT : PAGE_HEIGHT
x *= multiplier
y *= multiplier
}
if (e.shiftKey && !x) {
[ y, x ] = [ x, y ]
}
return { x, y }
}
export function stop (e) {
e.stopPropagation()
}
export function prevent (e) {
e.cancelable !== false && e.preventDefault()
}
export function stopAndPrevent (e) {
e.cancelable !== false && e.preventDefault()
e.stopPropagation()
}
export function preventDraggable (el, status) {
if (el === void 0 || (status === true && el.__dragPrevented === true)) {
return
}
const fn = status === true
? el => {
el.__dragPrevented = true
el.addEventListener('dragstart', prevent, listenOpts.notPassiveCapture)
}
: el => {
delete el.__dragPrevented
el.removeEventListener('dragstart', prevent, listenOpts.notPassiveCapture)
}
el.querySelectorAll('a, img').forEach(fn)
}
export function addEvt (ctx, targetName, events) {
const name = `__q_${ targetName }_evt`
ctx[ name ] = ctx[ name ] !== void 0
? ctx[ name ].concat(events)
: events
events.forEach(evt => {
evt[ 0 ].addEventListener(evt[ 1 ], ctx[ evt[ 2 ] ], listenOpts[ evt[ 3 ] ])
})
}
export function cleanEvt (ctx, targetName) {
const name = `__q_${ targetName }_evt`
if (ctx[ name ] !== void 0) {
ctx[ name ].forEach(evt => {
evt[ 0 ].removeEventListener(evt[ 1 ], ctx[ evt[ 2 ] ], listenOpts[ evt[ 3 ] ])
})
ctx[ name ] = void 0
}
}
/*
* also update /types/utils/event.d.ts
*/
export default {
listenOpts,
leftClick,
middleClick,
rightClick,
position,
getEventPath,
getMouseWheelDistance,
stop,
prevent,
stopAndPrevent,
preventDraggable
}