Skip to content

Commit

Permalink
Do not listen to mouse events on touch device
Browse files Browse the repository at this point in the history
This prevents issue #2 from happening on smartphone but does not fix it.
Indeed, we do not want to prevent mouse events on devices that support
both touch and mouses (touch screen on laptop for example).
  • Loading branch information
mpizenberg committed Nov 16, 2017
1 parent b94fd6d commit b983050
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions elm-pep.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@

if (! ("PointerEvent" in window) ) {

addMouseToPointerListener( document, 'mousedown', 'pointerdown' )
addMouseToPointerListener( document, 'mousemove', 'pointermove' )
addMouseToPointerListener( document, 'mouseup', 'pointerup' )
// Create Pointer polyfill from mouse events only on non-touch device
if (! ("TouchEvent" in window) ) {
addMouseToPointerListener( document, 'mousedown', 'pointerdown' )
addMouseToPointerListener( document, 'mousemove', 'pointermove' )
addMouseToPointerListener( document, 'mouseup', 'pointerup' )

function addMouseToPointerListener( target, mouseType, pointerType ) {
target.addEventListener( mouseType, ( mouseEvent ) => {
const elmPepTarget = findElmPEP( mouseEvent.target )
if ( elmPepTarget !== null ) {
let pointerEvent = new MouseEvent( pointerType, mouseEvent )
pointerEvent.pointerId = 1
pointerEvent.isPrimary = true
elmPepTarget.dispatchEvent( pointerEvent )
}
})
function addMouseToPointerListener( target, mouseType, pointerType ) {
target.addEventListener( mouseType, ( mouseEvent ) => {
const elmPepTarget = findElmPEP( mouseEvent.target )
if ( elmPepTarget !== null ) {
let pointerEvent = new MouseEvent( pointerType, mouseEvent )
pointerEvent.pointerId = 1
pointerEvent.isPrimary = true
elmPepTarget.dispatchEvent( pointerEvent )
}
})
}
}

addTouchToPointerListener( document, 'touchstart', 'pointerdown' )
Expand Down

0 comments on commit b983050

Please sign in to comment.