forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.js
More file actions
180 lines (151 loc) · 3.58 KB
/
event.js
File metadata and controls
180 lines (151 loc) · 3.58 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
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
171
172
173
174
175
176
177
178
179
180
export const listenOpts = {}
Object.defineProperty(listenOpts, 'passive', {
configurable: true,
get () {
let passive
try {
var opts = Object.defineProperty({}, 'passive', {
get () {
passive = { passive: true }
}
})
window.addEventListener('qtest', null, opts)
window.removeEventListener('qtest', null, opts)
}
catch (e) {}
listenOpts.passive = passive
return passive
},
set (val) {
Object.defineProperty(this, 'passive', {
value: val
})
}
})
export function leftClick (e = window.event) {
return e.button === 0
}
export function middleClick (e = window.event) {
return e.button === 1
}
export function rightClick (e = window.event) {
return e.button === 2
}
export function getEventKey (e = window.event) {
return e.which || e.keyCode
}
export function position (e = window.event) {
let posx, posy
if (e.touches && e.touches[0]) {
e = e.touches[0]
}
else if (e.changedTouches && e.changedTouches[0]) {
e = e.changedTouches[0]
}
if (e.clientX || e.clientY) {
posx = e.clientX
posy = e.clientY
}
else if (e.pageX || e.pageY) {
posx = e.pageX - document.body.scrollLeft - document.documentElement.scrollLeft
posy = e.pageY - document.body.scrollTop - document.documentElement.scrollTop
}
else {
const offset = targetElement(e).getBoundingClientRect()
posx = ((offset.right - offset.left) / 2) + offset.left
posy = ((offset.bottom - offset.top) / 2) + offset.top
}
return {
top: posy,
left: posx
}
}
export function targetElement (e = window.event) {
let target
if (e.target) {
target = e.target
}
else if (e.srcElement) {
target = e.srcElement
}
// defeat Safari bug
if (target.nodeType === 3) {
target = target.parentNode
}
return target
}
export function getEventPath (e = window.event) {
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
PIXEL_STEP = 10,
LINE_HEIGHT = 40,
PAGE_HEIGHT = 800
export function getMouseWheelDistance (e = window.event) {
var
sX = 0, sY = 0, // spinX, spinY
pX = 0, pY = 0 // pixelX, pixelY
// Legacy
if ('detail' in e) { sY = e.detail }
if ('wheelDelta' in e) { sY = -e.wheelDelta / 120 }
if ('wheelDeltaY' in e) { sY = -e.wheelDeltaY / 120 }
if ('wheelDeltaX' in e) { sX = -e.wheelDeltaX / 120 }
// side scrolling on FF with DOMMouseScroll
if ('axis' in e && e.axis === e.HORIZONTAL_AXIS) {
sX = sY
sY = 0
}
pX = sX * PIXEL_STEP
pY = sY * PIXEL_STEP
if ('deltaY' in e) { pY = e.deltaY }
if ('deltaX' in e) { pX = e.deltaX }
if ((pX || pY) && e.deltaMode) {
if (e.deltaMode === 1) { // delta in LINE units
pX *= LINE_HEIGHT
pY *= LINE_HEIGHT
}
else { // delta in PAGE units
pX *= PAGE_HEIGHT
pY *= PAGE_HEIGHT
}
}
// Fall-back if spin cannot be determined
if (pX && !sX) { sX = (pX < 1) ? -1 : 1 }
if (pY && !sY) { sY = (pY < 1) ? -1 : 1 }
/*
* spinX -- normalized spin speed (use for zoom) - x plane
* spinY -- " - y plane
* pixelX -- normalized distance (to pixels) - x plane
* pixelY -- " - y plane
*/
return {
spinX: sX,
spinY: sY,
pixelX: pX,
pixelY: pY
}
}
export function stopAndPrevent (e = window.event) {
if (!e) {
return
}
e.preventDefault()
e.stopPropagation()
}