1
/* YUI 3.9.1 (build 5852) Copyright 2013 Yahoo! Inc. http://yuilibrary.com/license/ */
2
YUI.add('event-flick', function (Y, NAME) {
5
* The gestures module provides gesture events such as "flick", which normalize user interactions
6
* across touch and mouse or pointer based input devices. This layer can be used by application developers
7
* to build input device agnostic components which behave the same in response to either touch or mouse based
10
* <p>Documentation for events added by this module can be found in the event document for the <a href="../classes/YUI.html#events">YUI</a> global.</p>
15
YUI().use('event-flick', function (Y) {
16
Y.one('#myNode').on('flick', function (e) {
21
* @module event-gestures
25
* Adds support for a "flick" event, which is fired at the end of a touch or mouse based flick gesture, and provides
26
* velocity of the flick, along with distance and time information.
28
* <p>Documentation for the flick event can be found on the <a href="../classes/YUI.html#event_flick">YUI</a> global,
29
* along with the other supported events.</p>
31
* @module event-gestures
32
* @submodule event-flick
34
var GESTURE_MAP = Y.Event._GESTURE_MAP,
36
start: GESTURE_MAP.start,
38
move: GESTURE_MAP.move
44
OWNER_DOCUMENT = "ownerDocument",
45
MIN_VELOCITY = "minVelocity",
46
MIN_DISTANCE = "minDistance",
47
PREVENT_DEFAULT = "preventDefault",
50
_FLICK_START_HANDLE = "_fsh",
51
_FLICK_END_HANDLE = "_feh",
52
_FLICK_MOVE_HANDLE = "_fmh",
54
NODE_TYPE = "nodeType";
57
* Sets up a "flick" event, that is fired whenever the user initiates a flick gesture on the node
58
* where the listener is attached. The subscriber can specify a minimum distance or velocity for
59
* which the event is to be fired. The subscriber can also specify if there is a particular axis which
60
* they are interested in - "x" or "y". If no axis is specified, the axis along which there was most distance
63
* <p>It is recommended that you use Y.bind to set up context and additional arguments for your event handler,
64
* however if you want to pass the context and arguments as additional signature arguments to "on",
65
* 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>
69
* @param type {string} "flick"
70
* @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.
71
* @param cfg {Object} Optional. An object which specifies any of the following:
73
* <dt>minDistance (in pixels, defaults to 10)</dt>
74
* <dd>The minimum distance between start and end points, which would qualify the gesture as a flick.</dd>
75
* <dt>minVelocity (in pixels/ms, defaults to 0)</dt>
76
* <dd>The minimum velocity which would qualify the gesture as a flick.</dd>
77
* <dt>preventDefault (defaults to false)</dt>
78
* <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
79
* prevented. This property can also be set to a function, which returns true or false, based on the event facade passed to it.</dd>
80
* <dt>axis (no default)</dt>
81
* <dd>Can be set to "x" or "y" if you want to constrain the flick velocity and distance to a single axis. If not
82
* defined, the axis along which the maximum distance was covered is used.</dd>
84
* @return {EventHandle} the detach handle
87
Y.Event.define('flick', {
89
on: function (node, subscriber, ce) {
91
var startHandle = node.on(EVENT[START],
98
subscriber[_FLICK_START_HANDLE] = startHandle;
101
detach: function (node, subscriber, ce) {
103
var startHandle = subscriber[_FLICK_START_HANDLE],
104
endHandle = subscriber[_FLICK_END_HANDLE];
107
startHandle.detach();
108
subscriber[_FLICK_START_HANDLE] = null;
113
subscriber[_FLICK_END_HANDLE] = null;
117
processArgs: function(args) {
118
var params = (args.length > 3) ? Y.merge(args.splice(3, 1)[0]) : {};
120
if (!(MIN_VELOCITY in params)) {
121
params[MIN_VELOCITY] = this.MIN_VELOCITY;
124
if (!(MIN_DISTANCE in params)) {
125
params[MIN_DISTANCE] = this.MIN_DISTANCE;
128
if (!(PREVENT_DEFAULT in params)) {
129
params[PREVENT_DEFAULT] = this.PREVENT_DEFAULT;
135
_onStart: function(e, node, subscriber, ce) {
137
var start = true, // always true for mouse
141
preventDefault = subscriber._extra.preventDefault,
145
start = (e.touches.length === 1);
151
if (preventDefault) {
152
// preventDefault is a boolean or function
153
if (!preventDefault.call || preventDefault(e)) {
154
origE.preventDefault();
159
time : new Date().getTime()
162
subscriber[_FLICK_START] = e;
164
endHandle = subscriber[_FLICK_END_HANDLE];
166
doc = (node.get(NODE_TYPE) === 9) ? node : node.get(OWNER_DOCUMENT);
168
endHandle = doc.on(EVENT[END], Y.bind(this._onEnd, this), null, node, subscriber, ce);
169
subscriber[_FLICK_END_HANDLE] = endHandle;
172
subscriber[_FLICK_MOVE_HANDLE] = doc.once(EVENT[MOVE], Y.bind(this._onMove, this), null, node, subscriber, ce);
176
_onMove: function(e, node, subscriber, ce) {
177
var start = subscriber[_FLICK_START];
179
// Start timing from first move.
180
if (start && start.flick) {
181
start.flick.time = new Date().getTime();
185
_onEnd: function(e, node, subscriber, ce) {
187
var endTime = new Date().getTime(),
188
start = subscriber[_FLICK_START],
199
moveHandle = subscriber[_FLICK_MOVE_HANDLE];
203
delete subscriber[_FLICK_MOVE_HANDLE];
208
if (e.changedTouches) {
209
if (e.changedTouches.length === 1 && e.touches.length === 0) {
210
endEvent = e.changedTouches[0];
218
params = subscriber._extra;
219
preventDefault = params[PREVENT_DEFAULT];
221
if (preventDefault) {
222
// preventDefault is a boolean or function
223
if (!preventDefault.call || preventDefault(e)) {
228
startTime = start.flick.time;
229
endTime = new Date().getTime();
230
time = endTime - startTime;
233
endEvent.pageX - start.pageX,
234
endEvent.pageY - start.pageY
240
axis = (Math.abs(xyDistance[0]) >= Math.abs(xyDistance[1])) ? 'x' : 'y';
243
distance = xyDistance[(axis === 'x') ? 0 : 1];
244
velocity = (time !== 0) ? distance/time : 0;
246
if (isFinite(velocity) && (Math.abs(distance) >= params[MIN_DISTANCE]) && (Math.abs(velocity) >= params[MIN_VELOCITY])) {
261
subscriber[_FLICK_START] = null;
268
PREVENT_DEFAULT : false
272
}, '3.9.1', {"requires": ["node-base", "event-touch", "event-synthetic"]});