From 10b91f8ad4419437aec9d8158e30e7990240dd3e Mon Sep 17 00:00:00 2001 From: danzel Date: Sat, 22 Jun 2013 10:47:10 +1200 Subject: [PATCH] More tests and more making them work --- spec/suites/NonPointSpec.js | 99 +++++++++++++++++++++++------------- spec/suites/getBoundsSpec.js | 91 +++++++++++++++++++++++++++++++++ src/MarkerClusterGroup.js | 64 ++++++++++++++++------- 3 files changed, 200 insertions(+), 54 deletions(-) create mode 100644 spec/suites/getBoundsSpec.js diff --git a/spec/suites/NonPointSpec.js b/spec/suites/NonPointSpec.js index 4e41a365d..561167e2c 100644 --- a/spec/suites/NonPointSpec.js +++ b/spec/suites/NonPointSpec.js @@ -43,6 +43,18 @@ expect(polygon._container.parentNode).to.be(map._pathRoot); }); + it('Removes polygons from map when removed', function () { + + var group = new L.MarkerClusterGroup(); + var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]); + + group.addLayer(polygon); + map.addLayer(group); + map.removeLayer(group); + + expect(polygon._container.parentNode).to.be(null); + }); + describe('hasLayer', function () { it('returns false when not added', function () { var group = new L.MarkerClusterGroup(); @@ -89,37 +101,6 @@ }); }); - describe('getBounds', function() { - it('returns the correct bounds before adding to the map', function() { - var group = new L.MarkerClusterGroup(); - var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]); - - group.addLayer(polygon); - - expect(group.getBounds()).to.be(polygon.getBounds()); - }); - - it('returns the correct bounds after adding to the map after adding polygon', function () { - var group = new L.MarkerClusterGroup(); - var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]); - - group.addLayer(polygon); - map.addLayer(group); - - expect(group.getBounds()).to.be(polygon.getBounds()); - }); - - it('returns the correct bounds after adding to the map before adding polygon', function () { - var group = new L.MarkerClusterGroup(); - var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]); - - map.addLayer(group); - group.addLayer(polygon); - - expect(group.getBounds()).to.be(polygon.getBounds()); - }); - }); - describe('removeLayer', function() { it('removes before adding to map', function () { var group = new L.MarkerClusterGroup(); @@ -147,8 +128,8 @@ var group = new L.MarkerClusterGroup(); var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]); - group.addLayers(polygon); - map.addLayers(group); + group.addLayer(polygon); + map.addLayer(group); expect(group.hasLayer(polygon)).to.be(true); group.removeLayer(polygon); @@ -159,12 +140,60 @@ var group = new L.MarkerClusterGroup(); var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]); - map.addLayers(group); - group.addLayers(polygon); + map.addLayer(group); + group.addLayer(polygon); expect(group.hasLayer(polygon)).to.be(true); group.removeLayer(polygon); expect(group.hasLayer(polygon)).to.be(false); }); }); + + describe('removeLayers', function () { + it('removes before adding to map', function () { + var group = new L.MarkerClusterGroup(); + var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]); + + group.addLayer(polygon); + expect(group.hasLayer(polygon)).to.be(true); + + group.removeLayers([polygon]); + expect(group.hasLayer(polygon)).to.be(false); + }); + + it('removes before adding to map', function () { + var group = new L.MarkerClusterGroup(); + var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]); + + group.addLayers([polygon]); + expect(group.hasLayer(polygon)).to.be(true); + + group.removeLayers([polygon]); + expect(group.hasLayer(polygon)).to.be(false); + }); + + it('removes after adding to map after adding polygon', function () { + var group = new L.MarkerClusterGroup(); + var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]); + + group.addLayer(polygon); + map.addLayer(group); + expect(group.hasLayer(polygon)).to.be(true); + + group.removeLayers([polygon]); + expect(group.hasLayer(polygon)).to.be(false); + }); + + it('removes after adding to map before adding polygon', function () { + var group = new L.MarkerClusterGroup(); + var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]); + + map.addLayer(group); + group.addLayer(polygon); + expect(group.hasLayer(polygon)).to.be(true); + + group.removeLayers([polygon]); + expect(group.hasLayer(polygon)).to.be(false); + }); + }); }); \ No newline at end of file diff --git a/spec/suites/getBoundsSpec.js b/spec/suites/getBoundsSpec.js new file mode 100644 index 000000000..72b28c7e7 --- /dev/null +++ b/spec/suites/getBoundsSpec.js @@ -0,0 +1,91 @@ +describe('adding non point data works', 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); + }); + + describe('polygon layer', function() { + describe('getBounds', function() { + it('returns the correct bounds before adding to the map', function() { + var group = new L.MarkerClusterGroup(); + var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]); + + group.addLayer(polygon); + + expect(group.getBounds().equals(polygon.getBounds())).to.be(true); + }); + + it('returns the correct bounds after adding to the map after adding polygon', function() { + var group = new L.MarkerClusterGroup(); + var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]); + + group.addLayer(polygon); + map.addLayer(group); + + expect(group.getBounds().equals(polygon.getBounds())).to.be(true); + }); + + it('returns the correct bounds after adding to the map before adding polygon', function() { + var group = new L.MarkerClusterGroup(); + var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]); + + map.addLayer(group); + group.addLayer(polygon); + + expect(group.getBounds().equals(polygon.getBounds())).to.be(true); + }); + }); + }); + + describe('marker layers', function () { + describe('getBounds', function () { + it('returns the correct bounds before adding to the map', function () { + var group = new L.MarkerClusterGroup(); + var marker = new L.Marker([1.5, 1.5]); + var marker2 = new L.Marker([1.0, 5.0]); + var marker3 = new L.Marker([6.0, 2.0]); + + group.addLayers([marker, marker2, marker3]); + + expect(group.getBounds().equals(L.latLngBounds([1.0, 5.0], [6.0, 1.5]))).to.be(true); + }); + + it('returns the correct bounds after adding to the map after adding markers', function () { + var group = new L.MarkerClusterGroup(); + var marker = new L.Marker([1.5, 1.5]); + var marker2 = new L.Marker([1.0, 5.0]); + var marker3 = new L.Marker([6.0, 2.0]); + + group.addLayers([marker, marker2, marker3]); + map.addLayer(group); + + expect(group.getBounds().equals(L.latLngBounds([1.0, 5.0], [6.0, 1.5]))).to.be(true); + }); + + it('returns the correct bounds after adding to the map before adding markers', function () { + var group = new L.MarkerClusterGroup(); + var marker = new L.Marker([1.5, 1.5]); + var marker2 = new L.Marker([1.0, 5.0]); + var marker3 = new L.Marker([6.0, 2.0]); + + map.addLayer(group); + group.addLayers([marker, marker2, marker3]); + + expect(group.getBounds().equals(L.latLngBounds([1.0, 5.0], [6.0, 1.5]))).to.be(true); + }); + }); + }); +}); \ No newline at end of file diff --git a/src/MarkerClusterGroup.js b/src/MarkerClusterGroup.js index 58c51b950..904bcc9fd 100644 --- a/src/MarkerClusterGroup.js +++ b/src/MarkerClusterGroup.js @@ -40,6 +40,9 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ this._featureGroup = L.featureGroup(); this._featureGroup.on(L.FeatureGroup.EVENTS, this._propagateEvent, this); + this._nonPointGroup = L.featureGroup(); + this._nonPointGroup.on(L.FeatureGroup.EVENTS, this._propagateEvent, this); + this._inZoomAnimation = 0; this._needsClustering = []; this._needsRemoving = []; //Markers removed while we aren't on the map need to be kept track of @@ -57,6 +60,12 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ return this.addLayers(array); } + //Don't cluster non point data + if (!layer.getLatLng) { + this._nonPointGroup.addLayer(layer); + return this; + } + if (!this._map) { this._needsClustering.push(layer); return this; @@ -66,6 +75,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ return this; } + //If we have already clustered we'll need to add this one to a cluster if (this._unspiderfy) { @@ -95,9 +105,9 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ removeLayer: function (layer) { - //If the layer doesn't have a getLatLng then we can't cluster it, so add it to our child featureGroup + //Non point layers if (!layer.getLatLng) { - this._featureGroup.removeLayer(layer); + this._nonPointGroup.removeLayer(layer); return this; } @@ -133,19 +143,16 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ //Takes an array of markers and adds them in bulk addLayers: function (layersArray) { var i, l, m, - fg = this._featureGroup; - - if (!this._map) { - this._needsClustering = this._needsClustering.concat(layersArray); - return this; - } + onMap = this._map, + fg = this._featureGroup, + npg = this._nonPointGroup; for (i = 0, l = layersArray.length; i < l; i++) { m = layersArray[i]; //Not point data, can't be clustered if (!m.getLatLng) { - fg.addLayer(m); + npg.addLayer(m); continue; } @@ -153,6 +160,11 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ continue; } + if (!onMap) { + this._needsClustering.push(m); + continue; + } + this._addLayer(m, this._maxZoom); //If we just made a cluster of size 2 then we need to remove the other marker from the map (if it is) or we never will @@ -165,14 +177,16 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ } } - //Update the icons of all those visible clusters that were affected - fg.eachLayer(function (c) { - if (c instanceof L.MarkerCluster && c._iconNeedsUpdate) { - c._updateIcon(); - } - }); + if (onMap) { + //Update the icons of all those visible clusters that were affected + fg.eachLayer(function (c) { + if (c instanceof L.MarkerCluster && c._iconNeedsUpdate) { + c._updateIcon(); + } + }); - this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds); + this._topClusterLevel._recursivelyAddChildrenToMap(null, this._zoom, this._currentShownBounds); + } return this; }, @@ -180,11 +194,14 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ //Takes an array of markers and removes them in bulk removeLayers: function (layersArray) { var i, l, m, - fg = this._featureGroup; + fg = this._featureGroup, + npg = this._nonPointGroup; if (!this._map) { for (i = 0, l = layersArray.length; i < l; i++) { - this._arraySplice(this._needsClustering, layersArray[i]); + m = layersArray[i]; + this._arraySplice(this._needsClustering, m); + npg.removeLayer(m); } return this; } @@ -193,6 +210,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ m = layersArray[i]; if (!m.__parent) { + npg.removeLayer(m); continue; } @@ -235,6 +253,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ //Remove all the visible layers this._featureGroup.clearLayers(); + this._nonPointGroup.clearLayers(); this.eachLayer(function (marker) { delete marker.__parent; @@ -258,6 +277,9 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ bounds.extend(this._needsClustering[i].getLatLng()); } } + + bounds.extend(this._nonPointGroup.getBounds()); + return bounds; }, @@ -273,6 +295,8 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ for (i = markers.length - 1; i >= 0; i--) { method.call(context, markers[i]); } + + this._nonPointGroup.eachLayer(method, context); }, //Returns true if the given layer is in this MarkerClusterGroup @@ -296,7 +320,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ } } - return !!(layer.__parent && layer.__parent._group === this); + return !!(layer.__parent && layer.__parent._group === this) || this._nonPointGroup.hasLayer(layer); }, //Zoom down to show the given layer (spiderfying if necessary) then calls the callback @@ -343,6 +367,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ var i, l, layer; this._featureGroup.onAdd(map); + this._nonPointGroup.onAdd(map); if (!this._gridClusters) { this._generateInitialClusters(); @@ -408,6 +433,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({ //Clean up all the layers we added to the map this._featureGroup.onRemove(map); + this._nonPointGroup.onRemove(map); this._map = null; },