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

Commit

Permalink
Merge pull request #102 from azakus/faster-constructor
Browse files Browse the repository at this point in the history
Faster constructor and Event cloning
  • Loading branch information
dfreedm committed Oct 9, 2013
2 parents 8c006ee + 46ea569 commit 6fd360b
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 90 deletions.
2 changes: 1 addition & 1 deletion build.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[
"../WeakMap/weakmap.js",
"src/boot.js",
"src/touch-action.js",
"src/PointerEvent.js",
"src/pointermap.js",
"src/sidetable.js",
"src/dispatcher.js",
"src/installer.js",
"src/mouse.js",
Expand Down
34 changes: 17 additions & 17 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ module.exports = function(karma) {
var common = require('../tools/test/karma-common.conf.js');
karma.set(common.mixin_common_opts(karma, {
// base path, that will be used to resolve files and exclude
basePath: '',
basePath: '../',

// list of files / patterns to load in the browser
files: [
'node_modules/chai/chai.js',
'node_modules/chai-spies/chai-spies.js',
'src/boot.js',
'src/PointerEvent.js',
'src/pointermap.js',
'src/sidetable.js',
'src/dispatcher.js',
'src/installer.js',
'src/mouse.js',
'src/touch.js',
'src/ms.js',
'src/platform-events.js',
'src/capture.js',
'tests/setup.js',
'tests/karma-setup.js',
'tests/!(setup|karma-setup).js'
'PointerEvents/node_modules/chai/chai.js',
'PointerEvents/node_modules/chai-spies/chai-spies.js',
'WeakMap/weakmap.js',
'PointerEvents/src/boot.js',
'PointerEvents/src/PointerEvent.js',
'PointerEvents/src/pointermap.js',
'PointerEvents/src/dispatcher.js',
'PointerEvents/src/installer.js',
'PointerEvents/src/mouse.js',
'PointerEvents/src/touch.js',
'PointerEvents/src/ms.js',
'PointerEvents/src/platform-events.js',
'PointerEvents/src/capture.js',
'PointerEvents/tests/setup.js',
'PointerEvents/tests/karma-setup.js',
'PointerEvents/tests/!(setup|karma-setup).js'
],
}));
};
2 changes: 1 addition & 1 deletion pointerevents.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
var thisFile = 'pointerevents.js';
var scopeName = 'PointerEventsPolyfill';
var modules = [
'../WeakMap/weakmap.js',
'src/boot.js',
'src/touch-action.js',
'src/PointerEvent.js',
'src/pointermap.js',
'src/sidetable.js',
'src/dispatcher.js',
'src/installer.js',
'src/mouse.js',
Expand Down
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 WeakMap(),
handledEvents: new WeakMap(),
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
40 changes: 0 additions & 40 deletions src/sidetable.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

// handler block for native touch events
var touchEvents = {
scrollType: new SideTable,
scrollType: new WeakMap(),
events: [
'touchstart',
'touchmove',
Expand Down
4 changes: 2 additions & 2 deletions tests/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ suite('Loader', function() {
test('PointerMap', function() {
expect(pep.PointerMap).to.be.ok;
});
test('SideTable', function() {
expect(SideTable).to.be.ok;
test('WeakMap', function() {
expect(WeakMap).to.be.ok;
});
test('Dispatcher', function() {
expect(pep.dispatcher).to.be.ok;
Expand Down

0 comments on commit 6fd360b

Please sign in to comment.