Don't dispatch select event if no feature is selected or deselected

This commit is contained in:
Frederic Junod
2016-06-02 17:02:25 +02:00
parent 39de933e64
commit e7c2d9ab32
2 changed files with 17 additions and 7 deletions

View File

@@ -263,7 +263,6 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
var features = this.featureOverlay_.getSource().getFeaturesCollection();
var deselected = [];
var selected = [];
var change = false;
if (set) {
// Replace the currently selected feature(s) with the feature(s) at the
// pixel, or clear the selected feature(s) if there is no feature at
@@ -284,9 +283,8 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
}, this, this.layerFilter_);
if (selected.length > 0 && features.getLength() == 1 &&
features.item(0) == selected[0]) {
// No change
// No change; an already selected feature is selected again
} else {
change = true;
if (features.getLength() !== 0) {
deselected = Array.prototype.concat(features.getArray());
features.clear();
@@ -320,11 +318,8 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
features.remove(deselected[i]);
}
features.extend(selected);
if (selected.length > 0 || deselected.length > 0) {
change = true;
}
}
if (change) {
if (selected.length > 0 || deselected.length > 0) {
this.dispatchEvent(
new ol.interaction.SelectEvent(ol.interaction.SelectEventType.SELECT,
selected, deselected, mapBrowserEvent));

View File

@@ -130,6 +130,21 @@ describe('ol.interaction.Select', function() {
expect(features.getLength()).to.equal(1);
});
it('single-click outside the geometry', function() {
var listenerSpy = sinon.spy(function(e) {
expect(e.selected).to.have.length(1);
});
select.on('select', listenerSpy);
simulateEvent(ol.MapBrowserEvent.EventType.SINGLECLICK, -10, -10);
expect(listenerSpy.callCount).to.be(0);
var features = select.getFeatures();
expect(features.getLength()).to.equal(0);
});
it('select with shift single-click', function() {
var listenerSpy = sinon.spy(function(e) {
expect(e.selected).to.have.length(1);