From f77480ec28d2b99a82ba0bac48dd9efcde4c51bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Harrtell?= Date: Wed, 17 Feb 2016 13:15:45 +0100 Subject: [PATCH 1/2] Add tests excercising the add/remove/toggle select logic with and without multi --- .../ol/interaction/selectinteraction.test.js | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/test/spec/ol/interaction/selectinteraction.test.js b/test/spec/ol/interaction/selectinteraction.test.js index 6667ae52a2..7842d85d04 100644 --- a/test/spec/ol/interaction/selectinteraction.test.js +++ b/test/spec/ol/interaction/selectinteraction.test.js @@ -129,6 +129,20 @@ describe('ol.interaction.Select', function() { var features = select.getFeatures(); expect(features.getLength()).to.equal(1); }); + + it('select with shift 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, true); + + expect(listenerSpy.callCount).to.be(1); + + var features = select.getFeatures(); + expect(features.getLength()).to.equal(1); + }); }); describe('multiselecting polygons', function() { @@ -154,6 +168,20 @@ describe('ol.interaction.Select', function() { var features = select.getFeatures(); expect(features.getLength()).to.equal(4); }); + + it('select with shift single-click', function() { + var listenerSpy = sinon.spy(function(e) { + expect(e.selected).to.have.length(4); + }); + select.on('select', listenerSpy); + + simulateEvent(ol.MapBrowserEvent.EventType.SINGLECLICK, 10, -20, true); + + expect(listenerSpy.callCount).to.be(1); + + var features = select.getFeatures(); + expect(features.getLength()).to.equal(4); + }); }); describe('toggle selecting polygons', function() { @@ -207,6 +235,24 @@ describe('ol.interaction.Select', function() { expect(features.item(0).get('type')).to.be('bar'); expect(features.item(1).get('type')).to.be('bar'); }); + + it('only selects features that pass the filter ' + + 'using shift single-click', function() { + var select = new ol.interaction.Select({ + multi: true, + filter: function(feature, layer) { + return feature.get('type') === 'bar'; + } + }); + map.addInteraction(select); + + simulateEvent(ol.MapBrowserEvent.EventType.SINGLECLICK, 10, -20, + true); + var features = select.getFeatures(); + expect(features.getLength()).to.equal(2); + expect(features.item(0).get('type')).to.be('bar'); + expect(features.item(1).get('type')).to.be('bar'); + }); }); describe('with multi set to false', function() { @@ -224,8 +270,23 @@ describe('ol.interaction.Select', function() { expect(features.getLength()).to.equal(1); expect(features.item(0).get('type')).to.be('bar'); }); - }); + it('only selects the first feature that passes the filter ' + + 'using shift single-click', function() { + var select = new ol.interaction.Select({ + multi: false, + filter: function(feature, layer) { + return feature.get('type') === 'bar'; + } + }); + map.addInteraction(select); + simulateEvent(ol.MapBrowserEvent.EventType.SINGLECLICK, 10, -20, + true); + var features = select.getFeatures(); + expect(features.getLength()).to.equal(1); + expect(features.item(0).get('type')).to.be('bar'); + }); + }); }); describe('#getLayer(feature)', function() { From 7822f5ce5df3694128a66dc18682ff6defe529bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Harrtell?= Date: Wed, 17 Feb 2016 13:18:06 +0100 Subject: [PATCH 2/2] Consider multi in add/remove/toggle select logic --- src/ol/interaction/selectinteraction.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ol/interaction/selectinteraction.js b/src/ol/interaction/selectinteraction.js index 02da1f19ca..dbc2af2263 100644 --- a/src/ol/interaction/selectinteraction.js +++ b/src/ol/interaction/selectinteraction.js @@ -320,6 +320,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) { /** * @param {ol.Feature|ol.render.Feature} feature Feature. * @param {ol.layer.Layer} layer Layer. + * @return {boolean|undefined} Continue to iterate over the features. */ function(feature, layer) { if (this.filter_(feature, layer)) { @@ -332,6 +333,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) { deselected.push(feature); this.removeFeatureLayerAssociation_(feature); } + return !this.multi_; } }, this, this.layerFilter_); var i;