Replace goog.structs.Map with plain object

This commit is contained in:
tsauerwein
2014-03-13 15:02:33 +01:00
parent 8a26ac6e24
commit 51a557efbb
5 changed files with 39 additions and 39 deletions

View File

@@ -30,6 +30,7 @@
goog.provide('ol.pointer.MouseSource');
goog.require('goog.object');
goog.require('ol.pointer.EventSource');
@@ -51,7 +52,7 @@ ol.pointer.MouseSource = function(dispatcher) {
/**
* @const
* @type {goog.structs.Map}
* @type {Object.<string, goog.events.BrowserEvent|Object>}
*/
this.pointerMap = dispatcher.pointerMap;
@@ -160,14 +161,16 @@ ol.pointer.MouseSource.prepareEvent = function(inEvent, dispatcher) {
*/
ol.pointer.MouseSource.prototype.mousedown = function(inEvent) {
if (!this.isEventSimulatedFromTouch_(inEvent)) {
var p = this.pointerMap.containsKey(ol.pointer.MouseSource.POINTER_ID);
var p = goog.object.containsKey(this.pointerMap,
ol.pointer.MouseSource.POINTER_ID.toString());
// TODO(dfreedman) workaround for some elements not sending mouseup
// http://crbug/149091
if (p) {
this.cancel(inEvent);
}
var e = ol.pointer.MouseSource.prepareEvent(inEvent, this.dispatcher);
this.pointerMap.set(ol.pointer.MouseSource.POINTER_ID, inEvent);
goog.object.set(this.pointerMap,
ol.pointer.MouseSource.POINTER_ID.toString(), inEvent);
this.dispatcher.down(e, inEvent);
}
};
@@ -193,7 +196,8 @@ ol.pointer.MouseSource.prototype.mousemove = function(inEvent) {
*/
ol.pointer.MouseSource.prototype.mouseup = function(inEvent) {
if (!this.isEventSimulatedFromTouch_(inEvent)) {
var p = this.pointerMap.get(ol.pointer.MouseSource.POINTER_ID);
var p = goog.object.get(this.pointerMap,
ol.pointer.MouseSource.POINTER_ID.toString());
if (p && p.button === inEvent.button) {
var e = ol.pointer.MouseSource.prepareEvent(inEvent, this.dispatcher);
@@ -246,5 +250,6 @@ ol.pointer.MouseSource.prototype.cancel = function(inEvent) {
* Remove the mouse from the list of active pointers.
*/
ol.pointer.MouseSource.prototype.cleanupMouse = function() {
this.pointerMap.remove(ol.pointer.MouseSource.POINTER_ID);
goog.object.remove(this.pointerMap,
ol.pointer.MouseSource.POINTER_ID.toString());
};

View File

@@ -30,6 +30,7 @@
goog.provide('ol.pointer.MsSource');
goog.require('goog.object');
goog.require('ol.pointer.EventSource');
@@ -54,7 +55,7 @@ ol.pointer.MsSource = function(dispatcher) {
/**
* @const
* @type {goog.structs.Map}
* @type {Object.<string, goog.events.BrowserEvent|Object>}
*/
this.pointerMap = dispatcher.pointerMap;
@@ -97,7 +98,7 @@ ol.pointer.MsSource.prototype.prepareEvent_ = function(inEvent) {
* @param {number} pointerId
*/
ol.pointer.MsSource.prototype.cleanup = function(pointerId) {
this.pointerMap.remove(pointerId);
goog.object.remove(this.pointerMap, pointerId);
};
@@ -107,7 +108,8 @@ ol.pointer.MsSource.prototype.cleanup = function(pointerId) {
* @param {goog.events.BrowserEvent} inEvent
*/
ol.pointer.MsSource.prototype.msPointerDown = function(inEvent) {
this.pointerMap.set(inEvent.getBrowserEvent().pointerId, inEvent);
goog.object.set(this.pointerMap,
inEvent.getBrowserEvent().pointerId, inEvent);
var e = this.prepareEvent_(inEvent);
this.dispatcher.down(e, inEvent);
};

View File

@@ -35,7 +35,6 @@ goog.require('goog.events');
goog.require('goog.events.BrowserEvent');
goog.require('goog.events.Event');
goog.require('goog.events.EventTarget');
goog.require('goog.structs.Map');
goog.require('ol.BrowserFeature');
goog.require('ol.pointer.MouseSource');
@@ -63,9 +62,9 @@ ol.pointer.PointerEventHandler = function(element) {
/**
* @const
* @type {goog.structs.Map}
* @type {Object.<string, goog.events.BrowserEvent|Object>}
*/
this.pointerMap = new goog.structs.Map();
this.pointerMap = {};
/**
* @type {Object.<string, function(goog.events.BrowserEvent)>}

View File

@@ -33,7 +33,9 @@ goog.provide('ol.pointer.TouchSource');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.math.Coordinate');
goog.require('goog.object');
goog.require('ol.pointer.EventSource');
goog.require('ol.pointer.MouseSource');
@@ -54,7 +56,7 @@ ol.pointer.TouchSource = function(dispatcher, mouseSource) {
/**
* @const
* @type {goog.structs.Map}
* @type {Object.<string, goog.events.BrowserEvent|Object>}
*/
this.pointerMap = dispatcher.pointerMap;
@@ -133,8 +135,10 @@ ol.pointer.TouchSource.prototype.isPrimaryTouch_ = function(inTouch) {
* @private
*/
ol.pointer.TouchSource.prototype.setPrimaryTouch_ = function(inTouch) {
if (this.pointerMap.getCount() === 0 ||
(this.pointerMap.getCount() === 1 && this.pointerMap.containsKey(1))) {
if (goog.object.getCount(this.pointerMap) === 0 ||
(goog.object.getCount(this.pointerMap) === 1 &&
goog.object.containsKey(this.pointerMap,
ol.pointer.MouseSource.POINTER_ID.toString()))) {
this.firstTouchId_ = inTouch.identifier;
this.cancelResetClickCount_();
}
@@ -265,13 +269,14 @@ ol.pointer.TouchSource.prototype.vacuumTouches_ = function(inEvent) {
var touchList = inEvent.getBrowserEvent().touches;
// pointerMap.getCount() should be < touchList.length here,
// as the touchstart has not been processed yet.
if (this.pointerMap.getCount() >= touchList.length) {
if (goog.object.getCount(this.pointerMap) >= touchList.length) {
var d = [];
this.forEach_(this.pointerMap, function(value, key) {
goog.object.forEach(this.pointerMap, function(value, key) {
// Never remove pointerId == 1, which is mouse.
// Touch identifiers are 2 smaller than their pointerId, which is the
// index in pointermap.
if (key !== 1 && !this.findTouch_(touchList, key - 2)) {
if (key != ol.pointer.MouseSource.POINTER_ID &&
!this.findTouch_(touchList, key - 2)) {
d.push(value.out);
}
}, this);
@@ -280,19 +285,6 @@ ol.pointer.TouchSource.prototype.vacuumTouches_ = function(inEvent) {
};
/**
* @private
* @param {goog.structs.Map} map
* @param {function(?, ?)} callback
* @param {Object} thisArg
*/
ol.pointer.TouchSource.prototype.forEach_ = function(map, callback, thisArg) {
goog.array.forEach(map.getValues(), function(value) {
callback.call(thisArg, value, map.get(value));
});
};
/**
* Handler for `touchstart`, triggers `pointerover`,
* `pointerenter` and `pointerdown` events.
@@ -314,7 +306,7 @@ ol.pointer.TouchSource.prototype.touchstart = function(inEvent) {
* @param {Object} inPointer
*/
ol.pointer.TouchSource.prototype.overDown_ = function(browserEvent, inPointer) {
this.pointerMap.set(inPointer.pointerId, {
goog.object.set(this.pointerMap, inPointer.pointerId, {
target: inPointer.target,
out: inPointer,
outTarget: inPointer.target
@@ -344,7 +336,7 @@ ol.pointer.TouchSource.prototype.touchmove = function(inEvent) {
ol.pointer.TouchSource.prototype.moveOverOut_ =
function(browserEvent, inPointer) {
var event = inPointer;
var pointer = this.pointerMap.get(event.pointerId);
var pointer = goog.object.get(this.pointerMap, event.pointerId);
// a finger drifted off the screen, ignore it
if (!pointer) {
return;
@@ -427,7 +419,7 @@ ol.pointer.TouchSource.prototype.cancelOut_ =
* @param {Object} inPointer
*/
ol.pointer.TouchSource.prototype.cleanUpPointer_ = function(inPointer) {
this.pointerMap.remove(inPointer.pointerId);
goog.object.remove(this.pointerMap, inPointer.pointerId);
this.removePrimaryPointer_(inPointer);
};

View File

@@ -1,5 +1,7 @@
goog.provide('ol.test.pointer.TouchSource');
goog.require('goog.object');
describe('ol.pointer.TouchSource', function() {
var handler;
var target;
@@ -46,7 +48,7 @@ describe('ol.pointer.TouchSource', function() {
expect(pointerEvent2.clientX).to.be(30);
expect(pointerEvent2.clientY).to.be(45);
expect(handler.pointerMap.getCount()).to.be(2);
expect(goog.object.getCount(handler.pointerMap)).to.be(2);
});
it('creates the right pointer events', function() {
@@ -57,7 +59,7 @@ describe('ol.pointer.TouchSource', function() {
{identifier: 3, clientX: 10, clientY: 11}
]);
expect(eventSpy.calledOnce).to.be.ok();
expect(handler.pointerMap.getCount()).to.be(1);
expect(goog.object.getCount(handler.pointerMap)).to.be(1);
// second touch (first touch still down)
simulateTouchEvent('touchstart', [
@@ -65,7 +67,7 @@ describe('ol.pointer.TouchSource', function() {
], [{identifier: 3}, {identifier: 4}]
);
expect(eventSpy.calledTwice).to.be.ok();
expect(handler.pointerMap.getCount()).to.be(2);
expect(goog.object.getCount(handler.pointerMap)).to.be(2);
// first touch moves
var moveEventSpy = sinon.spy();
@@ -87,7 +89,7 @@ describe('ol.pointer.TouchSource', function() {
], [{identifier: 3}, {identifier: 4}]
);
expect(upEventSpy.calledTwice).to.be.ok();
expect(handler.pointerMap.getCount()).to.be(0);
expect(goog.object.getCount(handler.pointerMap)).to.be(0);
});
it('handles flawed touches', function() {
@@ -98,7 +100,7 @@ describe('ol.pointer.TouchSource', function() {
{identifier: 3, clientX: 10, clientY: 11}
]);
expect(eventSpy.calledOnce).to.be.ok();
expect(handler.pointerMap.getCount()).to.be(1);
expect(goog.object.getCount(handler.pointerMap)).to.be(1);
// second touch, but the first touch has disappeared
var cancelEventSpy = sinon.spy();
@@ -111,7 +113,7 @@ describe('ol.pointer.TouchSource', function() {
// the first (broken) touch is canceled
expect(cancelEventSpy.calledOnce).to.be.ok();
expect(handler.pointerMap.getCount()).to.be(1);
expect(goog.object.getCount(handler.pointerMap)).to.be(1);
});
});