Skip to content

Commit

Permalink
Support layerremove event. fixes #647
Browse files Browse the repository at this point in the history
  • Loading branch information
danzel committed Jan 26, 2017
1 parent 2a88279 commit 16cf328
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
110 changes: 110 additions & 0 deletions spec/suites/eventsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
expect(callback.called).to.be(true);
});

//layeradd
it('fires layeradd when markers are added while not on the map', function() {
var callback = sinon.spy();

Expand Down Expand Up @@ -267,6 +268,115 @@

expect(callback.callCount).to.be(1);
});

//layerremove
it('fires layerremove when a marker is removed while not on the map', function() {
var callback = sinon.spy();

group = new L.MarkerClusterGroup();
group.on('layerremove', callback);

var marker = new L.Marker([1.5, 1.5]);
group.addLayer(marker);
group.removeLayer(marker);

expect(callback.callCount).to.be(1);
});

it('fires layerremove when a vector is removed while not on the map', function() {
var callback = sinon.spy();

group = new L.MarkerClusterGroup();
group.on('layerremove', callback);

var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]);
group.addLayer(polygon);
group.removeLayer(polygon);

expect(callback.callCount).to.be(1);
});

it('fires layerremove when a marker is removed while on the map', function() {
var callback = sinon.spy();

group = new L.MarkerClusterGroup();
group.on('layerremove', callback);
map.addLayer(group);

var marker = new L.Marker([1.5, 1.5]);
group.addLayer(marker);
group.removeLayer(marker);

expect(callback.callCount).to.be(1);
});

it('fires layerremove when a vector is removed while on the map', function() {
var callback = sinon.spy();

group = new L.MarkerClusterGroup();
group.on('layerremove', callback);
map.addLayer(group);

var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]);
group.addLayer(polygon);
group.removeLayer(polygon);

expect(callback.callCount).to.be(1);
});

it('fires layerremove when a marker is removed using removeLayers while on the map with chunked loading', function() {
var callback = sinon.spy();

group = new L.MarkerClusterGroup({ chunkedLoading: true });
group.on('layerremove', callback);
map.addLayer(group);

var marker = new L.Marker([1.5, 1.5]);
group.addLayers([marker]);
group.removeLayers([marker]);

expect(callback.callCount).to.be(1);
});

it('fires layerremove when a vector is removed using removeLayers while on the map with chunked loading', function() {
var callback = sinon.spy();

group = new L.MarkerClusterGroup({ chunkedLoading: true });
group.on('layerremove', callback);
map.addLayer(group);

var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]);
group.addLayers([polygon]);
group.removeLayers([polygon]);

expect(callback.callCount).to.be(1);
});

it('fires layerremove when a marker is removed using removeLayers while not on the map with chunked loading', function() {
var callback = sinon.spy();

group = new L.MarkerClusterGroup({ chunkedLoading: true });
group.on('layerremove', callback);

var marker = new L.Marker([1.5, 1.5]);
group.addLayers([marker]);
group.removeLayers([marker]);

expect(callback.callCount).to.be(1);
});

it('fires layerremove when a vector is removed using removeLayers while not on the map with chunked loading', function() {
var callback = sinon.spy();

group = new L.MarkerClusterGroup({ chunkedLoading: true });
group.on('layerremove', callback);

var polygon = new L.Polygon([[1.5, 1.5], [2.0, 1.5], [2.0, 2.0], [1.5, 2.0]]);
group.addLayers([polygon]);
group.removeLayers([polygon]);

expect(callback.callCount).to.be(1);
});
});

/*
Expand Down
6 changes: 6 additions & 0 deletions src/MarkerClusterGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,15 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
//Non point layers
if (!layer.getLatLng) {
this._nonPointGroup.removeLayer(layer);
this.fire('layerremove', { layer: layer });
return this;
}

if (!this._map) {
if (!this._arraySplice(this._needsClustering, layer) && this.hasLayer(layer)) {
this._needsRemoving.push(layer);
}
this.fire('layerremove', { layer: layer });
return this;
}

Expand All @@ -164,6 +166,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({

//Remove the marker from clusters
this._removeLayer(layer, true);
this.fire('layerremove', { layer: layer });

// Refresh bounds and weighted positions.
this._topClusterLevel._recalculateBounds();
Expand Down Expand Up @@ -338,6 +341,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
if (this.hasLayer(m)) {
this._needsRemoving.push(m);
}
this.fire('layerremove', { layer: m });
}
return this;
}
Expand Down Expand Up @@ -378,10 +382,12 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({

if (!m.__parent) {
npg.removeLayer(m);
this.fire('layerremove', { layer: m });
continue;
}

this._removeLayer(m, true, true);
this.fire('layerremove', { layer: m });

if (fg.hasLayer(m)) {
fg.removeLayer(m);
Expand Down

0 comments on commit 16cf328

Please sign in to comment.