[Pkg-javascript-commits] [leaflet-markercluster] 126/219: Corrected effect of removeOutsideVisibleBounds option

Jonas Smedegaard dr at jones.dk
Sat May 7 09:39:23 UTC 2016


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

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

commit f8a1ff926c0753f62505cba9baaa3346d264113f
Author: ghybs <ghybs1 at gmail.com>
Date:   Wed Oct 14 16:48:50 2015 +0400

    Corrected effect of removeOutsideVisibleBounds option
    
    Corrected effect of `removeOutsideVisibleBounds` option following issue #573.
    Method `L.MarkerClusterGroup._getExpandedVisibleBounds` returned incorrect
    bounds when this option was set to false.
    Also simplified the rest of the method: directly returning the visible
    bounds for mobile devices, and expanding bounds using `L.LatLngBounds.pad`
    method instead of doing the same thing manually.
---
 spec/index.html                               |   1 +
 spec/suites/removeOutsideVisibleBoundsSpec.js | 104 ++++++++++++++++++++++++++
 src/MarkerClusterGroup.js                     |  18 ++---
 3 files changed, 113 insertions(+), 10 deletions(-)

diff --git a/spec/index.html b/spec/index.html
index a6fb5e5..403c44b 100644
--- a/spec/index.html
+++ b/spec/index.html
@@ -61,6 +61,7 @@
 	<script type="text/javascript" src="suites/RememberOpacity.js"></script>
 
 	<script type="text/javascript" src="suites/RefreshSpec.js"></script>
+	<script type="text/javascript" src="suites/removeOutsideVisibleBoundsSpec.js"></script>
 
 	<script>
 		(window.mochaPhantomJS || window.mocha).run();
