[Pkg-javascript-commits] [leaflet-markercluster] 111/128: Test and fix for moving a marker while the MCG is off the map. Refs #753

Jonas Smedegaard dr at jones.dk
Sun Apr 16 06:26:09 UTC 2017


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

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

commit 706d6f266e38ab78aad9cd6d7a86e7ceecda2317
Author: danzel <dave at smartrak.co.nz>
Date:   Tue Jan 31 12:58:35 2017 +1300

    Test and fix for moving a marker while the MCG is off the map. Refs #753
---
 spec/index.html                      |  1 +
 spec/suites/markerMoveSupportSpec.js | 89 ++++++++++++++++++++++++++++++++++++
 src/MarkerClusterGroup.js            | 26 ++++++++---
 3 files changed, 109 insertions(+), 7 deletions(-)

diff --git a/spec/index.html b/spec/index.html
index 65924b5..fcf995d 100644
--- a/spec/index.html
+++ b/spec/index.html
@@ -43,6 +43,7 @@
 	<script type="text/javascript" src="suites/singleMarkerModeSpec.js"></script>
 
 	<script type="text/javascript" src="suites/ChildChangingIconSupportSpec.js"></script>
+	<script type="text/javascript" src="suites/markerMoveSupportSpec.js"></script>
 
 	<script type="text/javascript" src="suites/CircleMarkerSupportSpec.js"></script>
 	<script type="text/javascript" src="suites/CircleSupportSpec.js"></script>
diff --git a/spec/suites/markerMoveSupportSpec.js b/spec/suites/markerMoveSupportSpec.js
new file mode 100644
index 0000000..9497f98
--- /dev/null
+++ b/spec/suites/markerMoveSupportSpec.js
@@ -0,0 +1,89 @@
+describe('moving markers', function () {
+
+	/**
+	 * Avoid as much as possible creating and destroying objects for each test.
+	 * Instead, try re-using them, except for the ones under test of course.
+	 * PhantomJS does not perform garbage collection for the life of the page,
+	 * i.e. during the entire test process (Karma runs all tests in a single page).
+	 * http://stackoverflow.com/questions/27239708/how-to-get-around-memory-error-with-karma-phantomjs
+	 *
+	 * The `beforeEach` and `afterEach do not seem to cause much issue.
+	 * => they can still be used to initialize some setup between each test.
+	 * Using them keeps a readable spec/index.
+	 *
+	 * But refrain from re-creating div and map every time. Re-use those objects.
+	 */
+
+	/////////////////////////////
+	// SETUP FOR EACH TEST
+	/////////////////////////////
+
+	beforeEach(function () {
+
+		clock = sinon.useFakeTimers();
+
+	});
+
+	afterEach(function () {
+
+		if (group instanceof L.MarkerClusterGroup) {
+			group.clearLayers();
+			map.removeLayer(group);
+		}
+
+		// Throw away group as it can be assigned with different configurations between tests.
+		group = null;
+
+		clock.restore();
+
+	});
+
+
+	/////////////////////////////
+	// PREPARATION CODE
+	/////////////////////////////
+
+	var div, map, group, clock;
+
+	div = document.createElement('div');
+	div.style.width = '200px';
+	div.style.height = '200px';
+	document.body.appendChild(div);
+
+	map = L.map(div, { maxZoom: 18 });
+
+	// Corresponds to zoom level 8 for the above div dimensions.
+	map.fitBounds(new L.LatLngBounds([
+		[1, 1],
+		[2, 2]
+	]));
+
+
+	/////////////////////////////
+	// TESTS
+	/////////////////////////////
+
+	it('moves a marker that was moved while off the map', function () {
+
+		group = new L.MarkerClusterGroup();
+
+		var marker = new L.Marker([10, 10]);
+		map.addLayer(group);
+		group.addLayer(marker);
+
+		map.removeLayer(group);
+		marker.setLatLng([1.5, 1.5]);
+		map.addLayer(group);
+
+		expect(group.getLayers().length).to.be(1);
+	});
+
+
+	/////////////////////////////
+	// CLEAN UP CODE
+	/////////////////////////////
+
+	map.remove();
+	document.body.removeChild(div);
+
+});
diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js
index 18111c7..3046fa4 100644
--- a/src/MarkerClusterGroup.js
+++ b/src/MarkerClusterGroup.js
@@ -149,7 +149,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 
 		if (!this._map) {
 			if (!this._arraySplice(this._needsClustering, layer) && this.hasLayer(layer)) {
-				this._needsRemoving.push(layer);
+				this._needsRemoving.push({ layer: layer, latlng: layer._latlng });
 			}
 			this.fire('layerremove', { layer: layer });
 			return this;
@@ -339,7 +339,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 				this._arraySplice(this._needsClustering, m);
 				npg.removeLayer(m);
 				if (this.hasLayer(m)) {
-					this._needsRemoving.push(m);
+					this._needsRemoving.push({ layer: m, latlng: m._latlng });
 				}
 				this.fire('layerremove', { layer: m });
 			}
@@ -461,14 +461,23 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 	eachLayer: function (method, context) {
 		var markers = this._needsClustering.slice(),
 			needsRemoving = this._needsRemoving,
-			i;
+			thisNeedsRemoving, i, j;
 
 		if (this._topClusterLevel) {
 			this._topClusterLevel.getAllChildMarkers(markers);
 		}
 
 		for (i = markers.length - 1; i >= 0; i--) {
-			if (needsRemoving.indexOf(markers[i]) === -1) {
+			thisNeedsRemoving = true;
+
+			for (j = needsRemoving.length - 1; j >= 0; j--) {
+				if (needsRemoving[j].layer === markers[i]) {
+					thisNeedsRemoving = false;
+					break;
+				}
+			}
+
+			if (thisNeedsRemoving) {
 				method.call(context, markers[i]);
 			}
 		}
@@ -516,7 +525,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 
 		anArray = this._needsRemoving;
 		for (i = anArray.length - 1; i >= 0; i--) {
-			if (anArray[i] === layer) {
+			if (anArray[i].layer === layer) {
 				return false;
 			}
 		}
@@ -562,7 +571,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 	//Overrides FeatureGroup.onAdd
 	onAdd: function (map) {
 		this._map = map;
-		var i, l, layer;
+		var i, l, layer, latlngbk;
 
 		if (!isFinite(this._map.getMaxZoom())) {
 			throw "Map has no maxZoom specified";
@@ -579,7 +588,10 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 
 		for (i = 0, l = this._needsRemoving.length; i < l; i++) {
 			layer = this._needsRemoving[i];
-			this._removeLayer(layer, true);
+			latlngbk = layer.layer._latlng;
+			layer.layer._latlng = layer.latlng;
+			this._removeLayer(layer.layer, true);
+			layer.layer._latlng = latlngbk;
 		}
 		this._needsRemoving = [];
 

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