litejs
Version:
Single-page application framework
89 lines (81 loc) • 2.25 kB
JavaScript
!function(Event, window) {
var seq = 2
, MS = "MSPointer"
, DOWN = "pointerdown"
, MOVE = "pointermove"
, UP = "pointerup"
, CANCEL = "pointercancel"
, fixEv = Event.fixEv
, fixFn = Event.fixFn
, map = {
d: "touchstart",
m: "touchmove",
u: "touchend",
c: "touchcancel"
}
if (!window.PointerEvent) {
var patched = (window.xhr || window)._patched || []
// IE10
if (window[MS + "Event"]) {
fixEv[DOWN] = MS + "Down"
fixEv[MOVE] = MS + "Move"
fixEv[UP] = MS + "Up"
fixEv[CANCEL] = MS + "Cancel"
patched.push("pointer:MS")
return
}
patched.push("pointer")
fixEv[DOWN] = "mousedown"
fixEv[MOVE] = "mousemove"
fixEv[UP] = "mouseup"
fixEv[CANCEL] = "mouseup"
fixFn[DOWN] =
fixFn[MOVE] =
fixFn[UP] =
fixEv[CANCEL] = function(el, _fn, ev) {
var blockMouse
if (window.TouchEvent) {
// Calling preventDefault on a touchstart or the first touchmove event of a series
// prevents the corresponding mouse events from firing.
//
// chrome://flags/ Touch Events API
El.on(el, map[ev[7]], touchToPointer)
mouseToPointer._rm = El.off.bind(el, el, map[ev[7]], touchToPointer)
}
return mouseToPointer
function mouseToPointer(e) {
if (blockMouse) {
return
}
if (!e.target) e.target = el
e.pointerId = 1
e.pointerType = "mouse"
e.width = e.height = 1
e.pressure = e.type == "mouseup" ? 0 : 0.5
if (el.setCapture) {
if (e.type == "mousedown") el.setCapture(true)
if (e.type == "mouseup") document.releaseCapture()
}
_fn.call(el, e)
}
function touchToPointer(e) {
var touch
, touches = e.changedTouches
, preventDefault = e.preventDefault.bind(e)
, stopPropagation = e.stopPropagation.bind(e)
, i = 0
for (; touch = touches[i++]; ) {
touch.pointerId = touch.identifier + 2
touch.pointerType = "touch"
touch.width = 2 * (touch.radiusX || touch.webkitRadiusX || 0)
touch.height = 2 * (touch.radiusY || touch.webkitRadiusY || 0)
touch.pressure = touch.force || touch.webkitForce || 0.5
touch.preventDefault = preventDefault
touch.stopPropagation = stopPropagation
_fn.call(el, touch)
}
blockMouse = e.touches[0]
}
}
}
}(Event, this)