Skip to content
Browse files

Add `PointerEvent.fromDomMouseEvent()`

  • Loading branch information...
1 parent c0d8043 commit f3328e283cfc4521e09981f0c991c5000c1c6026 @davidaurelio davidaurelio committed Feb 27, 2013
Showing with 83 additions and 0 deletions.
  1. +25 −0 src/renderer/event.js
  2. +58 −0 test/renderer/event-spec.js
View
25 src/renderer/event.js
@@ -1,6 +1,14 @@
define(function() {
'use strict';
+ var eventTypeMap = {
+ mouseup: 'pointerup',
+ mousedown: 'pointerdown',
+ mousemove: 'pointermove',
+ mouseover: 'mouseover',
+ mouseout: 'mouseout'
+ };
+
/** @const */
var NO_MODIFIER = 0, ALT_KEY = 1 << 0, CTRL_KEY = 1 << 1, META_KEY = 1 << 2, SHIFT_KEY = 1 << 3;
@@ -96,6 +104,23 @@ define(function() {
};
/**
+ * Creates a PointerEvent from a DOM MouseEvent
+ *
+ * @param {MouseEvent} domEvent
+ * @param {number} stageX The x offset of the stage relative to the viewport
+ * @param {number} stageY The y offset of the stage relative to the viewport
+ * @returns {PointerEvent}
+ */
+ PointerEvent.fromDomMouseEvent = function(domEvent, stageX, stageY) {
+ var clientX = domEvent.clientX, clientY = domEvent.clientY;
+ return new PointerEvent(eventTypeMap[domEvent.type], clientX - stageX, clientY - stageY, clientX, clientY);
+ };
+
+ PointerEvent.fromDomTouchEvent = function() {
+
+ };
+
+ /**
* Clones the PointerEvent instance. Optionally sets the type property to a
* different value.
*
View
58 test/renderer/event-spec.js
@@ -3,6 +3,12 @@ define([
], function(event) {
'use strict';
+ function eachProperty(object, callback) {
+ for (var key in object) {
+ callback(object[key], key);
+ }
+ }
+
describe('renderer events', function() {
describe('PointerEvent', function() {
var PointerEvent = event.PointerEvent;
@@ -54,6 +60,58 @@ define([
});
});
+
+ describe('factories', function() {
+ var clientX = 123, clientY = 456;
+ var stageX = 78, stageY = 90;
+
+ describe('.fromDomMouseEvent', function() {
+ function createDomMouseEvent(type) {
+ return {
+ type: type,
+ clientX: clientX,
+ clientY: clientY
+ };
+ }
+ it('should return a PointerEvent', function() {
+ var pointerEvent = PointerEvent.fromDomMouseEvent(createDomMouseEvent('arbitrary'), stageX, stageY);
+ expect(pointerEvent)
+ .toBeInstanceOf(PointerEvent);
+ });
+
+ it('should initialize all offset properties from the mouse event', function() {
+ var pointerEvent = PointerEvent.fromDomMouseEvent(createDomMouseEvent('arbitrary'), stageX, stageY);
+ expect(pointerEvent)
+ .toHaveOwnProperties({
+ stageX: clientX - stageX,
+ x: clientX - stageX,
+ stageY: clientY - stageY,
+ y: clientY - stageY,
+ clientX: clientX,
+ clientY: clientY
+ });
+ });
+
+ eachProperty(
+ {
+ mouseup: 'pointerup',
+ mousedown: 'pointerdown',
+ mousemove: 'pointermove',
+ mouseover: 'mouseover',
+ mouseout: 'mouseout'
+ },
+ function(expectedType, domEventType) {
+ it('should create ' + expectedType + ' events to ' + domEventType + ' events', function() {
+ var pointerEvent = PointerEvent.fromDomMouseEvent(
+ createDomMouseEvent(domEventType), stageX, stageY
+ );
+ expect(pointerEvent).toHaveOwnProperties({type: expectedType});
+ });
+ }
+ );
+ });
+ });
+
describe('.fromDomEvent', function() {
var clientX = 123, clientY = 456;
var stageOffsetX = 78, stageOffsetY = 90;

0 comments on commit f3328e2

Please sign in to comment.
Something went wrong with that request. Please try again.