From c1d78300542743538a1b3de0f26642555eb0464e Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Tue, 29 Oct 2013 15:47:49 -0700 Subject: [PATCH] Allow for use in old IE by providing a non-getter API for size of pointermap --- src/pointermap.js | 67 ++++++++++++++++++++------------------------- src/touch.js | 6 ++-- tests/pointermap.js | 38 ++++++++++++------------- 3 files changed, 52 insertions(+), 59 deletions(-) diff --git a/src/pointermap.js b/src/pointermap.js index 6f6ff047..d5c57405 100644 --- a/src/pointermap.js +++ b/src/pointermap.js @@ -5,66 +5,59 @@ */ /** - * This module implements an ordered list of pointer states - * Each pointer object here has two properties: - * - id: the id of the pointer - * - event: the source event of the pointer, complete with positions - * - * The ordering of the pointers is from oldest pointer to youngest pointer, - * which allows for multi-pointer gestures to not rely on the actual ids - * imported from the source events. - * - * Any operation that needs to store state information about pointers can hang - * objects off of the pointer in the pointermap. This information will be - * preserved until the pointer is removed from the pointermap. + * This module implements an map of pointer states */ (function(scope) { + var USE_MAP = window.Map && window.Map.prototype.forEach; + var POINTERS_FN = function(){ return this.size; }; function PointerMap() { - this.ids = []; - this.pointers = []; - }; + if (USE_MAP) { + var m = new Map(); + m.pointers = POINTERS_FN; + return m; + } else { + this.keys = []; + this.values = []; + } + } PointerMap.prototype = { set: function(inId, inEvent) { - var i = this.ids.indexOf(inId); + var i = this.keys.indexOf(inId); if (i > -1) { - this.pointers[i] = inEvent; + this.values[i] = inEvent; } else { - this.ids.push(inId); - this.pointers.push(inEvent); + this.keys.push(inId); + this.values.push(inEvent); } }, has: function(inId) { - return this.ids.indexOf(inId) > -1; + return this.keys.indexOf(inId) > -1; }, 'delete': function(inId) { - var i = this.ids.indexOf(inId); + var i = this.keys.indexOf(inId); if (i > -1) { - this.ids.splice(i, 1); - this.pointers.splice(i, 1); + this.keys.splice(i, 1); + this.values.splice(i, 1); } }, get: function(inId) { - var i = this.ids.indexOf(inId); - return this.pointers[i]; - }, - get size() { - return this.pointers.length; + var i = this.keys.indexOf(inId); + return this.values[i]; }, clear: function() { - this.ids.length = 0; - this.pointers.length = 0; + this.keys.length = 0; + this.values.length = 0; }, forEach: function(callback, thisArg) { - this.ids.forEach(function(id, i) { - callback.call(thisArg, id, this.pointers[i], this); + this.keys.forEach(function(id, i) { + callback.call(thisArg, id, this.values[i], this); }, this); + }, + pointers: function() { + return this.keys.length; } }; - if (window.Map && Map.prototype.forEach) { - scope.PointerMap = Map; - } else { - scope.PointerMap = PointerMap; - } + scope.PointerMap = PointerMap; })(window.PointerEventsPolyfill); diff --git a/src/touch.js b/src/touch.js index 389435b8..0a5c45de 100644 --- a/src/touch.js +++ b/src/touch.js @@ -104,7 +104,7 @@ }, setPrimaryTouch: function(inTouch) { // set primary touch if there no pointers, or the only pointer is the mouse - if (pointermap.size == 0 || (pointermap.size == 1 && pointermap.has(1))) { + if (pointermap.pointers() === 0 || (pointermap.pointers() === 1 && pointermap.has(1))) { this.firstTouch = inTouch.identifier; this.firstXY = {X: inTouch.clientX, Y: inTouch.clientY}; this.scrolling = false; @@ -198,9 +198,9 @@ // pointercancel for this "abandoned" touch vacuumTouches: function(inEvent) { var tl = inEvent.touches; - // pointermap.size should be < tl.length here, as the touchstart has not + // pointermap.pointers() should be < tl.length here, as the touchstart has not // been processed yet. - if (pointermap.size >= tl.length) { + if (pointermap.pointers() >= tl.length) { var d = []; pointermap.forEach(function(key, value) { // Never remove pointerId == 1, which is mouse. diff --git a/tests/pointermap.js b/tests/pointermap.js index b2a0131c..b004ccf7 100644 --- a/tests/pointermap.js +++ b/tests/pointermap.js @@ -6,7 +6,7 @@ suite('PointerMap', function() { 'get', 'has', 'delete', - 'size', + 'pointers', 'clear', 'forEach' ]; @@ -16,49 +16,49 @@ suite('PointerMap', function() { }); }); test('PointerMap .set', function() { - var p = new PointerMap; + var p = new PointerMap(); p.set(1, true); if (PointerMap !== window.Map) { - expect(p.ids).to.have.length(1); - expect(p.pointers).to.have.length(1); + expect(p.keys).to.have.length(1); + expect(p.values).to.have.length(1); } - expect(p.size).to.equal(1); + expect(p.pointers()).to.equal(1); }); test('PointerMap .get', function() { - var p = new PointerMap; + var p = new PointerMap(); }); - test('PointerMap .size', function() { - var p = new PointerMap; - expect(p.size).not.to.be.a('function'); - expect(p.size).to.equal(0); + test('PointerMap .pointers', function() { + var p = new PointerMap(); + expect(p.pointers).to.be.a('function'); + expect(p.pointers()).to.equal(0); p.set(1, true); - expect(p.size).to.equal(1); + expect(p.pointers()).to.equal(1); p.set(1, false); - expect(p.size).to.equal(1); + expect(p.pointers()).to.equal(1); }); test('PointerMap .has', function() { - var p = new PointerMap; + var p = new PointerMap(); p.set(1, true); expect(p.has(1)).to.equal(true); expect(p.has(0)).to.equal(false); }); test('PointerMap .delete', function() { - var p = new PointerMap; + var p = new PointerMap(); p.set(1, true); p.set(2, false); - expect(p.size).to.equal(2); + expect(p.pointers()).to.equal(2); p.delete(1); - expect(p.size).to.equal(1); + expect(p.pointers()).to.equal(1); expect(p.get(2)).to.equal(false); }); test('PointerMap .clear', function() { - var p = new PointerMap; + var p = new PointerMap(); p.set(1, true); p.clear(); - expect(p.size).to.equal(0); + expect(p.pointers()).to.equal(0); }); test('PointerMap .forEach', function() { - var p = new PointerMap; + var p = new PointerMap(); p.set(1, true); p.set(2, false); p.set(3, {});