-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
147 lines (116 loc) · 4.34 KB
/
index.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
'use strict';
// Options
// listener
// multiplier
function Drag(options) {
this._event = {
X: 0,
Y: 0,
cursorDown: false,
initialX: 0, // The X position of the cursor on start.
initialY: 0, // The Y position of cursor on start.
originalEvent: null
};
this.initialized = false;
this.curDown = false; // True if cursor is down.
this.numListeners = 0;
this.listeners = [];
this.listener = options.listener || window;
this.multiplier = options.multiplier || 1;
// Detect interaction type.
this.hasMouseEvent = 'onmousedown' in document;
this.hasTouchEvent = 'ontouchstart' in document;
this.hasTouchWinEvent = navigator.msMaxTouchPoints && navigator.msMaxTouchPoints > 1;
this.hasPointerEvent = !!window.navigator.msPointerEnabled;
this.isTouch = this.hasTouchEvent || this.hasTouchWinEvent || this.hasPointerEvent;
this.msTouchAction = null;
// Context binding.
this._onDragStart = this._onDragStart.bind(this);
this._onDrag = this._onDrag.bind(this);
this._onDragEnd = this._onDragEnd.bind(this);
this._onMouseLeave = this._onMouseLeave.bind(this);
this._notify = this._notify.bind(this);
}
Drag.prototype.on = function(f) {
if(!this.initialized) this._addListeners();
this.listeners.push(f);
this.numListeners = this.listeners.length;
};
Drag.prototype.off = function(f) {
this.listeners.splice(f, 1);
this.numListeners = this.listeners.length;
if(this.numListeners <= 0) this._removeListeners();
};
Drag.prototype._notify = function(e) {
var _e = (this.isTouch && e.targetTouches) ? e.targetTouches[0] : e;
this._event.X = (_e.pageX - this._event.initialX) * this.multiplier;
this._event.Y = (_e.pageY - this._event.initialY) * this.multiplier;
this._event.originalEvent = e;
this._event.cursorDown = this.curDown;
this._event.initialX = _e.pageX;
this._event.initialY = _e.pageY;
for(var i = 0; i < this.numListeners; i++) {
this.listeners[i](this._event);
}
};
// Event Listeners.
Drag.prototype._onDrag = function(e) {
if(this.curDown){
e.preventDefault();
this._notify(e);
}
};
Drag.prototype._onDragStart = function(e) {
var _e = (this.isTouch && e.targetTouches) ? e.targetTouches[0] : e;
this._event.initialX = _e.pageX;
this._event.initialY = _e.pageY;
this.curDown = true;
//this._notify(e);
};
Drag.prototype._onDragEnd = function(e) {
this.curDown = false;
//this._notify(e);
};
Drag.prototype._onMouseLeave = function(e) {
this._onDragEnd(e);
};
Drag.prototype._addListeners = function() {
if (this.hasMouseEvent) {
this.listener.addEventListener('mouseleave', this._onMouseLeave);
this.listener.addEventListener('mouseup', this._onDragEnd);
this.listener.addEventListener('mousedown', this._onDragStart);
this.listener.addEventListener('mousemove', this._onDrag);
}
if (this.hasTouchEvent) {
this.listener.addEventListener('touchmove', this._onDrag);
this.listener.addEventListener('touchend', this._onDragEnd);
this.listener.addEventListener('touchstart', this._onDragStart);
}
if (this.hasPointerEvent && this.hasTouchWinEvent) {
this.msTouchAction = this.listener.style.msTouchAction;
this.listener.style.msTouchAction = "none";
this.listener.addEventListener('MSPointerMove', this._onDrag);
this.listener.addEventListener('MSPointerUp', this._onDragEnd);
this.listener.addEventListener('MSPointerDown', this._onDragStart);
}
};
Drag.prototype._removeListeners = function() {
if (this.hasMouseEvent) {
this.listener.removeEventListener('mouseleave', this._onMouseLeave);
this.listener.removeEventListener('mouseup', this._onDragEnd);
this.listener.removeEventListener('mousedown', this._onDragStart);
this.listener.removeEventListener('mousemove', this._onDrag);
}
if (this.hasTouchEvent) {
this.listener.removeEventListener('touchmove', this._onDrag);
this.listener.removeEventListener('touchend', this._onDragEnd);
this.listener.removeEventListener('touchstart', this._onDragStart);
}
if (this.hasPointerEvent && this.hasTouchWinEvent) {
if (this.msTouchAction) this.listener.style.msTouchAction = this.msTouchAction;
this.listener.removeEventListener('MSPointerMove', this._onDrag);
this.listener.removeEventListener('MSPointerUp', this._onDragEnd);
this.listener.removeEventListener('MSPointerDown', this._onDragStart);
}
};
module.exports = Drag;