Making it so the setFilter method on the filter strategy allows notification to listeners of feature add/remove events. p=vog,me r=me (closes #3216)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@11804 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2011-03-30 20:19:24 +00:00
parent b14822c569
commit 5fc138f69b
2 changed files with 37 additions and 16 deletions

View File

@@ -138,17 +138,19 @@ OpenLayers.Strategy.Filter = OpenLayers.Class(OpenLayers.Strategy, {
// cache now contains features to remove from layer
if (this.cache.length > 0) {
this.caching = true;
this.layer.removeFeatures(this.cache.slice(), {silent: true});
this.layer.removeFeatures(this.cache.slice());
this.caching = false;
}
// now look through previous cache for features to add to layer
if (previousCache.length > 0) {
var event = {features: previousCache};
this.handleAdd(event);
// event has features to add to layer
this.caching = true;
this.layer.addFeatures(event.features, {silent: true});
this.caching = false;
if (event.features.length > 0) {
// event has features to add to layer
this.caching = true;
this.layer.addFeatures(event.features);
this.caching = false;
}
}
},

View File

@@ -70,7 +70,7 @@ function test_autoActivate(t) {
function test_setFilter(t) {
t.plan(7);
t.plan(13);
var strategy = new OpenLayers.Strategy.Filter({filter: filter});
var layer = new OpenLayers.Layer.Vector(null, {
@@ -84,24 +84,43 @@ function test_setFilter(t) {
center: new OpenLayers.LonLat(0, 0),
zoom: 1
});
var log = [];
layer.events.on({
beforefeaturesadded: function(event) {
log.push(event.type);
},
beforefeaturesremoved: function(event) {
log.push(event.type);
}
})
// add all features
// a) add all features
log = [];
layer.addFeatures(features);
t.eq(features.length, 20, "collection of 20 features")
t.eq(layer.features.length, 10, "layer got 10 with filter 'index < 10'");
t.eq(strategy.cache.length, 10, "strategy cached 10 with filter 'index < 10'");
t.eq(features.length, 20, "a) collection of 20 features")
t.eq(layer.features.length, 10, "a) layer got 10 with filter 'index < 10'");
t.eq(strategy.cache.length, 10, "a) strategy cached 10 with filter 'index < 10'");
t.eq(log.length, 1, "a) one event logged");
t.eq(log[0], "beforefeaturesadded", "a) beforefeaturesadded fired");
// update filter
// b) update filter
log = [];
filter.value = 5;
strategy.setFilter(filter);
t.eq(layer.features.length, 5, "layer got 5 with filter 'index < 5'");
t.eq(strategy.cache.length, 15, "strategy cached 15 with filter 'index < 5'");
t.eq(layer.features.length, 5, "b) layer got 5 with filter 'index < 5'");
t.eq(strategy.cache.length, 15, "b) strategy cached 15 with filter 'index < 5'");
t.eq(log.length, 1, "b) one event logged");
t.eq(log[0], "beforefeaturesremoved", "b) beforefeaturesremoved fired");
// update filter
// c) update filter
log = [];
filter.value = 15;
strategy.setFilter(filter);
t.eq(layer.features.length, 15, "layer got 15 with filter 'index < 15'");
t.eq(strategy.cache.length, 5, "strategy cached 5 with filter 'index < 15'");
t.eq(layer.features.length, 15, "c) layer got 15 with filter 'index < 15'");
t.eq(strategy.cache.length, 5, "c) strategy cached 5 with filter 'index < 15'");
t.eq(log.length, 1, "c) one event logged");
t.eq(log[0], "beforefeaturesadded", "c) beforefeaturesadded fired");
map.destroy();