Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Touch events #7

Open
wants to merge 3 commits into
base: bs5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions src/motion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import $ from 'jquery';
import {Events} from './events.js';
import { Events } from './events.js';

export class Motion extends Events {

Expand All @@ -8,27 +8,36 @@ export class Motion extends Events {
this._down_handle = null;
this._down_scope = null;
this._move_scope = null;
this._touchstart_scope = null;
this._touchmove_scope = null;
}

reset_state() {
this._move_handle = null;
this._touchmove_handle = null;
this._up_handle = null;
this._touchend_handle = null;
this._prev_pos = null;
this._motion = null;
}

set_scope(down, move) {
if (this._up_handle) {
set_scope(down, move, touchstart, touchmove) {
if (this._up_handle || this._touchend_handle) {
throw 'Attempt to set motion scope while handling';
}
this.reset_state();
if (this._down_handle) {
$(this._down_scope).off('mousedown', this._down_handle);
$(this._down_scope).off('touchstart', this._touchstart.bind(this));
}
this._down_handle = this._mousedown.bind(this);
this._down_scope = down;
$(down).on('mousedown', this._down_handle);
this._move_scope = move ? move : null;
// touch events
const touch_start = this._touchstart_scope = touchstart ? touchstart : down;
$(touch_start).on('touchstart', this._touchstart.bind(this));
this._touchmove_scope = touchmove ? touchmove : this._move_scope;
}

_mousedown(evt) {
Expand Down Expand Up @@ -67,4 +76,41 @@ export class Motion extends Events {
this.trigger('up', evt);
this.reset_state();
}

_touchstart(evt) {
let touch = evt.originalEvent.touches[0];
this._motion = false;
this._prev_pos = {
x: touch.pageX,
y: touch.pageY
};
if (this._touchmove_scope) {
this._touchmove_handle = this._touchmove.bind(this);
$(this._touchmove_scope).on('touchmove', this._touchmove_handle);
}
this._touchend_handle = this._touchend.bind(this);
$(document).on('touchend', this._touchend_handle);
this.trigger('touchstart', evt);
}

_touchmove(evt) {
let touch = evt.originalEvent.touches[0];
this._motion = true;
evt.motion = this._motion;
evt.prev_pos = this._prev_pos;
this.trigger('touchmove', evt);
this._prev_pos.x = touch.pageX;
this._prev_pos.y = touch.pageY;
}

_touchend(evt) {
evt.stopPropagation();
if (this._touchmove_scope) {
$(this._touchmove_scope).off('touchmove', this._touchmove_handle);
}
$(document).off('touchend', this._touchend_handle);
evt.motion = this._motion;
this.trigger('touchend', evt);
this.reset_state();
}
}
46 changes: 44 additions & 2 deletions treibstoff/bundle/treibstoff.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -1532,25 +1532,33 @@ var ts = (function (exports, $) {
this._down_handle = null;
this._down_scope = null;
this._move_scope = null;
this._touchstart_scope = null;
this._touchmove_scope = null;
}
reset_state() {
this._move_handle = null;
this._touchmove_handle = null;
this._up_handle = null;
this._touchend_handle = null;
this._prev_pos = null;
this._motion = null;
}
set_scope(down, move) {
if (this._up_handle) {
set_scope(down, move, touchstart, touchmove) {
if (this._up_handle || this._touchend_handle) {
throw 'Attempt to set motion scope while handling';
}
this.reset_state();
if (this._down_handle) {
$(this._down_scope).off('mousedown', this._down_handle);
$(this._down_scope).off('touchstart', this._touchstart.bind(this));
}
this._down_handle = this._mousedown.bind(this);
this._down_scope = down;
$(down).on('mousedown', this._down_handle);
this._move_scope = move ? move : null;
const touch_start = this._touchstart_scope = touchstart ? touchstart : down;
$(touch_start).on('touchstart', this._touchstart.bind(this));
this._touchmove_scope = touchmove ? touchmove : this._move_scope;
}
_mousedown(evt) {
evt.stopPropagation();
Expand Down Expand Up @@ -1586,6 +1594,40 @@ var ts = (function (exports, $) {
this.trigger('up', evt);
this.reset_state();
}
_touchstart(evt) {
let touch = evt.originalEvent.touches[0];
this._motion = false;
this._prev_pos = {
x: touch.pageX,
y: touch.pageY
};
if (this._touchmove_scope) {
this._touchmove_handle = this._touchmove.bind(this);
$(this._touchmove_scope).on('touchmove', this._touchmove_handle);
}
this._touchend_handle = this._touchend.bind(this);
$(document).on('touchend', this._touchend_handle);
this.trigger('touchstart', evt);
}
_touchmove(evt) {
let touch = evt.originalEvent.touches[0];
this._motion = true;
evt.motion = this._motion;
evt.prev_pos = this._prev_pos;
this.trigger('touchmove', evt);
this._prev_pos.x = touch.pageX;
this._prev_pos.y = touch.pageY;
}
_touchend(evt) {
evt.stopPropagation();
if (this._touchmove_scope) {
$(this._touchmove_scope).off('touchmove', this._touchmove_handle);
}
$(document).off('touchend', this._touchend_handle);
evt.motion = this._motion;
this.trigger('touchend', evt);
this.reset_state();
}
}

class Widget extends Motion {
Expand Down
Loading
Loading