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

Commit

Permalink
Implement TouchEvent wrappers
Browse files Browse the repository at this point in the history
Fixes #425
  • Loading branch information
arv committed Apr 28, 2014
1 parent 180af95 commit 2a182ff
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"src/MutationObserver.js",
"src/TreeScope.js",
"src/wrappers/events.js",
"src/wrappers/TouchEvent.js",
"src/wrappers/NodeList.js",
"src/wrappers/HTMLCollection.js",
"src/wrappers/Node.js",
Expand Down
1 change: 1 addition & 0 deletions shadowdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'src/MutationObserver.js',
"src/TreeScope.js",
'src/wrappers/events.js',
'src/wrappers/TouchEvent.js',
'src/wrappers/NodeList.js',
'src/wrappers/HTMLCollection.js',
'src/wrappers/Node.js',
Expand Down
122 changes: 122 additions & 0 deletions src/wrappers/TouchEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright 2014 The Polymer Authors. All rights reserved.
* Use of this source code is goverened by a BSD-style
* license that can be found in the LICENSE file.
*/

(function(scope) {
'use strict';

var UIEvent = scope.wrappers.UIEvent;
var mixin = scope.mixin;
var registerWrapper = scope.registerWrapper;
var wrap = scope.wrap;

// TouchEvent is WebKit/Blink only.
var OriginalTouchEvent = window.TouchEvent;
if (!OriginalTouchEvent)
return;

var nativeEvent;
try {
nativeEvent = document.createEvent('TouchEvent');
} catch (ex) {
// In Chrome creating a TouchEvent fails if the feature is not turned on
// which it isn't on desktop Chrome.
return;
}

function nonEnum(obj, prop) {
Object.defineProperty(obj, prop, nonEnumDescriptor);
}

function Touch(impl) {
this.impl = impl;
}

Touch.prototype = {
get target() {
return wrap(this.impl.target);
}
};

var descr = {
configurable: true,
enumerable: true,
get: null
};

[
'clientX',
'clientY',
'screenX',
'screenY',
'pageX',
'pageY',
'identifier',
'webkitRadiusX',
'webkitRadiusY',
'webkitRotationAngle',
'webkitForce'
].forEach(function(name) {
descr.get = function() {
return this.impl[name];
};
Object.defineProperty(Touch.prototype, name, descr);
});

function TouchList() {
this.length = 0;
nonEnum(this, 'length');
}

TouchList.prototype = {
item: function(index) {
return this[index];
}
};

function wrapTouchList(nativeTouchList) {
var list = new TouchList();
for (var i = 0; i < nativeTouchList.length; i++) {
list[i] = new Touch(nativeTouchList[i]);
}
list.length = i;
return list;
}

function TouchEvent(impl) {
UIEvent.call(this, impl);
}

TouchEvent.prototype = Object.create(UIEvent.prototype);

mixin(TouchEvent.prototype, {
get touches() {
return wrapTouchList(unwrap(this).touches);
},

get targetTouches() {
return wrapTouchList(unwrap(this).targetTouches);
},

get changedTouches() {
return wrapTouchList(unwrap(this).changedTouches);
},

initTouchEvent: function() {
// The only way to use this is to reuse the TouchList from an existing
// TouchEvent. Since this is WebKit/Blink proprietary API we will not
// implement this until someone screams.
throw new Error('Not implemented');
}
});

registerWrapper(OriginalTouchEvent, TouchEvent, nativeEvent);

scope.wrappers.Touch = Touch;
scope.wrappers.TouchEvent = TouchEvent;
scope.wrappers.TouchList = TouchList;

})(window.ShadowDOMPolyfill);

60 changes: 60 additions & 0 deletions test/js/TouchEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2014 The Polymer Authors. All rights reserved.
* Use of this source code is goverened by a BSD-style
* license that can be found in the LICENSE file.
*/

htmlSuite('Events', function() {

var unwrap = ShadowDOMPolyfill.unwrap;
var wrap = ShadowDOMPolyfill.wrap;

try {
document.createEvent('TouchEvent');
} catch (ex) {
// Touch events are not supported
return;
}

test('TouchEvent', function() {
var e = document.createEvent('TouchEvent');
assert.instanceOf(e, TouchEvent);
assert.instanceOf(e, UIEvent);
assert.instanceOf(e, Event);
});

test('Touch', function() {
// There is no way to create a native Touch object so we use a mock impl.

var target = document.createElement('div');
var impl = {
clientX: 1,
clientY: 2,
screenX: 3,
screenY: 4,
pageX: 5,
pageY: 6,
identifier: 7,
webkitRadiusX: 8,
webkitRadiusY: 9,
webkitRotationAngle: 10,
webkitForce: 11,
target: unwrap(target)
};
var touch = new Touch(impl);

assert.equal(touch.clientX, 1);
assert.equal(touch.clientY, 2);
assert.equal(touch.screenX, 3);
assert.equal(touch.screenY, 4);
assert.equal(touch.pageX, 5);
assert.equal(touch.pageY, 6);
assert.equal(touch.identifier, 7);
assert.equal(touch.webkitRadiusX, 8);
assert.equal(touch.webkitRadiusY, 9);
assert.equal(touch.webkitRotationAngle, 10);
assert.equal(touch.webkitForce, 11);
assert.equal(touch.target, target);
});

});
1 change: 1 addition & 0 deletions test/test.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ var modules = [
'Selection.js',
'ShadowRoot.js',
'Text.js',
'TouchEvent.js',
'TreeScope.js',
'Window.js',
'createTable.js',
Expand Down

0 comments on commit 2a182ff

Please sign in to comment.