[Pkg-javascript-commits] [leaflet-markercluster] 42/128: Clone input array at first group met

Jonas Smedegaard dr at jones.dk
Sun Apr 16 06:26:02 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 33e5eec4f9ae716d6db60fc19c8712d33f7bcece
Author: ghybs <ghybs1 at gmail.com>
Date:   Mon Jan 18 15:44:45 2016 +0400

    Clone input array at first group met
    
    in `addLayers` and `removeLayers`, so that when there are no groups, behaviour is the same (no cloning occurs), and when groups are passed, we work on a separate array to be modified, and the input array is unmodified.
---
 README.md                 |  3 +--
 src/MarkerClusterGroup.js | 24 +++++++++++++++++++++---
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/README.md b/README.md
index cf00c2c..931d9df 100644
--- a/README.md
+++ b/README.md
@@ -159,9 +159,8 @@ markers.on('clusterclick', function (a) {
 `addLayers` and `removeLayers` are bulk methods for adding and removing markers and should be favoured over the single versions when doing bulk addition/removal of markers. Each takes an array of markers. You can use [dedicated options](#chunked-addlayers) to fine-tune the behaviour of `addLayers`.
 
 These methods extract non-group layer children from Layer Group types, even deeply nested. _However_, be noted that:
-- The input array is modified. If you need to keep it untouched, clone it before passing it (e.g. `newArray = originalArray.slice()`).
 - `chunkProgress` jumps backward when `addLayers` finds a group (since appending its children to the input array makes the total increase).
-- `hasLayer` method will return `true` for child non-group layers, but `false` on any (possibly parent) Layer Group type.
+- Groups are not actually added into the MarkerClusterGroup, only their non-group child layers. Therfore, `hasLayer` method will return `true` for non-group child layers, but `false` on any (possibly parent) Layer Group types.
 
 If you are removing a lot of markers it will almost definitely be better to call `clearLayers` then call `addLayers` to add the markers you don't want to remove back in. See [#59](https://github.com/Leaflet/Leaflet.markercluster/issues/59#issuecomment-9320628) for details.
 
diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js
index b4b6677..d2d1802 100644
--- a/src/MarkerClusterGroup.js
+++ b/src/MarkerClusterGroup.js
@@ -182,6 +182,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 		    chunkProgress = this.options.chunkProgress,
 		    l = layersArray.length,
 		    offset = 0,
+		    originalArray = true,
 		    m;
 
 		if (this._map) {
@@ -202,10 +203,14 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 					// Group of layers, append children to layersArray and skip.
 					// Side effects:
 					// - Total increases, so chunkProgress ratio jumps backward.
-					// - Input array is modified.
+					// - Groups are not included in this group, only their non-group child layers (hasLayer).
 					// Changing array length while looping does not affect performance in current browsers:
 					// http://jsperf.com/for-loop-changing-length/6
 					if (m instanceof L.LayerGroup) {
+						if (originalArray) {
+							layersArray = layersArray.slice();
+							originalArray = false;
+						}
 						this._extractNonGroupLayers(m, layersArray);
 						l = layersArray.length;
 						continue;
@@ -266,6 +271,10 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 
 				// Group of layers, append children to layersArray and skip.
 				if (m instanceof L.LayerGroup) {
+					if (originalArray) {
+						layersArray = layersArray.slice();
+						originalArray = false;
+					}
 					this._extractNonGroupLayers(m, layersArray);
 					l = layersArray.length;
 					continue;
@@ -291,8 +300,9 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 	removeLayers: function (layersArray) {
 		var i, m,
 		    l = layersArray.length,
-			fg = this._featureGroup,
-			npg = this._nonPointGroup;
+		    fg = this._featureGroup,
+		    npg = this._nonPointGroup,
+		    originalArray = true;
 
 		if (!this._map) {
 			for (i = 0; i < l; i++) {
@@ -300,6 +310,10 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 
 				// Group of layers, append children to layersArray and skip.
 				if (m instanceof L.LayerGroup) {
+					if (originalArray) {
+						layersArray = layersArray.slice();
+						originalArray = false;
+					}
 					this._extractNonGroupLayers(m, layersArray);
 					l = layersArray.length;
 					continue;
@@ -339,6 +353,10 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
 
 			// Group of layers, append children to layersArray and skip.
 			if (m instanceof L.LayerGroup) {
+				if (originalArray) {
+					layersArray = layersArray.slice();
+					originalArray = false;
+				}
 				this._extractNonGroupLayers(m, layersArray);
 				l = layersArray.length;
 				continue;

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