Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit 68c2bfa

Browse files
committed
Merge pull request #427 from arv/touch-event-list
Implement TouchEvent wrappers
2 parents 180af95 + 2a182ff commit 68c2bfa

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed

build.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"src/MutationObserver.js",
77
"src/TreeScope.js",
88
"src/wrappers/events.js",
9+
"src/wrappers/TouchEvent.js",
910
"src/wrappers/NodeList.js",
1011
"src/wrappers/HTMLCollection.js",
1112
"src/wrappers/Node.js",

shadowdom.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'src/MutationObserver.js',
2323
"src/TreeScope.js",
2424
'src/wrappers/events.js',
25+
'src/wrappers/TouchEvent.js',
2526
'src/wrappers/NodeList.js',
2627
'src/wrappers/HTMLCollection.js',
2728
'src/wrappers/Node.js',

src/wrappers/TouchEvent.js

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2014 The Polymer Authors. All rights reserved.
3+
* Use of this source code is goverened by a BSD-style
4+
* license that can be found in the LICENSE file.
5+
*/
6+
7+
(function(scope) {
8+
'use strict';
9+
10+
var UIEvent = scope.wrappers.UIEvent;
11+
var mixin = scope.mixin;
12+
var registerWrapper = scope.registerWrapper;
13+
var wrap = scope.wrap;
14+
15+
// TouchEvent is WebKit/Blink only.
16+
var OriginalTouchEvent = window.TouchEvent;
17+
if (!OriginalTouchEvent)
18+
return;
19+
20+
var nativeEvent;
21+
try {
22+
nativeEvent = document.createEvent('TouchEvent');
23+
} catch (ex) {
24+
// In Chrome creating a TouchEvent fails if the feature is not turned on
25+
// which it isn't on desktop Chrome.
26+
return;
27+
}
28+
29+
function nonEnum(obj, prop) {
30+
Object.defineProperty(obj, prop, nonEnumDescriptor);
31+
}
32+
33+
function Touch(impl) {
34+
this.impl = impl;
35+
}
36+
37+
Touch.prototype = {
38+
get target() {
39+
return wrap(this.impl.target);
40+
}
41+
};
42+
43+
var descr = {
44+
configurable: true,
45+
enumerable: true,
46+
get: null
47+
};
48+
49+
[
50+
'clientX',
51+
'clientY',
52+
'screenX',
53+
'screenY',
54+
'pageX',
55+
'pageY',
56+
'identifier',
57+
'webkitRadiusX',
58+
'webkitRadiusY',
59+
'webkitRotationAngle',
60+
'webkitForce'
61+
].forEach(function(name) {
62+
descr.get = function() {
63+
return this.impl[name];
64+
};
65+
Object.defineProperty(Touch.prototype, name, descr);
66+
});
67+
68+
function TouchList() {
69+
this.length = 0;
70+
nonEnum(this, 'length');
71+
}
72+
73+
TouchList.prototype = {
74+
item: function(index) {
75+
return this[index];
76+
}
77+
};
78+
79+
function wrapTouchList(nativeTouchList) {
80+
var list = new TouchList();
81+
for (var i = 0; i < nativeTouchList.length; i++) {
82+
list[i] = new Touch(nativeTouchList[i]);
83+
}
84+
list.length = i;
85+
return list;
86+
}
87+
88+
function TouchEvent(impl) {
89+
UIEvent.call(this, impl);
90+
}
91+
92+
TouchEvent.prototype = Object.create(UIEvent.prototype);
93+
94+
mixin(TouchEvent.prototype, {
95+
get touches() {
96+
return wrapTouchList(unwrap(this).touches);
97+
},
98+
99+
get targetTouches() {
100+
return wrapTouchList(unwrap(this).targetTouches);
101+
},
102+
103+
get changedTouches() {
104+
return wrapTouchList(unwrap(this).changedTouches);
105+
},
106+
107+
initTouchEvent: function() {
108+
// The only way to use this is to reuse the TouchList from an existing
109+
// TouchEvent. Since this is WebKit/Blink proprietary API we will not
110+
// implement this until someone screams.
111+
throw new Error('Not implemented');
112+
}
113+
});
114+
115+
registerWrapper(OriginalTouchEvent, TouchEvent, nativeEvent);
116+
117+
scope.wrappers.Touch = Touch;
118+
scope.wrappers.TouchEvent = TouchEvent;
119+
scope.wrappers.TouchList = TouchList;
120+
121+
})(window.ShadowDOMPolyfill);
122+

test/js/TouchEvent.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2014 The Polymer Authors. All rights reserved.
3+
* Use of this source code is goverened by a BSD-style
4+
* license that can be found in the LICENSE file.
5+
*/
6+
7+
htmlSuite('Events', function() {
8+
9+
var unwrap = ShadowDOMPolyfill.unwrap;
10+
var wrap = ShadowDOMPolyfill.wrap;
11+
12+
try {
13+
document.createEvent('TouchEvent');
14+
} catch (ex) {
15+
// Touch events are not supported
16+
return;
17+
}
18+
19+
test('TouchEvent', function() {
20+
var e = document.createEvent('TouchEvent');
21+
assert.instanceOf(e, TouchEvent);
22+
assert.instanceOf(e, UIEvent);
23+
assert.instanceOf(e, Event);
24+
});
25+
26+
test('Touch', function() {
27+
// There is no way to create a native Touch object so we use a mock impl.
28+
29+
var target = document.createElement('div');
30+
var impl = {
31+
clientX: 1,
32+
clientY: 2,
33+
screenX: 3,
34+
screenY: 4,
35+
pageX: 5,
36+
pageY: 6,
37+
identifier: 7,
38+
webkitRadiusX: 8,
39+
webkitRadiusY: 9,
40+
webkitRotationAngle: 10,
41+
webkitForce: 11,
42+
target: unwrap(target)
43+
};
44+
var touch = new Touch(impl);
45+
46+
assert.equal(touch.clientX, 1);
47+
assert.equal(touch.clientY, 2);
48+
assert.equal(touch.screenX, 3);
49+
assert.equal(touch.screenY, 4);
50+
assert.equal(touch.pageX, 5);
51+
assert.equal(touch.pageY, 6);
52+
assert.equal(touch.identifier, 7);
53+
assert.equal(touch.webkitRadiusX, 8);
54+
assert.equal(touch.webkitRadiusY, 9);
55+
assert.equal(touch.webkitRotationAngle, 10);
56+
assert.equal(touch.webkitForce, 11);
57+
assert.equal(touch.target, target);
58+
});
59+
60+
});

test/test.main.js

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ var modules = [
118118
'Selection.js',
119119
'ShadowRoot.js',
120120
'Text.js',
121+
'TouchEvent.js',
121122
'TreeScope.js',
122123
'Window.js',
123124
'createTable.js',

0 commit comments

Comments
 (0)