Skip to content

Commit b03ce53

Browse files
author
Oskar Eriksson
committed
Added basic touch event support
1 parent 654f649 commit b03ce53

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

src/common/api/Touch.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default class Touch {
2+
constructor(touch) {
3+
this.clientX = touch.clientX;
4+
this.clientY = touch.clientY;
5+
this.identifier = touch.identifier;
6+
this.pageX = touch.pageX;
7+
this.pageY = touch.pageY;
8+
this.screenX = touch.screenX;
9+
this.screenY = touch.screenY;
10+
}
11+
}

src/common/api/TouchList.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Touch from './Touch';
2+
3+
export default class TouchList {
4+
constructor(touches) {
5+
touches.forEach((touch, index) => {
6+
const touchMock = new Touch(touch);
7+
this[index] = touchMock;
8+
})
9+
}
10+
11+
get length() {
12+
let hasMoreTouches = true;
13+
let index = 0;
14+
while(hasMoreTouches) {
15+
if (this[index]) {
16+
index += 1;
17+
} else {
18+
hasMoreTouches = false;
19+
}
20+
}
21+
return index;
22+
}
23+
24+
item(index) {
25+
return this[index];
26+
}
27+
}

src/common/channel.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {WORKER_MESSAGES as _} from './constants';
2+
import TouchList from './api/TouchList';
23

34
export default class Channel {
45
constructor(channel) {
@@ -30,12 +31,36 @@ export default class Channel {
3031
selected: e.target.selected
3132
}
3233
};
34+
// TODO: Create general event parsing method to handle all even types
35+
if (isTouchEvent(e)) {
36+
result = Object.assign(result, getTouchProperties(e));
37+
}
3338
return JSON.stringify(result);
3439
}
3540

3641
static deserializeEvent(msg) {
37-
var e = JSON.parse(msg);
42+
let e = JSON.parse(msg);
43+
if (isTouchEvent(e)) {
44+
e = Object.assign(e, getTouchProperties(e));
45+
}
3846
e.preventDefault = e.stopPropgation = function () { }
3947
return e;
4048
}
4149
}
50+
51+
function isTouchEvent(e) {
52+
return e.type.match(/touchmove|touchstart|touchend|touchcancel/)
53+
}
54+
55+
function getTouchProperties(e) {
56+
// Support only one touch at the moment
57+
const targetTouches = new TouchList([e.targetTouches[0]]);
58+
const touches = new TouchList([e.touches[0]]);
59+
const changedTouches = new TouchList([e.changedTouches[0]]);
60+
return {
61+
targetTouches,
62+
touches,
63+
changedTouches,
64+
}
65+
66+
}

src/page/Dom.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {OPS as _, WORKER_MESSAGES} from './../common/constants';
22
import Channel from './../common/channel';
33
import {DOCUMENT_NODE} from './../common/nodeType';
44

5+
import TouchList from '../common/api/TouchList';
6+
57
var body, channel, container, head, nodes = {};
68

79
export default (ctr, messageChannel) => {

webpack.config.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ module.exports = {
2727
}]
2828
},
2929
plugins: [
30-
new webpack.optimize.UglifyJsPlugin({
31-
compress: {
32-
warnings: false
33-
}
34-
}),
35-
new webpack.optimize.DedupePlugin(),
3630
new webpack.DefinePlugin({
3731
'process.env': {
3832
'NODE_ENV': JSON.stringify('production')

0 commit comments

Comments
 (0)