FIX: dragging of timeline was flaky on iOS

This commit is contained in:
Sam 2018-04-30 16:12:12 +10:00
parent 8b62b59b2e
commit 6be2108291

View File

@ -54,11 +54,10 @@ let _watchingDocument = false;
let _dragging;
const DRAG_NAME = "mousemove.discourse-widget-drag";
const DRAG_NAME_TOUCH = "touchmove.discourse-widget-drag";
function cancelDrag(e) {
function cancelDrag(e, onDrag) {
$('body').removeClass('widget-dragging');
$(document).off(DRAG_NAME).off(DRAG_NAME_TOUCH);
document.removeEventListener('touchmove', onDrag);
if (_dragging) {
if (_dragging.dragEnd) { _dragging.dragEnd(e); }
@ -69,25 +68,41 @@ function cancelDrag(e) {
WidgetClickHook.setupDocumentCallback = function() {
if (_watchingDocument) { return; }
document.addEventListener('touchmove', e => {
e.preventDefault();
e.stopPropagation();
}, { passive: false, capture: true });
let widget;
let onDrag = dragE => {
const tt = dragE.targetTouches[0];
if (tt) {
dragE.preventDefault();
dragE.stopPropagation();
widget.drag(tt);
}
};
$(document).on('mousedown.discource-widget-drag, touchstart.discourse-widget-drag', e => {
cancelDrag(e);
const widget = findWidget(e.target, DRAG_ATTRIBUTE_NAME);
cancelDrag(e, onDrag);
widget = findWidget(e.target, DRAG_ATTRIBUTE_NAME);
if (widget) {
e.preventDefault();
e.stopPropagation();
_dragging = widget;
$('body').addClass('widget-dragging');
$(document).on(DRAG_NAME, dragE => widget.drag(dragE));
$(document).on(DRAG_NAME_TOUCH, dragE => {
const tt = dragE.originalEvent.targetTouches[0];
if (tt) {
widget.drag(tt);
$(document).on(DRAG_NAME, dragE => {
if (widget) {
widget.drag(dragE);
}
});
document.addEventListener('touchmove', onDrag, { passive: false, capture: true });
}
});
$(document).on('mouseup.discourse-widget-drag, touchend.discourse-widget-drag', e => cancelDrag(e));
$(document).on('mouseup.discourse-widget-drag, touchend.discourse-widget-drag', e => cancelDrag(e, onDrag));
$(document).on('click.discourse-widget', e => {
nodeCallback(e.target, CLICK_ATTRIBUTE_NAME, w => w.click(e));
@ -96,9 +111,9 @@ WidgetClickHook.setupDocumentCallback = function() {
const $outside = $('[data-click-outside]');
$outside.each((i, outNode) => {
if (outNode.contains(node)) { return; }
const widget = outNode[CLICK_OUTSIDE_ATTRIBUTE_NAME];
if (widget) {
widget.clickOutside(e);
const widget2 = outNode[CLICK_OUTSIDE_ATTRIBUTE_NAME];
if (widget2) {
widget2.clickOutside(e);
}
});
});