touch: prevent touchstart + mousedown from jquery touch punch; editor: ignore pointerdown interraction when selecting a widget (issue #157)

This commit is contained in:
jean-emmanuel 2018-02-18 15:46:39 +01:00
parent aeef06749d
commit ba6f1419c3
7 changed files with 205 additions and 26 deletions

View file

@ -24,9 +24,6 @@ class ContextMenu {
var item = DOM.create(`<div class="item has-sub" tabIndex="1">${label}</div>`)
menu.appendChild(item)
item.addEventListener(this.event, (e)=>{
e.stopPropagation()
})
this.open(e,actions[label],item)
@ -112,13 +109,13 @@ class ContextMenu {
}
var menu = new ContextMenu()
var contextMenu = new ContextMenu()
var handleClick = function(event) {
if (!EDITING) return
menu.close()
if (contextMenu.menu && !contextMenu.menu.contains(event.target)) contextMenu.close()
var eventData = event.detail,
widget = widgetManager.getWidgetByElement(eventData.target, ':not(.not-editable)')
@ -130,12 +127,20 @@ var handleClick = function(event) {
data = widget.props,
type = widget.props.type == 'tab' ? 'tab' : 'widget'
// if the widget is not already selected
if (!widget.container.classList.contains('editing')) {
// add a flag to the original event to prevent draginit
// and prevent any further fast-click (ie input focus)
eventData.preventInterraction = true
}
editor.select(widget)
if (event.type!='fast-right-click') return
if (container.classList.contains('root-container')) {
menu.open(eventData,{
contextMenu.open(eventData,{
'<i class="fa fa-plus"></i> Add tab': function(){
data.tabs.push({})
updateWidget(widget)
@ -258,9 +263,9 @@ var handleClick = function(event) {
}
menu.open(eventData, actions)
contextMenu.open(eventData, actions)
}
document.addEventListener('fast-right-click', handleClick)
document.addEventListener('fast-click', handleClick)
document.addEventListener('fast-right-click', handleClick, true)
document.addEventListener('fast-click', handleClick, true)

View file

@ -40,7 +40,6 @@ var Editor = class Editor {
`)
DOM.each(gridForm, '#grid-width-input', (input)=>{
console.log(input)
DOM.addEventListener(input, 'keyup mouseup change mousewheel', (e)=>{
setTimeout(()=>{
var v = Math.max(Math.min(parseInt(input.value), 100), 1)

View file

@ -13,20 +13,21 @@ document.body.setAttribute('oncontextmenu', 'return false')
document.addEventListener('mousedown', (event)=>{
if (event.touchPunch) return console.log('ignored')
if (event.sourceCapabilities && event.sourceCapabilities.firesTouchEvents) return
if (event.button == 2) event.preventDefault()
var e = fix(event)
var e = event
var name = event.button == 2 ? 'fast-right-click' : 'fast-click'
DOM.dispatchEvent(e.target, name, e)
})
}, true)
document.addEventListener('touchstart', (event)=>{
var e = fix(event.changedTouches[0])
var e = event.changedTouches[0]
DOM.dispatchEvent(e.target, 'fast-click', e)
@ -43,7 +44,7 @@ document.addEventListener('touchstart', (event)=>{
}, 600)
}, supportsPassive ? { passive: true } : false)
}, true)
DOM.addEventListener(document, 'touchend touchmove touchcancel', clearLongTouchTimer)

View file

@ -5,6 +5,8 @@ var targets = {},
function pointerDownHandler(event) {
if (event.preventInterraction === true) return
if (!event.multitouch) {
for (var i in targets) {
if (targets[i] == event.target) return

View file

@ -32,5 +32,6 @@ window.DOM = require('./dom')
window.$ = require('jquery/dist/jquery.slim.min.js')
require('../libs/jquery.ui')
require('../libs/jquery.touch')
require('cardinal-spline-js/curve.js')

View file

@ -0,0 +1,183 @@
/*!
* jQuery UI Touch Punch 0.2.3
*
* Copyright 2011–2014, Dave Furfero
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Depends:
* jquery.ui.widget.js
* jquery.ui.mouse.js
*/
(function ($) {
// Detect touch support
$.support.touch = 'ontouchend' in document;
// Ignore browsers without touch support
if (!$.support.touch) {
return;
}
var mouseProto = $.ui.mouse.prototype,
_mouseInit = mouseProto._mouseInit,
_mouseDestroy = mouseProto._mouseDestroy,
touchHandled;
/**
* Simulate a mouse event based on a corresponding touch event
* @param {Object} event A touch event
* @param {String} simulatedType The corresponding mouse event
*/
function simulateMouseEvent (event, simulatedType) {
// Ignore multi-touch events
if (event.originalEvent.touches.length > 1) {
return;
}
event.preventDefault();
var touch = event.originalEvent.changedTouches[0],
simulatedEvent = document.createEvent('MouseEvents');
// Initialize the simulated mouse event using the touch event's coordinates
simulatedEvent.initMouseEvent(
simulatedType, // type
true, // bubbles
true, // cancelable
window, // view
1, // detail
touch.screenX, // screenX
touch.screenY, // screenY
touch.clientX, // clientX
touch.clientY, // clientY
false, // ctrlKey
false, // altKey
false, // shiftKey
false, // metaKey
0, // button
null // relatedTarget
);
// osc: add a flag to prevent fast-click
simulatedEvent.touchPunch = true
// Dispatch the simulated event to the target element
event.target.dispatchEvent(simulatedEvent);
}
/**
* Handle the jQuery UI widget's touchstart events
* @param {Object} event The widget element's touchstart event
*/
mouseProto._touchStart = function (event) {
var self = this;
// Ignore the event if another widget is already being handled
if (touchHandled || !self._mouseCapture(event.originalEvent.changedTouches[0])) {
return;
}
// Set the flag to prevent other widgets from inheriting the touch event
touchHandled = true;
// Track movement to determine if interaction was a click
self._touchMoved = false;
// Simulate the mouseover event
simulateMouseEvent(event, 'mouseover');
// Simulate the mousemove event
simulateMouseEvent(event, 'mousemove');
// Simulate the mousedown event
simulateMouseEvent(event, 'mousedown');
};
/**
* Handle the jQuery UI widget's touchmove events
* @param {Object} event The document's touchmove event
*/
mouseProto._touchMove = function (event) {
// Ignore event if not handled
if (!touchHandled) {
return;
}
// Interaction was not a click
this._touchMoved = true;
// Simulate the mousemove event
simulateMouseEvent(event, 'mousemove');
};
/**
* Handle the jQuery UI widget's touchend events
* @param {Object} event The document's touchend event
*/
mouseProto._touchEnd = function (event) {
// Ignore event if not handled
if (!touchHandled) {
return;
}
// Simulate the mouseup event
simulateMouseEvent(event, 'mouseup');
// Simulate the mouseout event
simulateMouseEvent(event, 'mouseout');
// If the touch interaction did not move, it should trigger a click
if (!this._touchMoved) {
// Simulate the click event
simulateMouseEvent(event, 'click');
}
// Unset the flag to allow other widgets to inherit the touch event
touchHandled = false;
};
/**
* A duck punch of the $.ui.mouse _mouseInit method to support touch events.
* This method extends the widget with bound touch event handlers that
* translate touch events to mouse events and pass them to the widget's
* original mouse event handling methods.
*/
mouseProto._mouseInit = function () {
var self = this;
// Delegate the touch handlers to the widget's element
self.element.bind({
touchstart: $.proxy(self, '_touchStart'),
touchmove: $.proxy(self, '_touchMove'),
touchend: $.proxy(self, '_touchEnd')
});
// Call the original $.ui.mouse init method
_mouseInit.call(self);
};
/**
* Remove the touch event handlers
*/
mouseProto._mouseDestroy = function () {
var self = this;
// Delegate the touch handlers to the widget's element
self.element.unbind({
touchstart: $.proxy(self, '_touchStart'),
touchmove: $.proxy(self, '_touchMove'),
touchend: $.proxy(self, '_touchEnd')
});
// Call the original $.ui.mouse destroy method
_mouseDestroy.call(self);
};
})($);

File diff suppressed because one or more lines are too long