[Pkg-javascript-commits] [leaflet-markercluster] 226/479: Use events rather than hacky setTimeouts to make zoomToShowLayer more reliable

Jonas Smedegaard dr at jones.dk
Thu Oct 16 16:00:33 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository leaflet-markercluster.

commit 07a11582d3e9f83602a816b225ec54c09f38c953
Author: danzel <danzel at localhost.geek.nz>
Date:   Wed Sep 12 11:13:02 2012 +1200

    Use events rather than hacky setTimeouts to make zoomToShowLayer more reliable
---
 src/MarkerCluster.Spiderfier.js |  2 ++
 src/MarkerClusterGroup.js       | 36 +++++++++++++++++++++++++++---------
 2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/src/MarkerCluster.Spiderfier.js b/src/MarkerCluster.Spiderfier.js
index 5a69f8c..28b1f05 100644
--- a/src/MarkerCluster.Spiderfier.js
+++ b/src/MarkerCluster.Spiderfier.js
@@ -107,6 +107,7 @@ L.MarkerCluster.include(!L.DomUtil.TRANSITION ? {
 			m._spiderLeg = leg;
 		}
 		this.setOpacity(0.3);
+		group.fire('spiderfied');
 	},
 
 	_animationUnspiderfy: function () {
@@ -223,6 +224,7 @@ L.MarkerCluster.include(!L.DomUtil.TRANSITION ? {
 
 		setTimeout(function () {
 			group._animationEnd();
+			group.fire('spiderfied');
 		}, 250);
 	},
 
diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js
index e7904d6..fde1ab3 100644
--- a/src/MarkerClusterGroup.js
+++ b/src/MarkerClusterGroup.js
@@ -149,17 +149,34 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 	},
 
 	zoomToShowLayer: function (layer, callback) {
-		layer.__parent.zoomToBounds();
-		setTimeout(function () {
-			if (layer._icon) {
-				callback();
-			} else {
-				layer.__parent.spiderfy();
-				setTimeout(function () {
+
+		var showMarker = function () {
+			if ((layer._icon || layer.__parent._icon) && !this._inZoomAnimation) {
+				this._map.off('moveend', showMarker, this);
+				this.off('animationend', showMarker, this);
+
+				if (layer._icon) {
 					callback();
-				}, L.DomUtil.TRANSITION ? 250 : 0); //TODO: This is hardcoded based on the time to spiderfy
+				} else if (layer.__parent._icon) {
+					var afterSpiderfy = function () {
+						this.off('spiderfied', afterSpiderfy, this);
+						callback();
+					};
+
+					this.on('spiderfied', afterSpiderfy, this);
+					layer.__parent.spiderfy();
+				}
 			}
-		}, L.DomUtil.TRANSITION ? 600 : 0); //TODO: This is hardcoded based on the leaflet time to zoom
+		};
+
+		if ((layer._icon || layer.__parent._icon) && this._map.getBounds().contains(layer.__parent._latlng)) {
+			//Layer or cluster is already visible
+			showMarker.call(this);
+		} else {
+			this._map.on('moveend', showMarker, this);
+			this.on('animationend', showMarker, this);
+			layer.__parent.zoomToBounds();
+		}
 	},
 
 	//Overrides FeatureGroup.onAdd
@@ -533,6 +550,7 @@ L.MarkerClusterGroup.include(!L.DomUtil.TRANSITION ? {
 			this._map._mapPane.className = this._map._mapPane.className.replace(' leaflet-cluster-anim', '');
 		}
 		this._inZoomAnimation--;
+		this.fire('animationend');
 	},
 	_animationZoomIn: function (previousZoomLevel, newZoomLevel) {
 		var me = this,

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/leaflet-markercluster.git



More information about the Pkg-javascript-commits mailing list