touch/mouse events are both handled simultaneously, module rewritten for better performances (see issue #18)

This commit is contained in:
bordun 2016-05-13 02:05:19 +02:00
parent 598cbd1099
commit 892eaae7ff
14 changed files with 610 additions and 496 deletions

View file

@ -1,5 +1,13 @@
# Changelog
## 0.5.0
- bug fixes
- touch/mouse events are both handled simultaneously, module rewritten for better performances (see issue #18)
- engine
- electron 1.0.0
## 0.4.8
- bug fixes

View file

@ -179,11 +179,11 @@ module.exports = {
traversingDisable: function(){
$('.traversingEnable, .traversingDisable').toggleClass('on')
TRAVERSING = false
$(document).trigger('disableTraversingGestures')
},
traversingEnable: function(){
$('.traversingEnable, .traversingDisable').toggleClass('on')
TRAVERSING = true
$(document).enableTraversingGestures()
}
}

View file

@ -14,7 +14,7 @@ var correctPosition = function (m, parent){
}
}
var ev = 'touchstart mousedown'
var ev = 'fake-click'
var menu = function(e,actions,parent){
@ -37,9 +37,6 @@ var menu = function(e,actions,parent){
$(`<div class="item">${label}</div>`).on(ev + '.editor',function(){
var callback = actions[label]
return function(e){
// ignore mouse event when fired by a simulated touch event
if (e.originalEvent.sourceCapabilities.firesTouchEvents) return
callback()
$('.context-menu').remove()
}

View file

@ -1,7 +1,7 @@
var updateDom = require('./data-workers').updateDom,
widgetOptions = require('../widgets').widgetOptions
var ev = 'touchstart mousedown'
var ev = 'fake-click'
var editObject = function(container, data, refresh){

View file

@ -14,9 +14,7 @@ var data = require('./data-workers'),
var init = function(){
var ev = 'touchstart mousedown'
$('body').off('.editor').on(ev+'.editor fake-right-click',function(e,d){
$('body').off('.editor').on('fake-click fake-right-click',function(e,d){
if (!EDITING) return

View file

@ -180,8 +180,9 @@ module.exports.create = function(widgetData,container) {
})
var off = 0
wrapper.on('draginit',function(e,data){
if (snap || TRAVERSING) {
wrapper.on('draginit',function(e,data,traversing){
if (snap || traversing) {
var d = (dimension=='height')?
((fader.size-data.offsetY+(wrapper.size-fader.size)/2) * 100 / fader.size):
(data.offsetX - (wrapper.size-fader.size)/2) * 100 / fader.size
@ -208,11 +209,7 @@ module.exports.create = function(widgetData,container) {
})
wrapper.on('drag',function(e,data,originalEvent){
if (originalEvent) originalEvent.preventDefault()
if (TRAVERSING) return
wrapper.on('drag',function(e,data){
var d = (dimension=='height')?-data.deltaY:data.deltaX
d = clip(d*100/fader.size+off,[0,100])

View file

@ -140,9 +140,10 @@ module.exports.create = function(widgetData,container) {
var offR = 0,
offX = 0,
offY = 0
wrapper.on('draginit',function(e,data){
wrapper.on('draginit',function(e,data,traversing){
if (snap || traversing) {
if (snap || TRAVERSING) {
var w = wrapper.size.width,
h = wrapper.size.height,
x = data.offsetX-w/2,
@ -175,11 +176,7 @@ module.exports.create = function(widgetData,container) {
offR = knob.rotation
})
wrapper.on('drag',function(e,data,originalEvent){
if (originalEvent) originalEvent.preventDefault()
if (TRAVERSING) return
wrapper.on('drag',function(e,data){
var r = clip(-data.deltaY*2+offR,[0,270])

View file

@ -79,7 +79,7 @@ module.exports.create = function(widgetData,container) {
return widget.value
}
widget.handleDragging(true)
widget.enableTraversingGestures()
return widget
}

View file

@ -70,7 +70,7 @@ module.exports.create = function(widgetData,container) {
return widget.value
}
widget.handleDragging(true)
widget.enableTraversingGestures()
return widget
}

View file

@ -70,7 +70,7 @@ module.exports.create = function(widgetData,container) {
return widget.value
}
widget.handleDragging(true)
widget.enableTraversingGestures()
return widget
}

View file

@ -80,8 +80,9 @@ module.exports.create = function(widgetData,container) {
var rgbOff = {x:0,y:0}
pad.on('draginit',function(e,data){
if (snap || TRAVERSING) {
pad.on('draginit',function(e,data,traversing){
if (snap || traversing) {
var h = ((pad.height-data.offsetY) * 100 / pad.height),
w = (data.offsetX * 100 / pad.width)
@ -105,11 +106,7 @@ module.exports.create = function(widgetData,container) {
rgbOff = {x:rgbHandle.width,y:rgbHandle.height}
})
pad.on('drag',function(e,data,originalEvent){
if (originalEvent) originalEvent.preventDefault()
if (TRAVERSING) return
pad.on('drag',function(e,data){
var h = clip((-data.deltaY)*100/pad.height+rgbOff.y,[0,100]),
w = clip(data.deltaX*100/pad.width+rgbOff.x,[0,100])
@ -133,8 +130,9 @@ module.exports.create = function(widgetData,container) {
var hueOff = 0
huePad.on('draginit',function(e,data){
if (snap || data.ctrlKey || data.shiftKey) {
huePad.on('draginit',function(e,data,traversing){
if (snap || traversing) {
var d = (data.offsetX * 100 / pad.width)
hueHandle[0].setAttribute('style',`transform:translate3d(${pad.width*d/100}px,0,0)`)
hueHandle.width = d
@ -163,11 +161,7 @@ module.exports.create = function(widgetData,container) {
})
huePad.on('drag',function(e,data,originalEvent){
if (originalEvent) originalEvent.preventDefault()
if (TRAVERSING) return
huePad.on('drag',function(e,data){
var w = clip(data.deltaX*100/pad.width+hueOff,[0,100])
hueHandle[0].setAttribute('style',`transform:translate3d(${pad.width*w/100}px,0,0)`)

View file

@ -79,8 +79,9 @@ module.exports.create = function(widgetData,container) {
})
var off = {x:0,y:0}
pad.on('draginit',function(e,data){
if (snap || TRAVERSING) {
pad.on('draginit',function(e,data,traversing){
if (snap || traversing) {
var h = ((pad.height-data.offsetY) * 100 / pad.height),
w = (data.offsetX * 100 / pad.width)
@ -105,11 +106,7 @@ module.exports.create = function(widgetData,container) {
off = {x:handle.width,y:handle.height}
})
pad.on('drag',function(e,data,originalEvent){
if (originalEvent) originalEvent.preventDefault()
if (TRAVERSING) return
pad.on('drag',function(e,data){
var h = clip((-data.deltaY)*100/pad.height+off.y,[0,100]),
w = clip((data.deltaX)*100/pad.width+off.x,[0,100])

View file

@ -236,11 +236,11 @@ module.exports = {
traversingDisable: function(){
$('.traversingEnable, .traversingDisable').toggleClass('on')
TRAVERSING = false
$(document).trigger('disableTraversingGestures')
},
traversingEnable: function(){
$('.traversingEnable, .traversingDisable').toggleClass('on')
TRAVERSING = true
$(document).enableTraversingGestures()
}
}
@ -366,7 +366,7 @@ var correctPosition = function (m, parent){
}
}
var ev = 'touchstart mousedown'
var ev = 'fake-click'
var menu = function(e,actions,parent){
@ -389,9 +389,6 @@ var menu = function(e,actions,parent){
$(`<div class="item">${label}</div>`).on(ev + '.editor',function(){
var callback = actions[label]
return function(e){
// ignore mouse event when fired by a simulated touch event
if (e.originalEvent.sourceCapabilities.firesTouchEvents) return
callback()
$('.context-menu').remove()
}
@ -588,7 +585,7 @@ module.exports = {
var updateDom = require('./data-workers').updateDom,
widgetOptions = require('../widgets').widgetOptions
var ev = 'touchstart mousedown'
var ev = 'fake-click'
var editObject = function(container, data, refresh){
@ -913,9 +910,7 @@ var data = require('./data-workers'),
var init = function(){
var ev = 'touchstart mousedown'
$('body').off('.editor').on(ev+'.editor fake-right-click',function(e,d){
$('body').off('.editor').on('fake-click fake-right-click',function(e,d){
if (!EDITING) return
@ -1889,8 +1884,9 @@ module.exports.create = function(widgetData,container) {
})
var off = 0
wrapper.on('draginit',function(e,data){
if (snap || TRAVERSING) {
wrapper.on('draginit',function(e,data,traversing){
if (snap || traversing) {
var d = (dimension=='height')?
((fader.size-data.offsetY+(wrapper.size-fader.size)/2) * 100 / fader.size):
(data.offsetX - (wrapper.size-fader.size)/2) * 100 / fader.size
@ -1917,11 +1913,7 @@ module.exports.create = function(widgetData,container) {
})
wrapper.on('drag',function(e,data,originalEvent){
if (originalEvent) originalEvent.preventDefault()
if (TRAVERSING) return
wrapper.on('drag',function(e,data){
var d = (dimension=='height')?-data.deltaY:data.deltaX
d = clip(d*100/fader.size+off,[0,100])
@ -2152,9 +2144,10 @@ module.exports.create = function(widgetData,container) {
var offR = 0,
offX = 0,
offY = 0
wrapper.on('draginit',function(e,data){
wrapper.on('draginit',function(e,data,traversing){
if (snap || traversing) {
if (snap || TRAVERSING) {
var w = wrapper.size.width,
h = wrapper.size.height,
x = data.offsetX-w/2,
@ -2187,11 +2180,7 @@ module.exports.create = function(widgetData,container) {
offR = knob.rotation
})
wrapper.on('drag',function(e,data,originalEvent){
if (originalEvent) originalEvent.preventDefault()
if (TRAVERSING) return
wrapper.on('drag',function(e,data){
var r = clip(-data.deltaY*2+offR,[0,270])
@ -2500,7 +2489,7 @@ module.exports.create = function(widgetData,container) {
return widget.value
}
widget.handleDragging(true)
widget.enableTraversingGestures()
return widget
}
@ -2578,7 +2567,7 @@ module.exports.create = function(widgetData,container) {
return widget.value
}
widget.handleDragging(true)
widget.enableTraversingGestures()
return widget
}
@ -2656,7 +2645,7 @@ module.exports.create = function(widgetData,container) {
return widget.value
}
widget.handleDragging(true)
widget.enableTraversingGestures()
return widget
}
@ -3115,8 +3104,9 @@ module.exports.create = function(widgetData,container) {
var rgbOff = {x:0,y:0}
pad.on('draginit',function(e,data){
if (snap || TRAVERSING) {
pad.on('draginit',function(e,data,traversing){
if (snap || traversing) {
var h = ((pad.height-data.offsetY) * 100 / pad.height),
w = (data.offsetX * 100 / pad.width)
@ -3140,11 +3130,7 @@ module.exports.create = function(widgetData,container) {
rgbOff = {x:rgbHandle.width,y:rgbHandle.height}
})
pad.on('drag',function(e,data,originalEvent){
if (originalEvent) originalEvent.preventDefault()
if (TRAVERSING) return
pad.on('drag',function(e,data){
var h = clip((-data.deltaY)*100/pad.height+rgbOff.y,[0,100]),
w = clip(data.deltaX*100/pad.width+rgbOff.x,[0,100])
@ -3168,8 +3154,9 @@ module.exports.create = function(widgetData,container) {
var hueOff = 0
huePad.on('draginit',function(e,data){
if (snap || data.ctrlKey || data.shiftKey) {
huePad.on('draginit',function(e,data,traversing){
if (snap || traversing) {
var d = (data.offsetX * 100 / pad.width)
hueHandle[0].setAttribute('style',`transform:translate3d(${pad.width*d/100}px,0,0)`)
hueHandle.width = d
@ -3198,11 +3185,7 @@ module.exports.create = function(widgetData,container) {
})
huePad.on('drag',function(e,data,originalEvent){
if (originalEvent) originalEvent.preventDefault()
if (TRAVERSING) return
huePad.on('drag',function(e,data){
var w = clip(data.deltaX*100/pad.width+hueOff,[0,100])
hueHandle[0].setAttribute('style',`transform:translate3d(${pad.width*w/100}px,0,0)`)
@ -3932,8 +3915,9 @@ module.exports.create = function(widgetData,container) {
})
var off = {x:0,y:0}
pad.on('draginit',function(e,data){
if (snap || TRAVERSING) {
pad.on('draginit',function(e,data,traversing){
if (snap || traversing) {
var h = ((pad.height-data.offsetY) * 100 / pad.height),
w = (data.offsetX * 100 / pad.width)
@ -3958,11 +3942,7 @@ module.exports.create = function(widgetData,container) {
off = {x:handle.width,y:handle.height}
})
pad.on('drag',function(e,data,originalEvent){
if (originalEvent) originalEvent.preventDefault()
if (TRAVERSING) return
pad.on('drag',function(e,data){
var h = clip((-data.deltaY)*100/pad.height+off.y,[0,100]),
w = clip((data.deltaX)*100/pad.width+off.x,[0,100])
@ -4106,241 +4086,314 @@ require('./app')
return {left: offsetLeft, top: offsetTop}
}
$.fn.handleDragging = function(traversing) {
// mouse single touch handler
var previousEvent = null,
target = null,
isPointerDown = false
$.event.special.draginit = $.event.special.drag = {
setup:function(){
this.on(events.mouse.start,function(e){
e.stopPropagation()
var $this = $(this)
// ignore mouse event when fired by a simulated touch event
if (e.originalEvent.sourceCapabilities.firesTouchEvents) return
if (e.button==2) {
e.preventDefault()
$(e.target).trigger('fake-right-click',e)
return
}
isPointerDown = true
target = $(e.target)
previousEvent = e
e.speedX = 0
e.speedY = 0
e.deltaX = 0
e.deltaY = 0
target.triggerHandler('draginit',e)
})
this.on(events.mouse.move,function(e){
e.stopPropagation()
if (!isPointerDown) return
e.speedX = e.pageX - previousEvent.pageX
e.speedY = e.pageY - previousEvent.pageY
e.deltaX = e.speedX + previousEvent.deltaX
e.deltaY = e.speedY + previousEvent.deltaY
if ($this.data('dragSetup')) return
$this.data('dragSetup','true')
if (traversing || TRAVERSING) {
target = $(e.target)
e.preventDefault()
target.triggerHandler('draginit',e)
} else {
target.triggerHandler('drag',e)
}
previousEvent = e
})
$document.on(events.mouse.stop,function(e){
e.stopPropagation()
// ignore mouse event when fired by a simulated touch event
if (e.originalEvent.sourceCapabilities.firesTouchEvents) return
if (!isPointerDown) return
e.speedX = e.pageX - previousEvent.pageX
e.speedY = e.pageY - previousEvent.pageY
e.deltaX = e.deltaX + previousEvent.deltaX
e.deltaY = e.deltaY + previousEvent.deltaY
target.trigger('dragend',e)
isPointerDown = false
})
// mouse single touch handler
var previousEvent = null,
target = null,
isPointerDown = false,
traversing = false
this.on('mouseout',function(e){
if (traversing || TRAVERSING) {
e.stopPropagation()
if (!isPointerDown) return
$this.on(events.mouse.start,function(e){
// e.stopPropagation()
e.speedX = e.pageX - previousEvent.pageX
e.speedY = e.pageY - previousEvent.pageY
e.deltaX = e.deltaX + previousEvent.deltaX
e.deltaY = e.deltaY + previousEvent.deltaY
target.trigger('dragend',e)
// ignore mouse event when fired by a simulated touch event
if (e.originalEvent.sourceCapabilities.firesTouchEvents) return
isPointerDown = true
target = $this
previousEvent = e
}
})
e.speedX = 0
e.speedY = 0
e.deltaX = 0
e.deltaY = 0
target.triggerHandler('draginit',[e,e.originalEvent.traversing])
$document.on(events.mouse.move,function(e){
e.stopPropagation()
if (!isPointerDown) return
e.speedX = e.pageX - previousEvent.pageX
e.speedY = e.pageY - previousEvent.pageY
e.deltaX = e.speedX + previousEvent.deltaX
e.deltaY = e.speedY + previousEvent.deltaY
// multi touch hanlder
var targets = {},
previousTouches = {},
touchTapTimer = false
this.on(events.touch.start,function(e){
// e.stopPropagation()
var oE = e.originalEvent
for (i in oE.changedTouches) {
if (isNaN(i)) continue
var touch = oE.changedTouches[i],
id = touch.identifier
targets[id] = $(touch.target)
previousTouches[id] = touch
touch.speedX = 0
touch.speedY = 0
touch.deltaX = 0
touch.deltaY = 0
var off = getOffset(touch.target)
touch.offsetX = touch.pageX-off.left
touch.offsetY = touch.pageY-off.top
targets[id].triggerHandler('draginit',[touch,e])
}
if (!touchTapTimer&&oE.touches.length==1) {
touchTapTimer = setTimeout(function(){
$(oE.changedTouches[0].target).trigger('fake-right-click',oE.changedTouches[0])
},600)
} else {
clearTimeout(touchTapTimer)
touchTapTimer = false
}
})
this.on(events.touch.move,function(e){
// e.stopPropagation()
var oE = e.originalEvent
// ,touched = []
if (touchTapTimer) {
clearTimeout(touchTapTimer)
touchTapTimer = false
}
for (i in oE.changedTouches) {
if (isNaN(i)) continue
var touch = oE.changedTouches[i],
id = touch.identifier
if (traversing || TRAVERSING) {
targets[id] = $(document.elementFromPoint(touch.clientX, touch.clientY))
}
// touched[targets[id]] = touched[targets[id]]?touched[targets[id]]+1:1
touch.speedX = touch.pageX - previousTouches[id].pageX
touch.speedY = touch.pageY - previousTouches[id].pageY
touch.deltaX = touch.speedX + previousTouches[id].deltaX
touch.deltaY = touch.speedY + previousTouches[id].deltaY
touch.offsetX = previousTouches[id].offsetX+touch.speedX
touch.offsetY = previousTouches[id].offsetY+touch.speedY
if (traversing || TRAVERSING) {
var previousTarget = document.elementFromPoint(previousTouches[id].clientX, previousTouches[id].clientY)
if (targets[id][0]!=previousTarget) {
var off = getOffset(targets[id][0])
touch.offsetX = touch.pageX-off.left
touch.offsetY = touch.pageY-off.top
$(previousTarget).trigger('dragend',[touch,e])
if (e.originalEvent.traversing || TRAVERSING) {
target = $(e.target)
e.preventDefault()
target.triggerHandler('draginit',[e,e.originalEvent.traversing])
} else {
target.triggerHandler('drag',[e,e.originalEvent.traversing])
}
e.preventDefault()
if (this.contains(targets[id][0])) targets[id].triggerHandler('draginit',[touch,e])
} else {
previousEvent = e
})
targets[id].triggerHandler('drag',[touch,e])
$document.on(events.mouse.stop,function(e){
e.stopPropagation()
// ignore mouse event when fired by a simulated touch event
if (e.originalEvent.sourceCapabilities.firesTouchEvents) return
if (!isPointerDown) return
e.speedX = e.pageX - previousEvent.pageX
e.speedY = e.pageY - previousEvent.pageY
e.deltaX = e.deltaX + previousEvent.deltaX
e.deltaY = e.deltaY + previousEvent.deltaY
target.trigger('dragend',[e,e.originalEvent.traversing])
isPointerDown = false
$document.off(events.mouse.move)
$document.off(events.mouse.stop)
$document.off('mouseout')
})
$document.on('mouseout',function(e){
if (e.originalEvent.traversing || TRAVERSING) {
e.stopPropagation()
if (!isPointerDown) return
e.speedX = e.pageX - previousEvent.pageX
e.speedY = e.pageY - previousEvent.pageY
e.deltaX = e.deltaX + previousEvent.deltaX
e.deltaY = e.deltaY + previousEvent.deltaY
target.trigger('dragend',[e,e.originalEvent.traversing])
previousEvent = e
}
})
})
// multi touch hanlder
var targets = {},
previousTouches = {}
$this.on(events.touch.start,function(e){
e.preventDefault()
var oE = e.originalEvent
for (i in oE.changedTouches) {
if (isNaN(i)) continue
var touch = oE.changedTouches[i],
id = touch.identifier
targets[id] = $(touch.target)
previousTouches[id] = touch
touch.speedX = 0
touch.speedY = 0
touch.deltaX = 0
touch.deltaY = 0
var off = getOffset(touch.target)
touch.offsetX = touch.pageX-off.left
touch.offsetY = touch.pageY-off.top
targets[id].triggerHandler('draginit',[touch,e.originalEvent.traversing])
}
previousTouches[id] = touch
$document.on(events.touch.move,function(e){
// e.stopPropagation()
var oE = e.originalEvent
for (i in oE.changedTouches) {
if (isNaN(i)) continue
var touch = oE.changedTouches[i],
id = touch.identifier
if (e.originalEvent.traversing || TRAVERSING) {
targets[id] = $(document.elementFromPoint(touch.clientX, touch.clientY))
}
// touched[targets[id]] = touched[targets[id]]?touched[targets[id]]+1:1
touch.speedX = touch.pageX - previousTouches[id].pageX
touch.speedY = touch.pageY - previousTouches[id].pageY
touch.deltaX = touch.speedX + previousTouches[id].deltaX
touch.deltaY = touch.speedY + previousTouches[id].deltaY
touch.offsetX = previousTouches[id].offsetX+touch.speedX
touch.offsetY = previousTouches[id].offsetY+touch.speedY
if (e.originalEvent.traversing || TRAVERSING) {
touch.traversing = true
var previousTarget = document.elementFromPoint(previousTouches[id].clientX, previousTouches[id].clientY)
if (targets[id][0]!=previousTarget) {
var off = getOffset(targets[id][0])
touch.offsetX = touch.pageX-off.left
touch.offsetY = touch.pageY-off.top
$(previousTarget).trigger('dragend',[touch,e.originalEvent.traversing])
}
e.preventDefault()
if (this.contains(targets[id][0])) targets[id].triggerHandler('draginit',[touch,e.originalEvent.traversing])
} else {
targets[id].triggerHandler('drag',[touch,e.originalEvent.traversing])
}
previousTouches[id] = touch
}
})
$document.on(events.touch.stop,function(e){
e.stopPropagation()
var oE = e.originalEvent
for (i in oE.changedTouches) {
if (isNaN(i)) continue
var touch = oE.changedTouches[i],
id = touch.identifier
$(oE.changedTouches[i].target).trigger('dragend',[touch,e.originalEvent.traversing])
delete previousTouches[id]
delete targets[id]
}
$document.off(events.touch.move)
$document.off(events.touch.stop)
})
})
}
}
var makeEventTraversing = function(e){
e.traversing=true
}
$.fn.enableTraversingGestures = function() {
var self = this[0]
var down = function(){
self.addEventListener("mousemove", makeEventTraversing, true)
self.addEventListener("touchmove", makeEventTraversing, true)
self.addEventListener("mouseout", makeEventTraversing, true)
document.addEventListener("mouseup",up,true)
document.addEventListener("touchend",up,true)
},
up = function(){
self.removeEventListener("mousemove", makeEventTraversing, true)
self.removeEventListener("touchmove", makeEventTraversing, true)
self.removeEventListener("mouseout", makeEventTraversing, true)
document.removeEventListener("mouseup",up,true)
document.removeEventListener("touchend",up,true)
}
self.addEventListener("mousedown", down, true)
self.addEventListener("touchstart", down, true)
this.on('disableTraversingGestures',function() {
self.removeEventListener("mousedown", down, true)
self.removeEventListener("touchstart", down, true)
})
$document.on(events.touch.stop,function(e){
e.stopPropagation()
var oE = e.originalEvent
for (i in oE.changedTouches) {
if (isNaN(i)) continue
var touch = oE.changedTouches[i],
id = touch.identifier
$(oE.changedTouches[i].target).trigger('dragend',[touch,e])
delete previousTouches[id]
delete targets[id]
}
if (touchTapTimer) {
clearTimeout(touchTapTimer)
touchTapTimer = false
}
})
return this
}
$document.handleDragging()
// prevent mouse events to be fired when emulating touch with chrome
$document.on('mousedown mouseup',function(e){
if (e.originalEvent.sourceCapabilities.firesTouchEvents) {
e.preventDefault()
e.stopPropagation()
$document.on('mousedown',function(e){
if (e.originalEvent.sourceCapabilities && e.originalEvent.sourceCapabilities.firesTouchEvents) return
$(e.target).trigger('fake-click')
if (e.button==2) {
$(e.target).trigger('fake-right-click',e)
}
})
var touchTapTimer = false,
clearTouchTapTimer = function() {
clearTimeout(touchTapTimer)
touchTapTimer = false
}
$document.on(events.touch.start,function(e){
$(e.target).trigger('fake-click')
if (!touchTapTimer&&e.originalEvent.touches.length==1) {
touchTapTimer = setTimeout(function(){
$(e.originalEvent.changedTouches[0].target).trigger('fake-right-click',e.originalEvent.changedTouches[0])
touchTapTimer = false
},600)
} else {
clearTouchTapTimer()
}
})
document.addEventListener(events.touch.move,clearTouchTapTimer)
document.addEventListener(events.touch.stop,clearTouchTapTimer)
$document.on('contextmenu',function(){return false})
})(jQuery)

View file

@ -26,241 +26,314 @@
return {left: offsetLeft, top: offsetTop}
}
$.fn.handleDragging = function(traversing) {
// mouse single touch handler
var previousEvent = null,
target = null,
isPointerDown = false
$.event.special.draginit = $.event.special.drag = {
setup:function(){
this.on(events.mouse.start,function(e){
e.stopPropagation()
var $this = $(this)
// ignore mouse event when fired by a simulated touch event
if (e.originalEvent.sourceCapabilities.firesTouchEvents) return
if (e.button==2) {
e.preventDefault()
$(e.target).trigger('fake-right-click',e)
return
}
isPointerDown = true
target = $(e.target)
previousEvent = e
e.speedX = 0
e.speedY = 0
e.deltaX = 0
e.deltaY = 0
target.triggerHandler('draginit',e)
})
this.on(events.mouse.move,function(e){
e.stopPropagation()
if (!isPointerDown) return
e.speedX = e.pageX - previousEvent.pageX
e.speedY = e.pageY - previousEvent.pageY
e.deltaX = e.speedX + previousEvent.deltaX
e.deltaY = e.speedY + previousEvent.deltaY
if ($this.data('dragSetup')) return
$this.data('dragSetup','true')
if (traversing || TRAVERSING) {
target = $(e.target)
e.preventDefault()
target.triggerHandler('draginit',e)
} else {
target.triggerHandler('drag',e)
}
previousEvent = e
})
$document.on(events.mouse.stop,function(e){
e.stopPropagation()
// ignore mouse event when fired by a simulated touch event
if (e.originalEvent.sourceCapabilities.firesTouchEvents) return
if (!isPointerDown) return
e.speedX = e.pageX - previousEvent.pageX
e.speedY = e.pageY - previousEvent.pageY
e.deltaX = e.deltaX + previousEvent.deltaX
e.deltaY = e.deltaY + previousEvent.deltaY
target.trigger('dragend',e)
isPointerDown = false
})
// mouse single touch handler
var previousEvent = null,
target = null,
isPointerDown = false,
traversing = false
this.on('mouseout',function(e){
if (traversing || TRAVERSING) {
e.stopPropagation()
if (!isPointerDown) return
$this.on(events.mouse.start,function(e){
// e.stopPropagation()
e.speedX = e.pageX - previousEvent.pageX
e.speedY = e.pageY - previousEvent.pageY
e.deltaX = e.deltaX + previousEvent.deltaX
e.deltaY = e.deltaY + previousEvent.deltaY
target.trigger('dragend',e)
// ignore mouse event when fired by a simulated touch event
if (e.originalEvent.sourceCapabilities.firesTouchEvents) return
isPointerDown = true
target = $this
previousEvent = e
}
})
e.speedX = 0
e.speedY = 0
e.deltaX = 0
e.deltaY = 0
target.triggerHandler('draginit',[e,e.originalEvent.traversing])
$document.on(events.mouse.move,function(e){
e.stopPropagation()
if (!isPointerDown) return
e.speedX = e.pageX - previousEvent.pageX
e.speedY = e.pageY - previousEvent.pageY
e.deltaX = e.speedX + previousEvent.deltaX
e.deltaY = e.speedY + previousEvent.deltaY
// multi touch hanlder
var targets = {},
previousTouches = {},
touchTapTimer = false
this.on(events.touch.start,function(e){
// e.stopPropagation()
var oE = e.originalEvent
for (i in oE.changedTouches) {
if (isNaN(i)) continue
var touch = oE.changedTouches[i],
id = touch.identifier
targets[id] = $(touch.target)
previousTouches[id] = touch
touch.speedX = 0
touch.speedY = 0
touch.deltaX = 0
touch.deltaY = 0
var off = getOffset(touch.target)
touch.offsetX = touch.pageX-off.left
touch.offsetY = touch.pageY-off.top
targets[id].triggerHandler('draginit',[touch,e])
}
if (!touchTapTimer&&oE.touches.length==1) {
touchTapTimer = setTimeout(function(){
$(oE.changedTouches[0].target).trigger('fake-right-click',oE.changedTouches[0])
},600)
} else {
clearTimeout(touchTapTimer)
touchTapTimer = false
}
})
this.on(events.touch.move,function(e){
// e.stopPropagation()
var oE = e.originalEvent
// ,touched = []
if (touchTapTimer) {
clearTimeout(touchTapTimer)
touchTapTimer = false
}
for (i in oE.changedTouches) {
if (isNaN(i)) continue
var touch = oE.changedTouches[i],
id = touch.identifier
if (traversing || TRAVERSING) {
targets[id] = $(document.elementFromPoint(touch.clientX, touch.clientY))
}
// touched[targets[id]] = touched[targets[id]]?touched[targets[id]]+1:1
touch.speedX = touch.pageX - previousTouches[id].pageX
touch.speedY = touch.pageY - previousTouches[id].pageY
touch.deltaX = touch.speedX + previousTouches[id].deltaX
touch.deltaY = touch.speedY + previousTouches[id].deltaY
touch.offsetX = previousTouches[id].offsetX+touch.speedX
touch.offsetY = previousTouches[id].offsetY+touch.speedY
if (traversing || TRAVERSING) {
var previousTarget = document.elementFromPoint(previousTouches[id].clientX, previousTouches[id].clientY)
if (targets[id][0]!=previousTarget) {
var off = getOffset(targets[id][0])
touch.offsetX = touch.pageX-off.left
touch.offsetY = touch.pageY-off.top
$(previousTarget).trigger('dragend',[touch,e])
if (e.originalEvent.traversing || TRAVERSING) {
target = $(e.target)
e.preventDefault()
target.triggerHandler('draginit',[e,e.originalEvent.traversing])
} else {
target.triggerHandler('drag',[e,e.originalEvent.traversing])
}
e.preventDefault()
if (this.contains(targets[id][0])) targets[id].triggerHandler('draginit',[touch,e])
} else {
previousEvent = e
})
targets[id].triggerHandler('drag',[touch,e])
$document.on(events.mouse.stop,function(e){
e.stopPropagation()
// ignore mouse event when fired by a simulated touch event
if (e.originalEvent.sourceCapabilities.firesTouchEvents) return
if (!isPointerDown) return
e.speedX = e.pageX - previousEvent.pageX
e.speedY = e.pageY - previousEvent.pageY
e.deltaX = e.deltaX + previousEvent.deltaX
e.deltaY = e.deltaY + previousEvent.deltaY
target.trigger('dragend',[e,e.originalEvent.traversing])
isPointerDown = false
$document.off(events.mouse.move)
$document.off(events.mouse.stop)
$document.off('mouseout')
})
$document.on('mouseout',function(e){
if (e.originalEvent.traversing || TRAVERSING) {
e.stopPropagation()
if (!isPointerDown) return
e.speedX = e.pageX - previousEvent.pageX
e.speedY = e.pageY - previousEvent.pageY
e.deltaX = e.deltaX + previousEvent.deltaX
e.deltaY = e.deltaY + previousEvent.deltaY
target.trigger('dragend',[e,e.originalEvent.traversing])
previousEvent = e
}
})
})
// multi touch hanlder
var targets = {},
previousTouches = {}
$this.on(events.touch.start,function(e){
e.preventDefault()
var oE = e.originalEvent
for (i in oE.changedTouches) {
if (isNaN(i)) continue
var touch = oE.changedTouches[i],
id = touch.identifier
targets[id] = $(touch.target)
previousTouches[id] = touch
touch.speedX = 0
touch.speedY = 0
touch.deltaX = 0
touch.deltaY = 0
var off = getOffset(touch.target)
touch.offsetX = touch.pageX-off.left
touch.offsetY = touch.pageY-off.top
targets[id].triggerHandler('draginit',[touch,e.originalEvent.traversing])
}
previousTouches[id] = touch
$document.on(events.touch.move,function(e){
// e.stopPropagation()
var oE = e.originalEvent
for (i in oE.changedTouches) {
if (isNaN(i)) continue
var touch = oE.changedTouches[i],
id = touch.identifier
if (e.originalEvent.traversing || TRAVERSING) {
targets[id] = $(document.elementFromPoint(touch.clientX, touch.clientY))
}
// touched[targets[id]] = touched[targets[id]]?touched[targets[id]]+1:1
touch.speedX = touch.pageX - previousTouches[id].pageX
touch.speedY = touch.pageY - previousTouches[id].pageY
touch.deltaX = touch.speedX + previousTouches[id].deltaX
touch.deltaY = touch.speedY + previousTouches[id].deltaY
touch.offsetX = previousTouches[id].offsetX+touch.speedX
touch.offsetY = previousTouches[id].offsetY+touch.speedY
if (e.originalEvent.traversing || TRAVERSING) {
touch.traversing = true
var previousTarget = document.elementFromPoint(previousTouches[id].clientX, previousTouches[id].clientY)
if (targets[id][0]!=previousTarget) {
var off = getOffset(targets[id][0])
touch.offsetX = touch.pageX-off.left
touch.offsetY = touch.pageY-off.top
$(previousTarget).trigger('dragend',[touch,e.originalEvent.traversing])
}
e.preventDefault()
if (this.contains(targets[id][0])) targets[id].triggerHandler('draginit',[touch,e.originalEvent.traversing])
} else {
targets[id].triggerHandler('drag',[touch,e.originalEvent.traversing])
}
previousTouches[id] = touch
}
})
$document.on(events.touch.stop,function(e){
e.stopPropagation()
var oE = e.originalEvent
for (i in oE.changedTouches) {
if (isNaN(i)) continue
var touch = oE.changedTouches[i],
id = touch.identifier
$(oE.changedTouches[i].target).trigger('dragend',[touch,e.originalEvent.traversing])
delete previousTouches[id]
delete targets[id]
}
$document.off(events.touch.move)
$document.off(events.touch.stop)
})
})
}
}
var makeEventTraversing = function(e){
e.traversing=true
}
$.fn.enableTraversingGestures = function() {
var self = this[0]
var down = function(){
self.addEventListener("mousemove", makeEventTraversing, true)
self.addEventListener("touchmove", makeEventTraversing, true)
self.addEventListener("mouseout", makeEventTraversing, true)
document.addEventListener("mouseup",up,true)
document.addEventListener("touchend",up,true)
},
up = function(){
self.removeEventListener("mousemove", makeEventTraversing, true)
self.removeEventListener("touchmove", makeEventTraversing, true)
self.removeEventListener("mouseout", makeEventTraversing, true)
document.removeEventListener("mouseup",up,true)
document.removeEventListener("touchend",up,true)
}
self.addEventListener("mousedown", down, true)
self.addEventListener("touchstart", down, true)
this.on('disableTraversingGestures',function() {
self.removeEventListener("mousedown", down, true)
self.removeEventListener("touchstart", down, true)
})
$document.on(events.touch.stop,function(e){
e.stopPropagation()
var oE = e.originalEvent
for (i in oE.changedTouches) {
if (isNaN(i)) continue
var touch = oE.changedTouches[i],
id = touch.identifier
$(oE.changedTouches[i].target).trigger('dragend',[touch,e])
delete previousTouches[id]
delete targets[id]
}
if (touchTapTimer) {
clearTimeout(touchTapTimer)
touchTapTimer = false
}
})
return this
}
$document.handleDragging()
// prevent mouse events to be fired when emulating touch with chrome
$document.on('mousedown mouseup',function(e){
if (e.originalEvent.sourceCapabilities.firesTouchEvents) {
e.preventDefault()
e.stopPropagation()
$document.on('mousedown',function(e){
if (e.originalEvent.sourceCapabilities && e.originalEvent.sourceCapabilities.firesTouchEvents) return
$(e.target).trigger('fake-click')
if (e.button==2) {
$(e.target).trigger('fake-right-click',e)
}
})
var touchTapTimer = false,
clearTouchTapTimer = function() {
clearTimeout(touchTapTimer)
touchTapTimer = false
}
$document.on(events.touch.start,function(e){
$(e.target).trigger('fake-click')
if (!touchTapTimer&&e.originalEvent.touches.length==1) {
touchTapTimer = setTimeout(function(){
$(e.originalEvent.changedTouches[0].target).trigger('fake-right-click',e.originalEvent.changedTouches[0])
touchTapTimer = false
},600)
} else {
clearTouchTapTimer()
}
})
document.addEventListener(events.touch.move,clearTouchTapTimer)
document.addEventListener(events.touch.stop,clearTouchTapTimer)
$document.on('contextmenu',function(){return false})
})(jQuery)