-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathkendo.touch.js
166 lines (140 loc) · 4.83 KB
/
kendo.touch.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
import "./kendo.core.js";
import "./kendo.userevents.js";
export const __meta__ = {
id: "touch",
name: "Touch",
category: "mobile",
description: "The kendo Touch widget provides a cross-platform compatible API for handling user-initiated touch events, multi-touch gestures and event sequences (drag, swipe, etc.). ",
depends: [ "core", "userevents" ]
};
(function($, undefined) {
var kendo = window.kendo,
Widget = kendo.ui.Widget,
abs = Math.abs,
MAX_DOUBLE_TAP_DISTANCE = 20;
var Touch = Widget.extend({
init: function(element, options) {
var that = this;
Widget.fn.init.call(that, element, options);
options = that.options;
element = that.element;
that.wrapper = element;
function eventProxy(name) {
return function(e) {
that._triggerTouch(name, e);
};
}
function gestureEventProxy(name) {
return function(e) {
that.trigger(name, { touches: e.touches, distance: e.distance, center: e.center, event: e.event });
};
}
that.events = new kendo.UserEvents(element, {
filter: options.filter,
surface: options.surface,
minHold: options.minHold,
multiTouch: options.multiTouch,
allowSelection: true,
fastTap: options.fastTap,
press: eventProxy("touchstart"),
hold: eventProxy("hold"),
tap: that._tap.bind(that),
gesturestart: gestureEventProxy("gesturestart"),
gesturechange: gestureEventProxy("gesturechange"),
gestureend: gestureEventProxy("gestureend")
});
if (options.enableSwipe) {
that.events.bind("start", that._swipestart.bind(that));
that.events.bind("move", that._swipemove.bind(that));
} else {
that.events.bind("start", that._dragstart.bind(that));
that.events.bind("move", eventProxy("drag"));
that.events.bind("end", eventProxy("dragend"));
}
kendo.notify(that);
},
events: [
"touchstart",
"dragstart",
"drag",
"dragend",
"tap",
"doubletap",
"hold",
"swipe",
"gesturestart",
"gesturechange",
"gestureend"
],
options: {
name: "Touch",
surface: null,
global: false,
fastTap: false,
filter: null,
multiTouch: false,
enableSwipe: false,
minXDelta: 30,
maxYDelta: 20,
maxDuration: 1000,
minHold: 800,
doubleTapTimeout: 800
},
cancel: function() {
this.events.cancel();
},
destroy: function() {
Widget.fn.destroy.call(this);
this.events.destroy();
},
_triggerTouch: function(type, e) {
if (this.trigger(type, { touch: e.touch, event: e.event })) {
e.preventDefault();
}
},
_tap: function(e) {
var that = this,
lastTap = that.lastTap,
touch = e.touch;
if (lastTap &&
(touch.endTime - lastTap.endTime < that.options.doubleTapTimeout) &&
kendo.touchDelta(touch, lastTap).distance < MAX_DOUBLE_TAP_DISTANCE
) {
that._triggerTouch("doubletap", e);
that.lastTap = null;
} else {
that._triggerTouch("tap", e);
that.lastTap = touch;
}
},
_dragstart: function(e) {
this._triggerTouch("dragstart", e);
},
_swipestart: function(e) {
if (abs(e.x.velocity) * 2 >= abs(e.y.velocity)) {
e.sender.capture();
}
},
_swipemove: function(e) {
var that = this,
options = that.options,
touch = e.touch,
duration = e.event.timeStamp - touch.startTime,
direction = touch.x.initialDelta > 0 ? "right" : "left";
if (
abs(touch.x.initialDelta) >= options.minXDelta &&
abs(touch.y.initialDelta) < options.maxYDelta &&
duration < options.maxDuration
)
{
that.trigger("swipe", {
direction: direction,
touch: e.touch
});
touch.cancel();
}
}
});
kendo.ui.plugin(Touch);
})(window.kendo.jQuery);
export default kendo;