3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('event-flick', function(Y) {
10
* The gestures module provides gesture events such as "flick", which normalize user interactions
11
* across touch and mouse or pointer based input devices. This layer can be used by application developers
12
* to build input device agnostic components which behave the same in response to either touch or mouse based
15
* <p>Documentation for events added by this module can be found in the event document for the <a href="YUI.html#events">YUI</a> global.</p>
17
* @module event-gestures
21
* Adds support for a "flick" event, which is fired at the end of a touch or mouse based flick gesture, and provides
22
* velocity of the flick, along with distance and time information.
24
* @module event-gestures
25
* @submodule event-flick
28
var EVENT = ("ontouchstart" in Y.config.win && !Y.UA.chrome) ? {
42
OWNER_DOCUMENT = "ownerDocument",
43
MIN_VELOCITY = "minVelocity",
44
MIN_DISTANCE = "minDistance",
45
PREVENT_DEFAULT = "preventDefault",
48
_FLICK_START_HANDLE = "_fsh",
49
_FLICK_END_HANDLE = "_feh",
50
_FLICK_MOVE_HANDLE = "_fmh",
52
NODE_TYPE = "nodeType";
55
* Sets up a "flick" event, that is fired whenever the user initiates a flick gesture on the node
56
* where the listener is attached. The subscriber can specify a minimum distance or velocity for
57
* which the event is to be fired. The subscriber can also specify if there is a particular axis which
58
* they are interested in - "x" or "y". If no axis is specified, the axis along which there was most distance
61
* <p>It is recommended that you use Y.bind to set up context and additional arguments for your event handler,
62
* however if you want to pass the context and arguments as additional signature arguments to "on",
63
* you need to provide a null value for the configuration object, e.g: <code>node.on("flick", fn, null, context, arg1, arg2, arg3)</code></p>
67
* @param type {string} "flick"
68
* @param fn {function} The method the event invokes. It receives an event facade with an e.flick object containing the flick related properties: e.flick.time, e.flick.distance, e.flick.velocity and e.flick.axis, e.flick.start.
69
* @param cfg {Object} Optional. An object which specifies any of the following:
71
* <dt>minDistance (in pixels, defaults to 10)</dt>
72
* <dd>The minimum distance between start and end points, which would qualify the gesture as a flick.</dd>
73
* <dt>minVelocity (in pixels/ms, defaults to 0)</dt>
74
* <dd>The minimum velocity which would qualify the gesture as a flick.</dd>
75
* <dt>preventDefault (defaults to false)</dt>
76
* <dd>Can be set to true/false to prevent default behavior as soon as the touchstart/touchend or mousedown/mouseup is received so that things like scrolling or text selection can be
77
* prevented. This property can also be set to a function, which returns true or false, based on the event facade passed to it.</dd>
78
* <dt>axis (no default)</dt>
79
* <dd>Can be set to "x" or "y" if you want to constrain the flick velocity and distance to a single axis. If not
80
* defined, the axis along which the maximum distance was covered is used.</dd>
82
* @return {EventHandle} the detach handle
85
Y.Event.define('flick', {
87
on: function (node, subscriber, ce) {
89
var startHandle = node.on(EVENT[START],
96
subscriber[_FLICK_START_HANDLE] = startHandle;
99
detach: function (node, subscriber, ce) {
101
var startHandle = subscriber[_FLICK_START_HANDLE],
102
endHandle = subscriber[_FLICK_END_HANDLE];
105
startHandle.detach();
106
subscriber[_FLICK_START_HANDLE] = null;
111
subscriber[_FLICK_END_HANDLE] = null;
115
processArgs: function(args) {
116
var params = (args.length > 3) ? Y.merge(args.splice(3, 1)[0]) : {};
118
if (!(MIN_VELOCITY in params)) {
119
params[MIN_VELOCITY] = this.MIN_VELOCITY;
122
if (!(MIN_DISTANCE in params)) {
123
params[MIN_DISTANCE] = this.MIN_DISTANCE;
126
if (!(PREVENT_DEFAULT in params)) {
127
params[PREVENT_DEFAULT] = this.PREVENT_DEFAULT;
133
_onStart: function(e, node, subscriber, ce) {
135
var start = true, // always true for mouse
139
preventDefault = subscriber._extra.preventDefault,
143
start = (e.touches.length === 1);
149
if (preventDefault) {
150
// preventDefault is a boolean or function
151
if (!preventDefault.call || preventDefault(e)) {
152
origE.preventDefault();
157
time : new Date().getTime()
160
subscriber[_FLICK_START] = e;
162
endHandle = subscriber[_FLICK_END_HANDLE];
164
doc = (node.get(NODE_TYPE) === 9) ? node : node.get(OWNER_DOCUMENT);
166
endHandle = doc.on(EVENT[END], Y.bind(this._onEnd, this), null, node, subscriber, ce);
167
subscriber[_FLICK_END_HANDLE] = endHandle;
170
subscriber[_FLICK_MOVE_HANDLE] = doc.once(EVENT[MOVE], Y.bind(this._onMove, this), null, node, subscriber, ce);
174
_onMove: function(e, node, subscriber, ce) {
175
var start = subscriber[_FLICK_START];
177
// Start timing from first move.
178
if (start && start.flick) {
179
start.flick.time = new Date().getTime();
183
_onEnd: function(e, node, subscriber, ce) {
185
var endTime = new Date().getTime(),
186
start = subscriber[_FLICK_START],
197
moveHandle = subscriber[_FLICK_MOVE_HANDLE];
201
delete subscriber[_FLICK_MOVE_HANDLE];
206
if (e.changedTouches) {
207
if (e.changedTouches.length === 1 && e.touches.length === 0) {
208
endEvent = e.changedTouches[0];
216
params = subscriber._extra;
217
preventDefault = params[PREVENT_DEFAULT];
219
if (preventDefault) {
220
// preventDefault is a boolean or function
221
if (!preventDefault.call || preventDefault(e)) {
226
startTime = start.flick.time;
227
endTime = new Date().getTime();
228
time = endTime - startTime;
231
endEvent.pageX - start.pageX,
232
endEvent.pageY - start.pageY
238
axis = (Math.abs(xyDistance[0]) >= Math.abs(xyDistance[1])) ? 'x' : 'y';
241
distance = xyDistance[(axis === 'x') ? 0 : 1];
242
velocity = (time !== 0) ? distance/time : 0;
244
if (isFinite(velocity) && (Math.abs(distance) >= params[MIN_DISTANCE]) && (Math.abs(velocity) >= params[MIN_VELOCITY])) {
259
subscriber[_FLICK_START] = null;
266
PREVENT_DEFAULT : false
270
}, '3.4.1' ,{requires:['node-base','event-touch','event-synthetic']});