Skip to content

Commit

Permalink
Support layeradd event. refs #647
Browse files Browse the repository at this point in the history
  • Loading branch information
danzel committed Jan 26, 2017
1 parent dbdedb9 commit 2a88279
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
76 changes: 76 additions & 0 deletions spec/suites/eventsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,82 @@

expect(callback.called).to.be(true);
});

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

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

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

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

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

group = new L.MarkerClusterGroup();
group.on('layeradd', 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);

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

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

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

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

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

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

group = new L.MarkerClusterGroup();
group.on('layeradd', 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);

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

it('fires layeradd when markers are added using addLayers while on the map with chunked loading', function() {
var callback = sinon.spy();

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

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

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

it('fires layeradd when vectors are added using addLayers while on the map with chunked loading', function() {
var callback = sinon.spy();

group = new L.MarkerClusterGroup({ chunkedLoading: true });
group.on('layeradd', 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]);

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

/*
Expand Down
13 changes: 11 additions & 2 deletions src/MarkerClusterGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
//Don't cluster non point data
if (!layer.getLatLng) {
this._nonPointGroup.addLayer(layer);
this.fire('layeradd', { layer: layer });
return this;
}

if (!this._map) {
this._needsClustering.push(layer);
this.fire('layeradd', { layer: layer });
return this;
}

Expand All @@ -106,6 +108,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
}

this._addLayer(layer, this._maxZoom);
this.fire('layeradd', { layer: layer });

// Refresh bounds and weighted positions.
this._topClusterLevel._recalculateBounds();
Expand Down Expand Up @@ -180,7 +183,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
},

//Takes an array of markers and adds them in bulk
addLayers: function (layersArray) {
addLayers: function (layersArray, skipLayerAddEvent) {
if (!L.Util.isArray(layersArray)) {
return this.addLayer(layersArray);
}
Expand Down Expand Up @@ -229,6 +232,9 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
//Not point data, can't be clustered
if (!m.getLatLng) {
npg.addLayer(m);
if (!skipLayerAddEvent) {
this.fire('layeradd', { layer: m });
}
continue;
}

Expand All @@ -237,6 +243,9 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
}

this._addLayer(m, this._maxZoom);
if (!skipLayerAddEvent) {
this.fire('layeradd', { layer: m });
}

//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
if (m.__parent) {
Expand Down Expand Up @@ -584,7 +593,7 @@ L.MarkerClusterGroup = L.FeatureGroup.extend({
//Actually add our markers to the map:
l = this._needsClustering;
this._needsClustering = [];
this.addLayers(l);
this.addLayers(l, true);
},

//Overrides FeatureGroup.onRemove
Expand Down

0 comments on commit 2a88279

Please sign in to comment.