Merge pull request #5424 from fredj/select_event

Don't dispatch select event if no feature is selected or deselected
This commit is contained in:
Frédéric Junod
2016-06-29 15:15:13 +02:00
committed by GitHub
2 changed files with 33 additions and 9 deletions
+4 -9
View File
@@ -263,7 +263,6 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
var features = this.featureOverlay_.getSource().getFeaturesCollection(); var features = this.featureOverlay_.getSource().getFeaturesCollection();
var deselected = []; var deselected = [];
var selected = []; var selected = [];
var change = false;
if (set) { if (set) {
// Replace the currently selected feature(s) with the feature(s) at the // 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 // pixel, or clear the selected feature(s) if there is no feature at
@@ -282,11 +281,10 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
return !this.multi_; return !this.multi_;
} }
}, this, this.layerFilter_); }, this, this.layerFilter_);
if (selected.length > 0 && features.getLength() == 1 && if (selected.length > 0 && features.getLength() == 1 && features.item(0) == selected[0]) {
features.item(0) == selected[0]) { // No change; an already selected feature is selected again
// No change selected.length = 0;
} else { } else {
change = true;
if (features.getLength() !== 0) { if (features.getLength() !== 0) {
deselected = Array.prototype.concat(features.getArray()); deselected = Array.prototype.concat(features.getArray());
features.clear(); features.clear();
@@ -320,11 +318,8 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
features.remove(deselected[i]); features.remove(deselected[i]);
} }
features.extend(selected); features.extend(selected);
if (selected.length > 0 || deselected.length > 0) {
change = true;
}
} }
if (change) { if (selected.length > 0 || deselected.length > 0) {
this.dispatchEvent( this.dispatchEvent(
new ol.interaction.SelectEvent(ol.interaction.SelectEventType.SELECT, new ol.interaction.SelectEvent(ol.interaction.SelectEventType.SELECT,
selected, deselected, mapBrowserEvent)); selected, deselected, mapBrowserEvent));
@@ -130,6 +130,35 @@ describe('ol.interaction.Select', function() {
expect(features.getLength()).to.equal(1); 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 twice with single-click', function() {
var listenerSpy = sinon.spy(function(e) {
expect(e.selected).to.have.length(1);
});
select.on('select', listenerSpy);
simulateEvent(ol.MapBrowserEvent.EventType.SINGLECLICK, 10, -20);
simulateEvent(ol.MapBrowserEvent.EventType.SINGLECLICK, 9, -21);
expect(listenerSpy.callCount).to.be(1);
var features = select.getFeatures();
expect(features.getLength()).to.equal(1);
});
it('select with shift single-click', function() { it('select with shift single-click', function() {
var listenerSpy = sinon.spy(function(e) { var listenerSpy = sinon.spy(function(e) {
expect(e.selected).to.have.length(1); expect(e.selected).to.have.length(1);