clean up mouse events handling code

This commit is contained in:
Vladimir Agafonkin 2013-12-27 18:36:24 +02:00
parent e7898584c3
commit 93214d7e3c
5 changed files with 32 additions and 61 deletions

View File

@ -45,7 +45,9 @@
populate(); populate();
L.DomUtil.get('populate').onclick = 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('movestart', logEvent);
// map.on('move', logEvent); // map.on('move', logEvent);

View File

@ -170,7 +170,7 @@ L.Canvas = L.Renderer.extend({
for (var id in this._layers) { for (var id in this._layers) {
if (this._layers[id]._containsPoint(point)) { if (this._layers[id]._containsPoint(point)) {
this._layers[id]._onMouseClick(e); this._layers[id]._fireMouseEvent(e);
} }
} }
}, },

View File

@ -69,34 +69,12 @@ L.Path = L.Layer.extend({
return this; return this;
}, },
_onMouseClick: function (e) {
if (this._map.dragging && this._map.dragging.moved()) { return; }
this._fireMouseEvent(e);
},
_fireMouseEvent: function (e, type) { _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') { if (e.type !== 'mousemove') {
L.DomEvent.stopPropagation(e); L.DomEvent.stopPropagation(e);
} }
this._map._fireMouseEvent(this, e, type, true);
}, },
_clickTolerance: function () { _clickTolerance: function () {

View File

@ -143,13 +143,7 @@ L.SVG = L.Renderer.extend({
}, },
_fireMouseEvent: function (e) { _fireMouseEvent: function (e) {
var path = this._paths[L.stamp(e.target)]; this._paths[L.stamp(e.target)]._fireMouseEvent(e);
if (e.type === 'click') {
path._onMouseClick(e);
} else {
path._fireMouseEvent(e);
}
} }
}); });

View File

@ -526,14 +526,11 @@ L.Map = L.Evented.extend({
onOff = onOff || 'on'; 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', for (var i = 0, len = events.length; i < len; i++) {
'mouseleave', 'mousemove', 'contextmenu'], L.DomEvent[onOff](this._container, events[i], this._handleMouseEvent, this);
i, len;
for (i = 0, len = events.length; i < len; i++) {
L.DomEvent[onOff](this._container, events[i], this._fireMouseEvent, this);
} }
if (this.options.trackResize) { if (this.options.trackResize) {
@ -547,39 +544,39 @@ L.Map = L.Evented.extend({
function () { this.invalidateSize({debounceMoveend: true}); }, this, false, this._container); function () { this.invalidateSize({debounceMoveend: true}); }, this, false, this._container);
}, },
_onMouseClick: function (e) { _handleMouseEvent: function (e) {
if (!this._loaded || (!e._simulated && if (!this._loaded) { return; }
((this.dragging && this.dragging.moved()) ||
(this.boxZoom && this.boxZoom.moved()))) ||
L.DomEvent._skipped(e)) { return; }
this.fire('preclick'); this._fireMouseEvent(this, e,
this._fireMouseEvent(e); e.type === 'mouseenter' ? 'mouseover' :
e.type === 'mouseleave' ? 'mouseout' : e.type);
}, },
_fireMouseEvent: function (e) { _fireMouseEvent: function (obj, e, type, propagate) {
if (!this._loaded || L.DomEvent._skipped(e)) { return; } 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') { if (type === 'contextmenu') {
L.DomEvent.preventDefault(e); L.DomEvent.preventDefault(e);
} }
var containerPoint = this.mouseEventToContainerPoint(e), var data = {
layerPoint = this.containerPointToLayerPoint(containerPoint), originalEvent: e,
latlng = this.layerPointToLatLng(layerPoint); containerPoint: this.mouseEventToContainerPoint(e)
};
this.fire(type, { data.layerPoint = this.containerPointToLayerPoint(data.containerPoint);
latlng: latlng, data.latlng = this.layerPointToLatLng(data.layerPoint);
layerPoint: layerPoint,
containerPoint: containerPoint, obj.fire(type, data, propagate);
originalEvent: e
});
}, },
_clearHandlers: function () { _clearHandlers: function () {