From 93214d7e3c91f6ff00d704e4498841547449f522 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Fri, 27 Dec 2013 18:36:24 +0200 Subject: [PATCH] clean up mouse events handling code --- debug/map/map.html | 4 ++- src/layer/vector/Canvas.js | 2 +- src/layer/vector/Path.js | 26 ++----------------- src/layer/vector/SVG.js | 8 +----- src/map/Map.js | 53 ++++++++++++++++++-------------------- 5 files changed, 32 insertions(+), 61 deletions(-) diff --git a/debug/map/map.html b/debug/map/map.html index 6941ffd2..60544a76 100644 --- a/debug/map/map.html +++ b/debug/map/map.html @@ -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); diff --git a/src/layer/vector/Canvas.js b/src/layer/vector/Canvas.js index 3066c262..60ab295b 100644 --- a/src/layer/vector/Canvas.js +++ b/src/layer/vector/Canvas.js @@ -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); } } }, diff --git a/src/layer/vector/Path.js b/src/layer/vector/Path.js index e25f86b3..8b0b6587 100644 --- a/src/layer/vector/Path.js +++ b/src/layer/vector/Path.js @@ -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 () { diff --git a/src/layer/vector/SVG.js b/src/layer/vector/SVG.js index f7bd430c..633695b7 100644 --- a/src/layer/vector/SVG.js +++ b/src/layer/vector/SVG.js @@ -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); } }); diff --git a/src/map/Map.js b/src/map/Map.js index 6bda864c..1b01a293 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -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) { @@ -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 () {