-
Notifications
You must be signed in to change notification settings - Fork 7
/
nanodrag.js
162 lines (137 loc) · 4.16 KB
/
nanodrag.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
const window = require('global/window')
const document = require('global/document')
const Nanobus = require('nanobus')
const noop = () => {}
const defaultTrackingDelay = 300
const controlEvents = {
touchstart: 'start',
touchend: 'end',
mousedown: 'start',
mouseup: 'end',
mouseleave: 'end',
mouseover: ''
}
const moveEvents = {
touchmove: 'move',
mousemove: 'move'
}
const raf = window.requestAnimationFrame || window.setTimeout
function Nanodrag (targetEl, options) {
if (!(this instanceof Nanodrag)) return new Nanodrag(targetEl)
Nanobus.call(this)
if (typeof targetEl === 'string') {
targetEl = document.querySelector(targetEl)
}
if (!targetEl) {
throw new Error('You must supply a valid selector or DOM Node')
}
options = options || {}
this.targetEl = targetEl
this._active = false
this._startX = null
this._startY = null
this._currentX = null
this._currentY = null
this._direction = {x: '', y: ''}
this._trackingDelay = options.trackingDelay || defaultTrackingDelay
this._passiveMove = options.passive || false
this._leaveTimer = null
this.preventDefault = true
this._touchTriggered = false
Object.keys(controlEvents).forEach((evt) => this.targetEl.addEventListener(evt, this, {passive: true}))
}
Nanodrag.prototype = Object.create(Nanobus.prototype)
Nanodrag.prototype.handleEvent = function (evt) {
const evtType = controlEvents[evt.type] || moveEvents[evt.type]
const pointerData = this._getPointerData(evt)
const evtMethod = (this[`on${evtType}`] || noop).bind(this)
if (evt.type.startsWith('touch')) {
this._touchTriggered = true
}
// iOS fires both `mousedown` and `touchstart`, so if we've gotten a touch
// event, we ignore mouse events
if (this._touchTriggered && evt.type.startsWith('mouse')) {
return
}
if (evt.type === 'mouseleave' && this._active) {
this._leaveTimer = window.setTimeout(() => evtMethod(evt, pointerData), this._trackingDelay)
return
} else if (this._leaveTimer !== null) {
window.clearTimeout(this._leaveTimer)
this._leaveTimer = null
}
evtMethod(evt, pointerData)
}
Nanodrag.prototype._getPointerData = function (evt) {
if (evt.touches && evt.touches.length > 0) {
return evt.touches[0]
}
const data = {
pageX: evt.screenX + evt.currentTarget.scrollLeft,
pageY: evt.screenY + evt.currentTarget.scrollTop
}
return data
}
Nanodrag.prototype.onstart = function (evt, pointerData) {
this._active = true
this._startX = pointerData.pageX
this._startY = pointerData.pageY
this._currentX = pointerData.pageX
this._currentY = pointerData.pageY
Object.keys(moveEvents).forEach((evt) => {
this.targetEl.addEventListener(evt, this, {passive: this._passiveMove})
})
this.emit('start', {start: {x: this._startX, y: this._startY}})
}
Nanodrag.prototype.onend = function (evt) {
if (this._active) {
const data = {
start: {
x: this._startX,
y: this._startY
},
end: {
x: this._currentX,
y: this._currentY
}
}
Object.keys(moveEvents).forEach((evt) => this.targetEl.removeEventListener(evt, this))
this._active = false
this.emit('end', data)
}
}
Nanodrag.prototype.onmove = function (evt, pointerData, force) {
const update = () => {
this._currentX = pointerData.pageX
this._currentY = pointerData.pageY
const direction = this._getSwipeDirection()
const data = {
direction,
start: {
x: this._startX,
y: this._startY
},
current: {
x: this._currentX,
y: this._currentY}
}
this.emit('move', data)
}
if (this._active) {
if (!this._passiveMove && this.preventDefault) evt.preventDefault()
raf(update)
}
}
Nanodrag.prototype.close = function () {
Object.keys(controlEvents).forEach((evt) => this.targetEl.removeEventListener(evt, this))
this.removeAllListeners()
this._active = false
}
Nanodrag.prototype._getSwipeDirection = function () {
const diffX = this._startX - this._currentX
const diffY = this._startY - this._currentY
const x = diffX > -1 ? 'left' : diffX < 0 ? 'right' : ''
const y = diffY > -1 ? 'up' : diffY < 0 ? 'down' : ''
return {x, y}
}
module.exports = Nanodrag