Include own layer in layerFilter and only select unselected features
When selecting an already selected feature, it will be on the Select interaction's featureOverlay_. So we need to include that featureOverlay_ in the layer filter, regardless of what the user set as layer filter. When in toggle mode, we need to make sure that we only select features that are not already included in the selection.
This commit is contained in:
@@ -154,10 +154,36 @@ ol.interaction.Select = function(opt_options) {
|
|||||||
this.filter_ = options.filter ? options.filter :
|
this.filter_ = options.filter ? options.filter :
|
||||||
goog.functions.TRUE;
|
goog.functions.TRUE;
|
||||||
|
|
||||||
|
var featureOverlay = new ol.layer.Vector({
|
||||||
|
source: new ol.source.Vector({
|
||||||
|
useSpatialIndex: false,
|
||||||
|
features: options.features,
|
||||||
|
wrapX: options.wrapX
|
||||||
|
}),
|
||||||
|
style: options.style ? options.style :
|
||||||
|
ol.interaction.Select.getDefaultStyleFunction(),
|
||||||
|
updateWhileAnimating: true,
|
||||||
|
updateWhileInteracting: true
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @type {ol.layer.Vector}
|
||||||
|
*/
|
||||||
|
this.featureOverlay_ = featureOverlay;
|
||||||
|
|
||||||
var layerFilter;
|
var layerFilter;
|
||||||
if (options.layers) {
|
if (options.layers) {
|
||||||
if (goog.isFunction(options.layers)) {
|
if (goog.isFunction(options.layers)) {
|
||||||
layerFilter = options.layers;
|
layerFilter =
|
||||||
|
/**
|
||||||
|
* @param {ol.layer.Layer} layer Layer.
|
||||||
|
* @return {boolean} Include.
|
||||||
|
*/
|
||||||
|
function(layer) {
|
||||||
|
goog.asserts.assertFunction(options.layers);
|
||||||
|
return layer === featureOverlay || options.layers(layer);
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
var layers = options.layers;
|
var layers = options.layers;
|
||||||
layerFilter =
|
layerFilter =
|
||||||
@@ -166,7 +192,7 @@ ol.interaction.Select = function(opt_options) {
|
|||||||
* @return {boolean} Include.
|
* @return {boolean} Include.
|
||||||
*/
|
*/
|
||||||
function(layer) {
|
function(layer) {
|
||||||
return ol.array.includes(layers, layer);
|
return layer === featureOverlay || ol.array.includes(layers, layer);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -187,22 +213,6 @@ ol.interaction.Select = function(opt_options) {
|
|||||||
*/
|
*/
|
||||||
this.featureLayerAssociation_ = {};
|
this.featureLayerAssociation_ = {};
|
||||||
|
|
||||||
/**
|
|
||||||
* @private
|
|
||||||
* @type {ol.layer.Vector}
|
|
||||||
*/
|
|
||||||
this.featureOverlay_ = new ol.layer.Vector({
|
|
||||||
source: new ol.source.Vector({
|
|
||||||
useSpatialIndex: false,
|
|
||||||
features: options.features,
|
|
||||||
wrapX: options.wrapX
|
|
||||||
}),
|
|
||||||
style: options.style ? options.style :
|
|
||||||
ol.interaction.Select.getDefaultStyleFunction(),
|
|
||||||
updateWhileAnimating: true,
|
|
||||||
updateWhileInteracting: true
|
|
||||||
});
|
|
||||||
|
|
||||||
var features = this.featureOverlay_.getSource().getFeaturesCollection();
|
var features = this.featureOverlay_.getSource().getFeaturesCollection();
|
||||||
goog.events.listen(features, ol.CollectionEventType.ADD,
|
goog.events.listen(features, ol.CollectionEventType.ADD,
|
||||||
this.addFeature_, false, this);
|
this.addFeature_, false, this);
|
||||||
@@ -318,9 +328,11 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
|
|||||||
* @param {ol.layer.Layer} layer Layer.
|
* @param {ol.layer.Layer} layer Layer.
|
||||||
*/
|
*/
|
||||||
function(feature, layer) {
|
function(feature, layer) {
|
||||||
if (!ol.array.includes(features.getArray(), feature)) {
|
if (layer !== this.featureOverlay_) {
|
||||||
if (add || toggle) {
|
if (add || toggle) {
|
||||||
if (this.filter_(feature, layer)) {
|
if (this.filter_(feature, layer) &&
|
||||||
|
!ol.array.includes(features.getArray(), feature) &&
|
||||||
|
!ol.array.includes(selected, feature)) {
|
||||||
selected.push(feature);
|
selected.push(feature);
|
||||||
this.addFeatureLayerAssociation_(feature, layer);
|
this.addFeatureLayerAssociation_(feature, layer);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user