Skip to content

Commit

Permalink
Supporting handler removal by saving references to bound fns.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Sep 28, 2011
1 parent b063ce5 commit b70e476
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,9 @@
// and clear its memory usage.
destroy: function() {
this.requestManager.clear();
for (var i = 0; i < this.eventHandlers.length; i++) {
this.eventHandlers[i].remove();
}
MM.removeEvent(window, 'resize', this.windowResize());
return this;
}
Expand Down
34 changes: 28 additions & 6 deletions src/mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@

init: function(map) {
this.map = map;
MM.addEvent(map.parent, 'mousewheel', MM.bind(this.mouseWheel, this));
this._mouseWheel = MM.bind(this.mouseWheel, this);
MM.addEvent(map.parent, 'mousewheel', this._mouseWheel);
},

remove: function() {
MM.removeEvent(this.map.parent, 'mousewheel', this._mouseWheel);
},

mouseWheel: function(e) {
Expand Down Expand Up @@ -68,7 +73,12 @@

init: function(map) {
this.map = map;
MM.addEvent(map.parent, 'dblclick', this._doubleClick = MM.bind(this.doubleClick, this));
this._doubleClick = MM.bind(this.doubleClick, this);
MM.addEvent(map.parent, 'dblclick', this._doubleClick);
},

remove: function() {
MM.removeEvent(this.map.parent, 'dblclick', this._doubleClick);
},

doubleClick: function(e) {
Expand All @@ -94,7 +104,12 @@

init: function(map) {
this.map = map;
MM.addEvent(map.parent, 'mousedown', MM.bind(this.mouseDown, this));
this._mouseDown = MM.bind(this.mouseDown, this);
MM.addEvent(map.parent, 'mousedown', this._mouseDown);
},

remove: function() {
MM.removeEvent(this.map.parent, 'mousedown', this._mouseDown);
},

mouseDown: function(e) {
Expand Down Expand Up @@ -143,8 +158,15 @@
MM.MouseHandler.prototype = {
init: function(map) {
this.map = map;
new MM.DragHandler(map);
new MM.DoubleClickHandler(map);
new MM.MouseWheelHandler(map);
this.handlers = [
new MM.DragHandler(map),
new MM.DoubleClickHandler(map),
new MM.MouseWheelHandler(map)
];
},
remove: function() {
for (var i = 0; i < this.handlers.length; i++) {
this.handlers[i].remove();
}
}
};
51 changes: 29 additions & 22 deletions src/touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,30 @@
init: function(map, options) {
this.map = map;
options = options || {};

this._touchStartMachine = MM.bind(this.touchStartMachine, this);
this._touchMoveMachine = MM.bind(this.touchMoveMachine, this);
this._touchEndMachine = MM.bind(this.touchEndMachine, this);
MM.addEvent(map.parent, 'touchstart',
MM.bind(this.touchStartMachine, this));
this._touchStartMachine);
MM.addEvent(map.parent, 'touchmove',
MM.bind(this.touchMoveMachine, this));
this._touchMoveMachine);
MM.addEvent(map.parent, 'touchend',
MM.bind(this.touchEndMachine, this));
this._touchEndMachine);

this.options = {};
this.options.snapToZoom = options.snapToZoom || true;
},

remove: function() {
MM.removeEvent(this.map.parent, 'touchstart',
this._touchStartMachine);
MM.removeEvent(this.map.parent, 'touchmove',
this._touchMoveMachine);
MM.removeEvent(this.map.parent, 'touchend',
this._touchEndMachine);
},

updateTouches: function(e) {
for (var i = 0; i < e.touches.length; i += 1) {
var t = e.touches[i];
Expand All @@ -37,7 +50,7 @@
this.locations[t.identifier] = {
scale: e.scale,
startPos: { x: t.screenX, y: t.screenY },
x: t.screenX,
x: t.screenX,
y: t.screenY,
time: new Date().getTime()
};
Expand Down Expand Up @@ -71,19 +84,16 @@
},

touchEndMachine: function(e) {

var now = new Date().getTime();

// round zoom if we're done pinching
if (e.touches.length == 0 && this.wasPinching) {
// round zoom if we're done pinching
if (e.touches.length === 0 && this.wasPinching) {
this.onPinched(this.lastPinchCenter);
}

// Look at each changed touch in turn.
for (var i = 0; i < e.changedTouches.length; i += 1) {
var t = e.changedTouches[i],
loc = this.locations[t.identifier];

// if we didn't see this one (bug?)
// or if it was consumed by pinching already
// just skip to the next one
Expand All @@ -95,7 +105,7 @@
// matching touch that's just ended. Let's see
// what kind of event it is based on how long it
// lasted and how far it moved.
var pos = { x: t.screenX, y: t.screenY },
var pos = { x: t.screenX, y: t.screenY },
time = now - loc.time,
travel = MM.Point.distance(pos, loc.startPos);
if (travel > this.maxTapDistance) {
Expand All @@ -115,7 +125,7 @@
// Weird, sometimes an end event doesn't get thrown
// for a touch that nevertheless has disappeared.
// Still, this will eventually catch those ids:

var validTouchIds = {};
for (var j = 0; j < e.touches.length; j++) {
validTouchIds[e.touches[j].identifier] = true;
Expand All @@ -125,7 +135,7 @@
delete validTouchIds[id];
}
}

return MM.cancelEvent(e);
},

Expand All @@ -151,21 +161,19 @@
var z = this.map.getZoom(), // current zoom
tz = Math.round(z) + 1, // target zoom
dz = tz - z; // desired delate

// zoom in to a round number
var p = new MM.Point(tap.x, tap.y);
this.map.zoomByAbout(dz, p);
},

// Re-transform the actual map parent's CSS transformation
onPanning: function(touch) {
var pos = { x: touch.screenX, y: touch.screenY },
var pos = { x: touch.screenX, y: touch.screenY },
prev = this.locations[touch.identifier];
this.map.panBy(pos.x - prev.x, pos.y - prev.y);
},

onPinching: function(e) {

// use the first two touches and their previous positions
var t0 = e.touches[0],
t1 = e.touches[1],
Expand All @@ -177,10 +185,10 @@
// mark these touches so they aren't used as taps/holds
l0.wasPinch = true;
l1.wasPinch = true;

// scale about the center of these touches
var center = MM.Point.interpolate(p0, p1, 0.5);

this.map.zoomByAbout(
Math.log(e.scale) / Math.LN2 -
Math.log(l0.scale) / Math.LN2,
Expand All @@ -189,10 +197,9 @@
// pan from the previous center of these touches
var prevCenter = MM.Point.interpolate(l0, l1, 0.5);

this.map.panBy( center.x - prevCenter.x,
center.y - prevCenter.y );

this.wasPinching = true;
this.map.panBy(center.x - prevCenter.x,
center.y - prevCenter.y);
this.wasPinching = true;
this.lastPinchCenter = center;
},

Expand All @@ -206,4 +213,4 @@
}
this.wasPinching = false;
}
};
};

0 comments on commit b70e476

Please sign in to comment.