From 9d7727031ad0e6531f44d5aed073b998cd9a8d62 Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Tue, 8 Oct 2013 18:04:26 -0700 Subject: [PATCH] Use explicit properties to avoid for-in loops --- src/PointerGestureEvent.js | 17 +++----- src/dispatcher.js | 87 +++++++++++++++++++++++++++++++++++--- 2 files changed, 89 insertions(+), 15 deletions(-) diff --git a/src/PointerGestureEvent.js b/src/PointerGestureEvent.js index 5df3d39..9fb202d 100644 --- a/src/PointerGestureEvent.js +++ b/src/PointerGestureEvent.js @@ -19,20 +19,17 @@ function PointerGestureEvent(inType, inDict) { var dict = inDict || {}; var e = document.createEvent('Event'); var props = { - bubbles: true, - cancelable: true, + bubbles: Boolean(dict.bubbles) === dict.bubbles || true, + cancelable: Boolean(dict.cancelable) === dict.cancelable || true }; - Object.keys(props).forEach(function(k) { - if (k in dict) { - props[k] = dict[k]; - } - }); e.initEvent(inType, props.bubbles, props.cancelable); - Object.keys(dict).forEach(function(k) { - e[k] = inDict[k]; - }); + var keys = Object.keys(dict), k; + for (var i = 0; i < keys.length; i++) { + k = keys[i]; + e[k] = dict[k]; + } e.preventTap = this.preventTap; diff --git a/src/dispatcher.js b/src/dispatcher.js index 1ea4c49..5fcf682 100644 --- a/src/dispatcher.js +++ b/src/dispatcher.js @@ -5,9 +5,85 @@ */ (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', + 'screenX', + 'screenY', + 'pageX', + 'pageY', + ]; + + 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, + 0, + 0, + 0, + 0 + ]; + var dispatcher = { - handledEvents: new WeakMap, - targets: new WeakMap, + handledEvents: new WeakMap(), + targets: new WeakMap(), handlers: {}, recognizers: {}, events: {}, @@ -98,9 +174,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; },