Skip to content

Commit

Permalink
clean up mouse events handling code
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Dec 27, 2013
1 parent e789858 commit 93214d7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 61 deletions.
4 changes: 3 additions & 1 deletion debug/map/map.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
populate();
L.DomUtil.get('populate').onclick = populate;

// function logEvent(e) { console.log(e.type); }
function logEvent(e) { console.log(e.type); }

map.on('click', logEvent);
//
// map.on('movestart', logEvent);
// map.on('move', logEvent);
Expand Down
2 changes: 1 addition & 1 deletion src/layer/vector/Canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ L.Canvas = L.Renderer.extend({

for (var id in this._layers) {
if (this._layers[id]._containsPoint(point)) {
this._layers[id]._onMouseClick(e);
this._layers[id]._fireMouseEvent(e);
}
}
},
Expand Down
26 changes: 2 additions & 24 deletions src/layer/vector/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,12 @@ L.Path = L.Layer.extend({
return this;
},

_onMouseClick: function (e) {
if (this._map.dragging && this._map.dragging.moved()) { return; }
this._fireMouseEvent(e);
},

_fireMouseEvent: function (e, type) {
type = type || e.type;

if (!this.listens(type, true)) { return; }

var map = this._map,
containerPoint = map.mouseEventToContainerPoint(e),
layerPoint = map.containerPointToLayerPoint(containerPoint),
latlng = map.layerPointToLatLng(layerPoint);

this.fire(type, {
latlng: latlng,
layerPoint: layerPoint,
containerPoint: containerPoint,
originalEvent: e
}, true);

if (type === 'contextmenu') {
L.DomEvent.preventDefault(e);
}
if (e.type !== 'mousemove') {
L.DomEvent.stopPropagation(e);
}

this._map._fireMouseEvent(this, e, type, true);
},

_clickTolerance: function () {
Expand Down
8 changes: 1 addition & 7 deletions src/layer/vector/SVG.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,7 @@ L.SVG = L.Renderer.extend({
},

_fireMouseEvent: function (e) {
var path = this._paths[L.stamp(e.target)];

if (e.type === 'click') {
path._onMouseClick(e);
} else {
path._fireMouseEvent(e);
}
this._paths[L.stamp(e.target)]._fireMouseEvent(e);
}
});

Expand Down
53 changes: 25 additions & 28 deletions src/map/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,14 +526,11 @@ L.Map = L.Evented.extend({

onOff = onOff || 'on';

L.DomEvent[onOff](this._container, 'click', this._onMouseClick, this);
var events = ['click', 'dblclick', 'mousedown', 'mouseup',
'mouseenter', 'mouseleave', 'mousemove', 'contextmenu'];

var events = ['dblclick', 'mousedown', 'mouseup', 'mouseenter',
'mouseleave', 'mousemove', 'contextmenu'],
i, len;

for (i = 0, len = events.length; i < len; i++) {
L.DomEvent[onOff](this._container, events[i], this._fireMouseEvent, this);
for (var i = 0, len = events.length; i < len; i++) {
L.DomEvent[onOff](this._container, events[i], this._handleMouseEvent, this);
}

if (this.options.trackResize) {
Expand All @@ -547,39 +544,39 @@ L.Map = L.Evented.extend({
function () { this.invalidateSize({debounceMoveend: true}); }, this, false, this._container);
},

_onMouseClick: function (e) {
if (!this._loaded || (!e._simulated &&
((this.dragging && this.dragging.moved()) ||
(this.boxZoom && this.boxZoom.moved()))) ||
L.DomEvent._skipped(e)) { return; }
_handleMouseEvent: function (e) {
if (!this._loaded) { return; }

this.fire('preclick');
this._fireMouseEvent(e);
this._fireMouseEvent(this, e,
e.type === 'mouseenter' ? 'mouseover' :
e.type === 'mouseleave' ? 'mouseout' : e.type);
},

_fireMouseEvent: function (e) {
if (!this._loaded || L.DomEvent._skipped(e)) { return; }
_fireMouseEvent: function (obj, e, type, propagate) {
type = type || e.type;

var type = e.type;
if (!obj.listens(type, propagate) || L.DomEvent._skipped(e)) { return; }

type = (type === 'mouseenter' ? 'mouseover' : (type === 'mouseleave' ? 'mouseout' : type));
if (type === 'click') {
if (!e._simulated && ((this.dragging && this.dragging.moved()) ||
(this.boxZoom && this.boxZoom.moved()))) { return; }

if (!this.listens(type)) { return; }
obj.fire('preclick');
}

if (type === 'contextmenu') {
L.DomEvent.preventDefault(e);
}

var containerPoint = this.mouseEventToContainerPoint(e),
layerPoint = this.containerPointToLayerPoint(containerPoint),
latlng = this.layerPointToLatLng(layerPoint);
var data = {
originalEvent: e,
containerPoint: this.mouseEventToContainerPoint(e)
};

this.fire(type, {
latlng: latlng,
layerPoint: layerPoint,
containerPoint: containerPoint,
originalEvent: e
});
data.layerPoint = this.containerPointToLayerPoint(data.containerPoint);
data.latlng = this.layerPointToLatLng(data.layerPoint);

obj.fire(type, data, propagate);
},

_clearHandlers: function () {
Expand Down

0 comments on commit 93214d7

Please sign in to comment.