event-flick-debug.js revision df44ce679e6e095a4e0f1a587961e4d0962b37a9
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff ConniffYUI.add('event-flick', function(Y) {
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff/**
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff * Adds support for a "flick" event, which is fired at the end of a touch or mouse based flick gesture, and provides
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich * velocity of the flick, along with distance and time information.
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich *
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich * @module event-gestures
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich * @submodule event-flick
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich */
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich// TODO: Better way to sniff 'n' switch touch support?
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovichvar EVENT = ("ontouchstart" in Y.config.win) ? {
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff start: "touchstart",
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff end: "touchend"
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff } : {
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff start: "mousedown",
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich end: "mouseup"
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff },
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff START = "start",
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff END = "end",
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff OWNER_DOCUMENT = "ownerDocument",
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich MIN_VELOCITY = "minVelocity",
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff MIN_DISTANCE = "minDistance",
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich _FLICK_START = "_fs",
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff _FLICK_START_HANDLE = "_fsh",
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff _FLICK_END_HANDLE = "_feh",
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff NODE_TYPE = "nodeType";
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff/**
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff * Sets up a "flick" event, that is fired whenever the user initiates a flick gesture on the node
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff * where the listener is attached. The subscriber can specify a minimum distance or velocity for
3600c6bf768f1465933aaf2e3820d9d9f6b97890Allen Rabinovich * which the event is to be fired.
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff *
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff * @event flick
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff * @param type {string} "flick"
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff * @param fn {function} The method the event invokes.
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff * @param cfg {Object} Optional. An object which specifies the minimum distance and/or velocity
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff * of the flick gesture for which the event is to be fired.
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff *
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff * @return {EventHandle} the detach handle
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff */
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff ConniffY.Event.define('flick', {
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff init: function (node, subscriber, ce) {
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff var startHandle = node.on(EVENT[START],
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff this._onStart,
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff this,
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff node,
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff subscriber,
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff ce);
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff node.setData(_FLICK_START_HANDLE, startHandle);
a63b2a9b2e7e42b82930977f426c72f854d30d05Jeff Conniff },
9c46bf9bc0ac9bab8d409ae4ab6d2a2ddc4dc55bJeff Conniff
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff destroy: function (node, subscriber, ce) {
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich var startHandle = node.getData(_FLICK_START_HANDLE),
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich endHandle = node.getData(_FLICK_END_HANDLE);
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich if (startHandle) {
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich startHandle.detach();
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich node.clearData(_FLICK_START_HANDLE);
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich }
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich if (endHandle) {
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich endHandle.detach();
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich node.clearData(_FLICK_END_HANDLE);
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich }
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich },
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich processArgs: function(args) {
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich var params = (args[3]) ? args.splice(3, 1)[0] : {};
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich if (!(MIN_VELOCITY in params)) {
6a0b5391dbcaace88784e407eb97b46fc8ffb619Allen Rabinovich params.minVelocity = this.MIN_VELOCITY;
80ea28fae9dc032e5b99c726c41e492cd59bf49dJeff Conniff }
if (!(MIN_DISTANCE in params)) {
params.minDistance = this.MIN_DISTANCE;
}
Y.log("flick, processArgs : minDistance =" + params.minDistance + ", minVelocity =" + params.minVelocity);
return params;
},
filterSubs: function (subs, args) {
var i,
sub,
params,
absDistance,
e = args[0];
for (i in subs) {
if (subs.hasOwnProperty(i)) {
sub = subs[i];
params = sub._extra;
absDistance = Math.abs(e.distance);
if (e.velocity < params.minVelocity || absDistance < params.minDistance) {
subs[i] = null;
}
}
}
},
_onStart: function(e, node, subscriber, ce) {
var start = true, // always true for mouse
endHandle,
doc,
origE = e;
if (e.touches) {
start = (e.touches.length === 1);
e = e.touches[0];
}
if (start) {
origE.preventDefault();
node.setData(_FLICK_START, {
time : new Date().getTime(),
pageX: e.pageX,
pageY: e.pageY,
clientX: e.clientX,
clientY: e.clientY,
_e : e
});
endHandle = node.getData(_FLICK_END_HANDLE);
if (!endHandle) {
doc = (node.get(NODE_TYPE) === 9) ? node : node.get(OWNER_DOCUMENT);
endHandle = doc.on(EVENT[END], Y.bind(this._onEnd, this), null, node, subscriber, ce);
node.setData(_FLICK_END_HANDLE,endHandle);
}
}
},
_onEnd: function(e, node, subscriber, ce) {
var endTime = new Date().getTime(),
valid = node.getData(_FLICK_START),
start = valid,
endEvent = e,
startTime,
time,
params,
xyDistance,
distance,
absDistance,
velocity,
axis;
if (valid) {
if (e.changedTouches) {
if (e.changedTouches.length === 1 && e.touches.length === 0) {
endEvent = e.changedTouches[0];
} else {
valid = false;
}
}
if (valid) {
endEvent.preventDefault();
startTime = start.time;
endTime = new Date().getTime();
time = endTime - startTime;
params = subscriber._extra;
xyDistance = [
endEvent.pageX - start.pageX,
endEvent.pageY - start.pageY
];
axis = params.axis || (Math.abs(xyDistance[0]) >= Math.abs(xyDistance[1])) ? 'x' : 'y';
distance = xyDistance[(axis === 'x') ? 0 : 1];
absDistance = Math.abs(distance);
velocity = absDistance/time;
if (isFinite(velocity)) {
ce.fire({
distance: distance,
time: time,
direction: distance/absDistance,
velocity: velocity,
axis: axis,
button: e.button,
start: start,
end: {
time: endTime,
clientX: endEvent.clientX,
clientY: endEvent.clientY,
pageX: endEvent.pageX,
pageY: endEvent.pageY,
_e : e
}
});
}
node.clearData(_FLICK_START);
}
}
},
MIN_VELOCITY : 0,
MIN_DISTANCE : 10
});
}, '@VERSION@' ,{requires:['node-base','event-touch','event-synthetic']});