diff --git a/spec/suites/removeOutsideVisibleBoundsSpec.js b/spec/suites/removeOutsideVisibleBoundsSpec.js
new file mode 100644
index 0000000..be97928
--- /dev/null
+++ b/spec/suites/removeOutsideVisibleBoundsSpec.js
@@ -0,0 +1,104 @@
+describe('Option removeOutsideVisibleBounds', function () {
+	var map, div;
+
+	/*var marker1 = new L.Marker([1.5, -0.4]), // 2 screens width away.
+	    marker2 = new L.Marker([1.5, 0.6]), // 1 screen width away.
+	    marker3 = new L.Marker([1.5, 1.5]), // In view port.
+	    marker4 = new L.Marker([1.5, 2.4]), // 1 screen width away.
+	    marker5 = new L.Marker([1.5, 3.4]), // 2 screens width away.
+	    markers = [marker1, marker2, marker3, marker4, marker5],
+		group,
+		previousMobileValue = L.Browser.mobile;*/
+
+	function centerMapView() {
+		// Corresponds to zoom level 8 for the above div dimensions.
+		map.fitBounds(new L.LatLngBounds([
+			[1, 1],
+			[2, 2]
+		]));
+	}
+
+	beforeEach(function () {
+		clock = sinon.useFakeTimers();
+
+		div = document.createElement('div');
+		div.style.width = '200px';
+		div.style.height = '200px';
+		document.body.appendChild(div);
+
+		map = L.map(div, { maxZoom: 18 });
+	});
+
+	afterEach(function () {
+		clock.restore();
+
+		document.body.removeChild(div);
+	});
+
+	it('removes objects more than 1 screen away from view port by default', function () {
+
+		centerMapView();
+
+		var marker1 = new L.Marker([1.5, -0.4]), // 2 screens width away.
+		    marker2 = new L.Marker([1.5, 0.6]), // 1 screen width away.
+		    marker3 = new L.Marker([1.5, 1.5]), // In view port.
+		    marker4 = new L.Marker([1.5, 2.4]), // 1 screen width away.
+		    marker5 = new L.Marker([1.5, 3.4]), // 2 screens width away.
+		    markers = [marker1, marker2, marker3, marker4, marker5],
+		    group = L.markerClusterGroup().addTo(map);
+
+		group.addLayers(markers);
+
+		expect(marker1._icon).to.be(undefined);
+		expect(map._panes.markerPane.childNodes.length).to.be(3); // markers 2, 3 and 4.
+		expect(marker5._icon).to.be(undefined);
+
+	});
+
+	it('removes objects out of view port by default for mobile device', function () {
+
+		// Fool Leaflet
+		var previous = L.Browser.mobile;
+		L.Browser.mobile = true;
+
+		centerMapView();
+
+		var marker1 = new L.Marker([1.5, -0.4]), // 2 screens width away.
+		    marker2 = new L.Marker([1.5, 0.6]), // 1 screen width away.
+		    marker3 = new L.Marker([1.5, 1.5]), // In view port.
+		    marker4 = new L.Marker([1.5, 2.4]), // 1 screen width away.
+		    marker5 = new L.Marker([1.5, 3.4]), // 2 screens width away.
+		    markers = [marker1, marker2, marker3, marker4, marker5],
+		    group = L.markerClusterGroup().addTo(map);
+
+		group.addLayers(markers);
+
+		expect(marker1._icon).to.be(undefined);
+		expect(marker2._icon).to.be(undefined);
+		expect(map._panes.markerPane.childNodes.length).to.be(1); // marker 3 only.
+		expect(marker4._icon).to.be(undefined);
+		expect(marker5._icon).to.be(undefined);
+
+		L.Browser.mobile = previous;
+
+	});
+
+	it('leaves all objects on map when set to false', function () {
+
+		centerMapView();
+
+		var marker1 = new L.Marker([1.5, -0.4]), // 2 screens width away.
+		    marker2 = new L.Marker([1.5, 0.6]), // 1 screen width away.
+		    marker3 = new L.Marker([1.5, 1.5]), // In view port.
+		    marker4 = new L.Marker([1.5, 2.4]), // 1 screen width away.
+		    marker5 = new L.Marker([1.5, 3.4]), // 2 screens width away.
+		    markers = [marker1, marker2, marker3, marker4, marker5],
+		    group = L.markerClusterGroup({removeOutsideVisibleBounds: false}).addTo(map);
+
+		group.addLayers(markers);
+
+		expect(map._panes.markerPane.childNodes.length).to.be(5); // All 5 markers.
+
+	});
+
+});
diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js
index e6245f6..37f080f 100644
--- a/src/MarkerClusterGroup.js
+++ b/src/MarkerClusterGroup.js
@@ -888,19 +888,12 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 	//Gets the maps visible bounds expanded in each direction by the size of the screen (so the user cannot see an area we do not cover in one pan)
 	_getExpandedVisibleBounds: function () {
 		if (!this.options.removeOutsideVisibleBounds) {
+			return this._mapBoundsInfinite;
+		} else if (L.Browser.mobile) {
 			return this._map.getBounds();
 		}
 
-		var map = this._map,
-			bounds = map.getBounds(),
-			sw = bounds._southWest,
-			ne = bounds._northEast,
-			latDiff = L.Browser.mobile ? 0 : Math.abs(sw.lat - ne.lat),
-			lngDiff = L.Browser.mobile ? 0 : Math.abs(sw.lng - ne.lng);
-
-		return new L.LatLngBounds(
-			new L.LatLng(sw.lat - latDiff, sw.lng - lngDiff, true),
-			new L.LatLng(ne.lat + latDiff, ne.lng + lngDiff, true));
+		return this._map.getBounds().pad(1); // Padding expands the bounds by its own dimensions but scaled with the given factor.
 	},
 
 	//Shared animation code
@@ -919,6 +912,11 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 	}
 });
 
+// Constant bounds used in case option "removeOutsideVisibleBounds" is set to false.
+L.MarkerClusterGroup.include({
+	_mapBoundsInfinite: new L.LatLngBounds(new L.LatLng(-Infinity, -Infinity), new L.LatLng(Infinity, Infinity))
+});
+
 L.MarkerClusterGroup.include(!L.DomUtil.TRANSITION ? {
 
 	//Non Animated versions of everything

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