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

Commit

Permalink
Allow for use in old IE by providing a non-getter API for size of poi…
Browse files Browse the repository at this point in the history
…ntermap
  • Loading branch information
dfreedm committed Oct 29, 2013
1 parent 47409b8 commit c1d7830
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 59 deletions.
67 changes: 30 additions & 37 deletions src/pointermap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
6 changes: 3 additions & 3 deletions src/touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
38 changes: 19 additions & 19 deletions tests/pointermap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ suite('PointerMap', function() {
'get',
'has',
'delete',
'size',
'pointers',
'clear',
'forEach'
];
Expand All @@ -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, {});
Expand Down

0 comments on commit c1d7830

Please sign in to comment.