event-gestures-debug.js revision 0e232b16e640a3801393ca223d42fd1e0e9e83c3
45e9809aff7304721fddb95654901b32195c9c7avboxsyncYUI.add('event-flick', function(Y) {
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync/**
45e9809aff7304721fddb95654901b32195c9c7avboxsync * The gestures module provides gesture events such as "flick", which normalize user interactions
45e9809aff7304721fddb95654901b32195c9c7avboxsync * across touch and mouse or pointer based input devices. This layer can be used by application developers
45e9809aff7304721fddb95654901b32195c9c7avboxsync * to build input device agnostic components which behave the same in response to either touch or mouse based
45e9809aff7304721fddb95654901b32195c9c7avboxsync * interaction.
45e9809aff7304721fddb95654901b32195c9c7avboxsync *
45e9809aff7304721fddb95654901b32195c9c7avboxsync * <p>Documentation for events added by this module can be found in the event document for the <a href="YUI.html#events">YUI</event> global.</p>
45e9809aff7304721fddb95654901b32195c9c7avboxsync *
45e9809aff7304721fddb95654901b32195c9c7avboxsync * @module event-gestures
45e9809aff7304721fddb95654901b32195c9c7avboxsync */
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync/**
45e9809aff7304721fddb95654901b32195c9c7avboxsync * Adds support for a "flick" event, which is fired at the end of a touch or mouse based flick gesture, and provides
45e9809aff7304721fddb95654901b32195c9c7avboxsync * velocity of the flick, along with distance and time information.
45e9809aff7304721fddb95654901b32195c9c7avboxsync *
45e9809aff7304721fddb95654901b32195c9c7avboxsync * @module event-gestures
45e9809aff7304721fddb95654901b32195c9c7avboxsync * @submodule event-flick
45e9809aff7304721fddb95654901b32195c9c7avboxsync */
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsyncvar EVENT = ("ontouchstart" in Y.config.win && !Y.UA.chrome) ? {
45e9809aff7304721fddb95654901b32195c9c7avboxsync start: "touchstart",
45e9809aff7304721fddb95654901b32195c9c7avboxsync end: "touchend"
45e9809aff7304721fddb95654901b32195c9c7avboxsync } : {
45e9809aff7304721fddb95654901b32195c9c7avboxsync start: "mousedown",
45e9809aff7304721fddb95654901b32195c9c7avboxsync end: "mouseup"
45e9809aff7304721fddb95654901b32195c9c7avboxsync },
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync START = "start",
45e9809aff7304721fddb95654901b32195c9c7avboxsync END = "end",
45e9809aff7304721fddb95654901b32195c9c7avboxsync
45e9809aff7304721fddb95654901b32195c9c7avboxsync OWNER_DOCUMENT = "ownerDocument",
45e9809aff7304721fddb95654901b32195c9c7avboxsync MIN_VELOCITY = "minVelocity",
45e9809aff7304721fddb95654901b32195c9c7avboxsync MIN_DISTANCE = "minDistance",
45e9809aff7304721fddb95654901b32195c9c7avboxsync
_FLICK_START = "_fs",
_FLICK_START_HANDLE = "_fsh",
_FLICK_END_HANDLE = "_feh",
NODE_TYPE = "nodeType";
/**
* Sets up a "flick" event, that is fired whenever the user initiates a flick gesture on the node
* where the listener is attached. The subscriber can specify a minimum distance or velocity for
* which the event is to be fired. The subscriber can also specify if there is a particular axis which
* they are interested in - "x" or "y". If no axis is specified, the axis along which there was most distance
* covered is used.
*
* @event flick
* @for YUI
* @param type {string} "flick"
* @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.
* @param cfg {Object} Optional. An object which specifies the minimum distance and/or velocity (in px/ms)
* of the flick gesture for which the event is to be fired and an axis of interest. e.g. { minDistance:10, minVelocity:0.5, axis:"x" }.
*
* @return {EventHandle} the detach handle
*/
Y.Event.define('flick', {
on: function (node, subscriber, ce) {
var startHandle = node.on(EVENT[START],
this._onStart,
this,
node,
subscriber,
ce);
subscriber[_FLICK_START_HANDLE] = startHandle;
},
detach: function (node, subscriber, ce) {
var startHandle = subscriber[_FLICK_START_HANDLE],
endHandle = subscriber[_FLICK_END_HANDLE];
if (startHandle) {
startHandle.detach();
subscriber[_FLICK_START_HANDLE] = null;
}
if (endHandle) {
endHandle.detach();
subscriber[_FLICK_END_HANDLE] = null;
}
},
processArgs: function(args) {
var params = (args[3] !== undefined) ? Y.merge(args.splice(3, 1)[0]) : {};
if (!(MIN_VELOCITY in params)) {
params.minVelocity = this.MIN_VELOCITY;
}
if (!(MIN_DISTANCE in params)) {
params.minDistance = this.MIN_DISTANCE;
}
Y.log("flick, processArgs : minDistance =" + params.minDistance + ", minVelocity =" + params.minVelocity);
return params;
},
_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();
e.flick = {
time : new Date().getTime()
};
subscriber[_FLICK_START] = e;
endHandle = subscriber[_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);
subscriber[_FLICK_END_HANDLE] = endHandle;
}
}
},
_onEnd: function(e, node, subscriber, ce) {
var endTime = new Date().getTime(),
start = subscriber[_FLICK_START],
valid = !!start,
endEvent = e,
startTime,
time,
params,
xyDistance,
distance,
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.flick.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];
velocity = (time !== 0) ? distance/time : 0;
if (isFinite(velocity) && (Math.abs(distance) >= params.minDistance) && (Math.abs(velocity) >= params.minVelocity)) {
e.type = "flick";
e.flick = {
time:time,
distance: distance,
velocity:velocity,
axis: axis,
start : start
};
ce.fire(e);
}
subscriber[_FLICK_START] = null;
}
}
},
MIN_VELOCITY : 0,
MIN_DISTANCE : 0
});
}, '@VERSION@' ,{requires:['node-base','event-touch','event-synthetic']});
YUI.add('event-move', function(Y) {
/**
* Adds lower level support for "gesturemovestart", "gesturemove" and "gesturemoveend" events, which can be used to create drag/drop
* interactions which work across touch and mouse input devices. They correspond to "touchstart", "touchmove" and "touchend" on touch input
* device, and "mousedown", "mousemove", "mouseup" on mouse based input devices.
*
* @module event-gestures
* @submodule event-move
*/
var EVENT = ("ontouchstart" in Y.config.win && !Y.UA.chrome) ? {
start: "touchstart",
move: "touchmove",
end: "touchend"
} : {
start: "mousedown",
move: "mousemove",
end: "mouseup"
},
START = "start",
MOVE = "move",
END = "end",
_MOVE_START_HANDLE = "_msh",
_MOVE_HANDLE = "_mh",
_MOVE_END_HANDLE = "_meh",
_DEL_MOVE_START_HANDLE = "_dmsh",
_DEL_MOVE_HANDLE = "_dmh",
_DEL_MOVE_END_HANDLE = "_dmeh",
_MOVE_START = "_ms",
_MOVE = "_m",
MIN_TIME = "minTime",
MIN_DISTANCE = "minDistance",
OWNER_DOCUMENT = "ownerDocument",
NODE_TYPE = "nodeType",
_defArgsProcessor = function(args, delegate) {
var iExtra = (delegate) ? 4 : 3;
return (args[iExtra] !== undefined) ? Y.merge(args.splice(iExtra,1)[0]) : {};
},
_getRoot = function(node, subscriber) {
return subscriber._extra.root || (node.get(NODE_TYPE) === 9) ? node : node.get(OWNER_DOCUMENT);
},
define = Y.Event.define;
/**
* Sets up a "gesturemovestart" event, that is fired on touch devices in response to a single finger "touchstart",
* and on mouse based devices in response to a "mousedown". The subscriber can specify the minimum time
* and distance thresholds which should be crossed before the "gesturemovestart" is fired and for the mouse,
* which button should initiate a "gesturemovestart". This event can also be listened for using node.delegate().
*
* @event gesturemovestart
* @for YUI
* @param type {string} "gesturemovestart"
* @param fn {function} The method the event invokes. It receives the event facade of the underlying DOM event (mousedown or touchstart.touches[0]) which contains position co-ordinates.
* @param cfg {Object} Optional. An object which specifies:
* <dl>
* <dt>minDistance (defaults to 0)</dt>
* <dd>The minimum distance theshold which should be crossed before the gesturemovestart is fired</dd>
* <dt>minTime (defaults to 0)</dt>
* <dd>The minimum time theshold for which the finger/mouse should be help down before the gesturemovestart is fired</dd>
* <dt>button (no default)</dt>
* <dd>In the case of a mouse input device, if the event should only be fired for a specific mouse button.</dd>
* </dl>
*
* @return {EventHandle} the detach handle
*/
define('gesturemovestart', {
on: function (node, subscriber, ce) {
subscriber[_MOVE_START_HANDLE] = node.on(EVENT[START],
this._onStart,
this,
node,
subscriber,
ce);
},
delegate : function(node, subscriber, ce, filter) {
var se = this;
subscriber[_DEL_MOVE_START_HANDLE] = node.delegate(EVENT[START],
function(e) {
se._onStart(e, node, subscriber, ce, true);
},
filter);
},
detachDelegate : function(node, subscriber, ce, filter) {
var handle = subscriber[_DEL_MOVE_START_HANDLE];
if (handle) {
handle.detach();
subscriber[_DEL_MOVE_START_HANDLE] = null;
}
},
detach: function (node, subscriber, ce) {
var startHandle = subscriber[_MOVE_START_HANDLE];
if (startHandle) {
startHandle.detach();
subscriber[_MOVE_START_HANDLE] = null;
}
},
processArgs : function(args, delegate) {
var params = _defArgsProcessor(args, delegate);
if (!(MIN_TIME in params)) {
params[MIN_TIME] = this.MIN_TIME;
}
if (!(MIN_DISTANCE in params)) {
params[MIN_DISTANCE] = this.MIN_DISTANCE;
}
return params;
},
_onStart : function(e, node, subscriber, ce, delegate) {
e.preventDefault();
if (delegate) {
node = e.currentTarget;
}
var origE = e,
params = subscriber._extra,
start = true,
minTime = params.minTime,
minDistance = params.minDistance,
button = params.button,
root = _getRoot(node, subscriber),
startXY;
if (e.touches) {
start = (e.touches.length === 1);
e = e.touches[0];
e.target = e.target || origE.target;
e.currentTarget = e.currentTarget || origE.currentTarget;
} else {
start = (button === undefined) || (button = e.button);
}
Y.log("gesturemovestart: params = button:" + button + ", minTime = " + minTime + ", minDistance = " + minDistance, "event-gestures");
if (start) {
if (minTime === 0 || minDistance === 0) {
Y.log("gesturemovestart: No minTime or minDistance.", "event-gestures");
this._start(e, node, ce, params);
} else {
startXY = [e.pageX, e.pageY];
if (minTime > 0) {
Y.log("gesturemovestart: minTime specified. Setup timer.", "event-gestures");
Y.log("gesturemovestart: initialTime for minTime = " + new Date().getTime(), "event-gestures");
params._ht = Y.later(minTime, this, this._start, [e, node, ce, params]);
params._hme = root.on(EVENT[END], Y.bind(function() {
this._cancel(params);
}, this));
}
if (minDistance > 0) {
Y.log("gesturemovestart: minDistance specified. Setup native mouse/touchmove listener to measure distance.", "event-gestures");
Y.log("gesturemovestart: initialXY for minDistance = " + startXY, "event-gestures");
params._hm = root.on(EVENT[MOVE], Y.bind(function(em) {
if (Math.abs(em.pageX - startXY[0]) > minDistance || Math.abs(em.pageY - startXY[1]) > minDistance) {
Y.log("gesturemovestart: minDistance hit.", "event-gestures");
this._start(e, node, ce, params);
}
}, this));
}
}
}
},
_cancel : function(params) {
if (params._ht) {
params._ht.cancel();
params._ht = null;
}
if (params._hme) {
params._hme.detach();
params._hme = null;
}
if (params._hm) {
params._hm.detach();
params._hm = null;
}
},
_start : function(e, node, ce, params) {
if (params) {
this._cancel(params);
}
e.type = "gesturemovestart";
Y.log("gesturemovestart: Firing start: " + new Date().getTime(), "event-gestures");
node.setData(_MOVE_START, e);
ce.fire(e);
},
MIN_TIME : 0,
MIN_DISTANCE : 0
});
/**
* Sets up a "gesturemove" event, that is fired on touch devices in response to a single finger "touchmove",
* and on mouse based devices in response to a "mousemove".
*
* <p>By default this event is only fired when the same node
* has received a "gesturemovestart" event. The subscriber can set standAlone to true, in the configuration properties,
* if they want to listen for this event without an initial "gesturemovestart".</p>
*
* <p>By default this event sets up it's internal "touchmove" and "mousemove" DOM listeners on the document element. The subscriber
* can set the root configuration property, to specify which node to attach DOM listeners to, if different from the document.</p>
*
* <p>This event can also be listened for using node.delegate().</p>
*
* @event gesturemove
* @for YUI
* @param type {string} "gesturemove"
* @param fn {function} The method the event invokes. It receives the event facade of the underlying DOM event (mousemove or touchmove.touches[0]) which contains position co-ordinates.
* @param cfg {Object} Optional. An object which specifies:
* <dl>
* <dt>standAlone (defaults to false)</dt>
* <dd>true, if the subscriber should be notified even if a "gesturemovestart" has not occured on the same node.</dd>
* <dt>root (defaults to document)</dt>
* <dd>The node to which the internal DOM listeners should be attached.</dd>
* </dl>
*
* @return {EventHandle} the detach handle
*/
define('gesturemove', {
on : function (node, subscriber, ce) {
var root = _getRoot(node, subscriber),
moveHandle = root.on(EVENT[MOVE],
this._onMove,
this,
node,
subscriber,
ce);
subscriber[_MOVE_HANDLE] = moveHandle;
},
delegate : function(node, subscriber, ce, filter) {
var se = this;
subscriber[_DEL_MOVE_HANDLE] = node.delegate(EVENT[MOVE],
function(e) {
se._onMove(e, node, subscriber, ce, true);
},
filter);
},
detach : function (node, subscriber, ce) {
var moveHandle = subscriber[_MOVE_HANDLE];
if (moveHandle) {
moveHandle.detach();
subscriber[_MOVE_HANDLE] = null;
}
},
detachDelegate : function(node, subscriber, ce, filter) {
var handle = subscriber[_DEL_MOVE_HANDLE];
if (handle) {
handle.detach();
subscriber[_DEL_MOVE_HANDLE] = null;
}
},
processArgs : _defArgsProcessor,
_onMove : function(e, node, subscriber, ce, delegate) {
if (delegate) {
node = e.currentTarget;
}
var move = subscriber._extra.standAlone || node.getData(_MOVE_START),
origE = e;
Y.log("onMove:" + move,"event-gestures");
if (move) {
if (e.touches) {
move = (e.touches.length === 1);
e = e.touches[0];
e.target = e.target || origE.target;
e.currentTarget = e.currentTarget || origE.currentTarget;
}
if (move) {
Y.log("onMove2:" + move,"event-gestures");
origE.preventDefault();
e.type = "gesturemove";
ce.fire(e);
}
}
}
});
/**
* Sets up a "gesturemoveend" event, that is fired on touch devices in response to a single finger "touchend",
* and on mouse based devices in response to a "mouseup".
*
* <p>By default this event is only fired when the same node
* has received a "gesturemove" or "gesturemovestart" event. The subscriber can set standAlone to true, in the configuration properties,
* if they want to listen for this event without a preceding "gesturemovestart" or "gesturemove".</p>
*
* <p>By default this event sets up it's internal "touchend" and "mouseup" DOM listeners on the document element. The subscriber
* can set the root configuration property, to specify which node to attach DOM listeners to, if different from the document.</p>
*
* <p>This event can also be listened for using node.delegate().</p>
*
* @event gesturemoveend
* @for YUI
* @param type {string} "gesturemoveend"
* @param fn {function} The method the event invokes. It receives the event facade of the underlying DOM event (mouseup or touchend.changedTouches[0]).
* @param cfg {Object} Optional. An object which specifies:
* <dl>
* <dt>standAlone (defaults to false)</dt>
* <dd>true, if the subscriber should be notified even if a "gesturemovestart" or "gesturemove" has not occured on the same node.</dd>
* <dt>root (defaults to document)</dt>
* <dd>The node to which the internal DOM listeners should be attached.</dd>
* </dl>
*
* @return {EventHandle} the detach handle
*/
define('gesturemoveend', {
on : function (node, subscriber, ce) {
var root = _getRoot(node, subscriber),
endHandle = root.on(EVENT[END],
this._onEnd,
this,
node,
subscriber,
ce);
subscriber[_MOVE_END_HANDLE] = endHandle;
},
delegate : function(node, subscriber, ce, filter) {
var se = this;
subscriber[_DEL_MOVE_END_HANDLE] = node.delegate(EVENT[END],
function(e) {
se._onEnd(e, node, subscriber, ce, true);
},
filter);
},
detachDelegate : function(node, subscriber, ce, filter) {
var handle = subscriber[_DEL_MOVE_END_HANDLE];
if (handle) {
handle.detach();
subscriber[_DEL_MOVE_END_HANDLE] = null;
}
},
detach : function (node, subscriber, ce) {
var endHandle = subscriber[_MOVE_END_HANDLE];
if (endHandle) {
endHandle.detach();
subscriber[_MOVE_END_HANDLE] = null;
}
},
processArgs : _defArgsProcessor,
_onEnd : function(e, node, subscriber, ce, delegate) {
if (delegate) {
node = e.currentTarget;
}
var moveEnd = subscriber._extra.standAlone || node.getData(_MOVE) || node.getData(_MOVE_START),
origE = e;
if (moveEnd) {
if (e.changedTouches) {
if (e.changedTouches.length === 1) {
e = e.changedTouches[0];
e.target = e.target || origE.target;
e.currentTarget = e.currentTarget || origE.currentTarget;
} else {
moveEnd = false;
}
}
if (moveEnd) {
origE.preventDefault();
e.type = "gesturemoveend";
ce.fire(e);
node.clearData(_MOVE_START);
node.clearData(_MOVE);
}
}
}
});
}, '@VERSION@' ,{requires:['node-base','event-touch','event-synthetic']});
YUI.add('event-gestures', function(Y){}, '@VERSION@' ,{use:['event-flick', 'event-move']});