Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Avoid for-in loops with explicity copied keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dfreedm committed Oct 9, 2013
1 parent 8c006ee commit ae8833f
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 28 deletions.
62 changes: 40 additions & 22 deletions src/PointerEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,40 @@
} catch(e) {
}

var MOUSE_PROPS = [
'bubbles',
'cancelable',
'view',
'detail',
'screenX',
'screenY',
'clientX',
'clientY',
'ctrlKey',
'altKey',
'shiftKey',
'metaKey',
'button',
'relatedTarget',
];

var MOUSE_DEFAULTS = [
false,
false,
null,
null,
0,
0,
0,
0,
false,
false,
false,
false,
0,
null
];

function PointerEvent(inType, inDict) {
inDict = inDict || {};
// According to the w3c spec,
Expand Down Expand Up @@ -67,29 +101,13 @@
e = new MouseEvent(inType, inDict);
} else {
e = document.createEvent('MouseEvent');
// import values from the given dictionary
var props = {
bubbles: false,
cancelable: false,
view: null,
detail: null,
screenX: 0,
screenY: 0,
clientX: 0,
clientY: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
button: 0,
relatedTarget: null
};

Object.keys(props).forEach(function(k) {
if (k in inDict) {
props[k] = inDict[k];
}
});
// import values from the given dictionary
var props = {}, p;
for(var i = 0; i < MOUSE_PROPS.length; i++) {
p = MOUSE_PROPS[i];
props[p] = inDict[p] || MOUSE_DEFAULTS[i];
}

// define the properties inherited from MouseEvent
e.initMouseEvent(
Expand Down
80 changes: 74 additions & 6 deletions src/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,73 @@
*/

(function(scope) {
var CLONE_PROPS = [
// MouseEvent
'bubbles',
'cancelable',
'view',
'detail',
'screenX',
'screenY',
'clientX',
'clientY',
'ctrlKey',
'altKey',
'shiftKey',
'metaKey',
'button',
'relatedTarget',
// DOM Level 3
'buttons',
// PointerEvent
'pointerId',
'width',
'height',
'pressure',
'tiltX',
'tiltY',
'pointerType',
'hwTimestamp',
'isPrimary',
// event instance
'type',
'target',
'currentTarget'
];

var CLONE_DEFAULTS = [
// MouseEvent
false,
false,
null,
null,
0,
0,
0,
0,
false,
false,
false,
false,
0,
null,
// DOM Level 3
0,
// PointerEvent
0,
0,
0,
0,
0,
0,
'',
0,
false,
// event instance
'',
null,
null
];

/**
* This module is for normalizing events. Mouse and Touch events will be
Expand All @@ -19,9 +86,9 @@
* - pointercancel: a pointer will no longer generate events
*/
var dispatcher = {
targets: new SideTable,
handledEvents: new SideTable,
pointermap: new scope.PointerMap,
targets: new SideTable(),
handledEvents: new SideTable(),
pointermap: new scope.PointerMap(),
eventMap: {},
// Scope objects for native events.
// This exists for ease of testing.
Expand Down Expand Up @@ -163,9 +230,10 @@
* properties.
*/
cloneEvent: function(inEvent) {
var eventCopy = {};
for (var n in inEvent) {
eventCopy[n] = inEvent[n];
var eventCopy = {}, p;
for (var i = 0; i < CLONE_PROPS.length; i++) {
p = CLONE_PROPS[i];
eventCopy[p] = inEvent[p] || CLONE_DEFAULTS[i];
}
return eventCopy;
},
Expand Down

0 comments on commit ae8833f

Please sign in to comment.