This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #427 from arv/touch-event-list
Implement TouchEvent wrappers
- Loading branch information
Showing
5 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters