[Pkg-javascript-commits] [leaflet-markercluster] 433/479: Fix getVisibleParent when no parent is visible. Add tests for getVisibleParent. Fixes #265

Jonas Smedegaard dr at jones.dk
Thu Oct 16 16:01:04 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 df054cf884ba3181794b307d4a9d9b8538e9d99a
Author: danzel <danzel at localhost.geek.nz>
Date:   Wed Oct 23 16:58:12 2013 +1300

    Fix getVisibleParent when no parent is visible. Add tests for getVisibleParent. Fixes #265
---
 README.md                           |  1 +
 spec/index.html                     |  1 +
 spec/suites/getVisibleParentSpec.js | 59 +++++++++++++++++++++++++++++++++++++
 src/MarkerClusterGroup.js           |  4 +--
 4 files changed, 63 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 4390396..612e741 100644
--- a/README.md
+++ b/README.md
@@ -105,6 +105,7 @@ markers.on('clusterclick', function (a) {
 
 ### Getting the visible parent of a marker
 If you have a marker in your MarkerClusterGroup and you want to get the visible parent of it (Either itself or a cluster it is contained in that is currently visible on the map).
+This will return null if the marker and its parent clusters are not visible currently (they are not near the visible viewpoint)
 ```
 var visibleOne = markerClusterGroup.getVisibleParent(myMarker);
 console.log(visibleOne.getLatLng());
diff --git a/spec/index.html b/spec/index.html
index 6b00aae..42ad45b 100644
--- a/spec/index.html
+++ b/spec/index.html
@@ -47,6 +47,7 @@
 	<script type="text/javascript" src="suites/eventsSpec.js"></script>
 	<script type="text/javascript" src="suites/getBoundsSpec.js"></script>
 	<script type="text/javascript" src="suites/getLayersSpec.js"></script>
+	<script type="text/javascript" src="suites/getVisibleParentSpec.js"></script>
 
 	<script type="text/javascript" src="suites/NonPointSpec.js"></script>
 
diff --git a/spec/suites/getVisibleParentSpec.js b/spec/suites/getVisibleParentSpec.js
new file mode 100644
index 0000000..7318184
--- /dev/null
+++ b/spec/suites/getVisibleParentSpec.js
@@ -0,0 +1,59 @@
+describe('getVisibleParent', function () {
+	var map, div;
+	beforeEach(function () {
+		div = document.createElement('div');
+		div.style.width = '200px';
+		div.style.height = '200px';
+		document.body.appendChild(div);
+
+		map = L.map(div, { maxZoom: 18 });
+
+		map.fitBounds(new L.LatLngBounds([
+			[1, 1],
+			[2, 2]
+		]));
+	});
+	afterEach(function () {
+		document.body.removeChild(div);
+	});
+
+	it('gets the marker if the marker is visible', function () {
+		var group = new L.MarkerClusterGroup();
+		var marker = new L.Marker([1.5, 1.5]);
+
+		group.addLayer(marker);
+		map.addLayer(group);
+
+		var vp = group.getVisibleParent(marker);
+
+		expect(vp).to.be(marker);
+	});
+
+	it('gets the visible cluster if it is clustered', function () {
+		var group = new L.MarkerClusterGroup();
+		var marker = new L.Marker([1.5, 1.5]);
+		var marker2 = new L.Marker([1.5, 1.5]);
+
+		group.addLayers([marker, marker2]);
+		map.addLayer(group);
+
+		var vp = group.getVisibleParent(marker);
+
+		expect(vp).to.be.a(L.MarkerCluster);
+		expect(vp._icon).to.not.be(null);
+		expect(vp._icon).to.not.be(undefined);
+	});
+
+	it('returns null if the marker and parents are all not visible', function () {
+		var group = new L.MarkerClusterGroup();
+		var marker = new L.Marker([5.5, 1.5]);
+		var marker2 = new L.Marker([5.5, 1.5]);
+
+		group.addLayers([marker, marker2]);
+		map.addLayer(group);
+
+		var vp = group.getVisibleParent(marker);
+
+		expect(vp).to.be(null);
+	});
+});
\ No newline at end of file
diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js
index 896d855..5196ecb 100644
--- a/src/MarkerClusterGroup.js
+++ b/src/MarkerClusterGroup.js
@@ -467,10 +467,10 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 
 	getVisibleParent: function (marker) {
 		var vMarker = marker;
-		while (vMarker !== null && !vMarker._icon) {
+		while (vMarker && !vMarker._icon) {
 			vMarker = vMarker.__parent;
 		}
-		return vMarker;
+		return vMarker || null;
 	},
 
 	//Remove the given object from the given array

-- 
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