Permalink
Browse files

Add clone methods

  • Loading branch information...
1 parent adbeb28 commit c0d8043db8a6dd9c45cd4e29d6413018182c514a @davidaurelio davidaurelio committed Feb 27, 2013
Showing with 91 additions and 4 deletions.
  1. +46 −4 src/renderer/event.js
  2. +45 −0 test/renderer/event-spec.js
View
@@ -34,10 +34,10 @@ define(function() {
*/
KeyboardEvent.fromDomEvent = function(type, keys, targetValue) {
var modifiers =
- !!keys.altKey * ALT_KEY |
- !!keys.ctrlKey * CTRL_KEY |
- !!keys.metaKey * META_KEY |
- !!keys.shiftKey * SHIFT_KEY;
+ (keys.altKey ? ALT_KEY : 0) |
+ (keys.ctrlKey ? CTRL_KEY : 0) |
+ (keys.metaKey ? META_KEY : 0) |
+ (keys.shiftKey ? SHIFT_KEY : 0);
return new KeyboardEvent(undefined, keys.keyCode, modifiers, targetValue);
};
KeyboardEvent.NO_MODIFIER = NO_MODIFIER;
@@ -46,6 +46,20 @@ define(function() {
KeyboardEvent.META_KEY = META_KEY;
KeyboardEvent.SHIFT_KEY = SHIFT_KEY;
+ KeyboardEvent.prototype.clone = function(type) {
+ var modifiers = (this.altKey ? ALT_KEY : 0) |
+ (this.ctrlKey ? CTRL_KEY : 0) |
+ (this.metaKey ? META_KEY : 0) |
+ (this.shiftKey ? SHIFT_KEY : 0);
+
+ return new KeyboardEvent(
+ type || this.type,
+ this.keyCode,
+ modifiers,
+ this.inputValue
+ );
+ };
+
/**
* Represents a pointer event (either mouse or touch)
*
@@ -81,6 +95,34 @@ define(function() {
return new PointerEvent(undefined, clientX - stageClientX, clientY - stageClientY, clientX, clientY);
};
+ /**
+ * Clones the PointerEvent instance. Optionally sets the type property to a
+ * different value.
+ *
+ * @param {string} [type] Optional new event .type
+ * @return {PointerEvent}
+ */
+ PointerEvent.prototype.clone = function(type) {
+ var clone = new PointerEvent(
+ type || this.type,
+ this.x,
+ this.y,
+ this.clientX,
+ this.clientY
+ );
+ clone.deltaX = this.deltaX;
+ clone.deltaY = this.deltaY;
+ clone.diffX = this.diffX;
+ clone.diffY = this.diffY;
+ clone.isLeft = this.isLeft;
+ clone.isRight = this.isRight;
+ clone.isMiddle = this.isMiddle;
+ clone.isLeft = this.isLeft;
+ clone.touchId = this.touchId;
+ clone.touchIndex = this.touchIndex;
+ return clone;
+ };
+
return {
KeyboardEvent: KeyboardEvent,
PointerEvent: PointerEvent
@@ -72,6 +72,29 @@ define([
});
});
});
+
+ describe('#clone()', function() {
+ var pointerEvent = new PointerEvent('arbitrary', 123, 345, 567, 789);
+ pointerEvent.deltaX = 10;
+ pointerEvent.deltaY = 0;
+ pointerEvent.isLeft = true;
+ pointerEvent.touchId = 123;
+ it('should return a PointerEvent instance', function() {
+ expect(pointerEvent.clone()).toBeInstanceOf(PointerEvent);
+ });
+ it('should copy all property values to the clone', function() {
+ expect(pointerEvent.clone()).toEqual(pointerEvent);
+ });
+ it('should not return the instance itself', function() {
+ expect(pointerEvent.clone()).not.toBe(pointerEvent);
+ });
+ it('should change the "type" property if called with an argument', function() {
+ var expected = pointerEvent.clone();
+ var newType = 'othertype';
+ expected.type = newType;
+ expect(pointerEvent.clone(newType)).toEqual(expected);
+ });
+ });
});
describe('KeyboardEvent', function() {
@@ -150,6 +173,28 @@ define([
});
});
});
+
+ describe('#clone()', function() {
+ var modifiers = KeyboardEvent.SHIFT_KEY | KeyboardEvent.META_KEY;
+ var keyboardEvent = new KeyboardEvent('arbitrary', 0x123, modifiers, 'arbitrary value');
+
+ it('should return a Keyboard event instance', function() {
+ expect(keyboardEvent.clone()).toBeInstanceOf(KeyboardEvent);
+ });
+ it('should copy all property values to the clone', function() {
+ expect(keyboardEvent.clone()).toEqual(keyboardEvent);
+ });
+ it('should not return the instance itself', function() {
+ expect(keyboardEvent.clone()).not.toBe(keyboardEvent);
+ });
+ it('should change the "type" property if called with an argument', function() {
+ var expected = keyboardEvent.clone();
+ var newType = 'some different type';
+ expected.type = newType;
+
+ expect(keyboardEvent.clone(newType)).toEqual(expected);
+ });
+ });
});
});
});

0 comments on commit c0d8043

Please sign in to comment.