Clear features properly when there is no spatial index

Previously clear() only kept the features collection in sync whern there
was also a features RTree.
This commit is contained in:
Andreas Hocevar
2015-07-15 15:15:41 +02:00
parent afce912f11
commit 8f7cbc5ed6
2 changed files with 54 additions and 6 deletions

View File

@@ -372,16 +372,14 @@ ol.source.Vector.prototype.bindFeaturesCollection_ = function(collection) {
*/ */
ol.source.Vector.prototype.clear = function(opt_fast) { ol.source.Vector.prototype.clear = function(opt_fast) {
if (opt_fast) { if (opt_fast) {
for (var featureId in this.featureChangeKeys_) {
var keys = this.featureChangeKeys_[featureId];
goog.array.forEach(keys, goog.events.unlistenByKey);
}
if (goog.isNull(this.featuresCollection_)) { if (goog.isNull(this.featuresCollection_)) {
for (var featureId in this.featureChangeKeys_) {
var keys = this.featureChangeKeys_[featureId];
goog.array.forEach(keys, goog.events.unlistenByKey);
}
this.featureChangeKeys_ = {}; this.featureChangeKeys_ = {};
this.idIndex_ = {}; this.idIndex_ = {};
this.undefIdIndex_ = {}; this.undefIdIndex_ = {};
} else {
this.featuresCollection_.clear();
} }
} else { } else {
var rmFeatureInternal = this.removeFeatureInternal; var rmFeatureInternal = this.removeFeatureInternal;
@@ -390,6 +388,9 @@ ol.source.Vector.prototype.clear = function(opt_fast) {
goog.object.forEach(this.nullGeometryFeatures_, rmFeatureInternal, this); goog.object.forEach(this.nullGeometryFeatures_, rmFeatureInternal, this);
} }
} }
if (!goog.isNull(this.featuresCollection_)) {
this.featuresCollection_.clear();
}
goog.asserts.assert(goog.object.isEmpty(this.featureChangeKeys_), goog.asserts.assert(goog.object.isEmpty(this.featureChangeKeys_),
'featureChangeKeys is an empty object now'); 'featureChangeKeys is an empty object now');
goog.asserts.assert(goog.object.isEmpty(this.idIndex_), goog.asserts.assert(goog.object.isEmpty(this.idIndex_),

View File

@@ -447,6 +447,53 @@ describe('ol.source.Vector', function() {
}); });
describe('with a collection of features', function() { describe('with a collection of features', function() {
var collection, source;
beforeEach(function() {
source = new ol.source.Vector({
useSpatialIndex: false
});
collection = source.getFeaturesCollection();
});
it('creates a features collection', function() {
expect(source.getFeaturesCollection()).to.not.be(null);
});
it('adding/removing features keeps the collection in sync', function() {
var feature = new ol.Feature();
source.addFeature(feature);
expect(collection.getLength()).to.be(1);
source.removeFeature(feature);
expect(collection.getLength()).to.be(0);
});
it('#clear() features keeps the collection in sync', function() {
var feature = new ol.Feature();
source.addFeatures([feature]);
expect(collection.getLength()).to.be(1);
source.clear();
expect(collection.getLength()).to.be(0);
source.addFeatures([feature]);
expect(collection.getLength()).to.be(1);
source.clear(true);
expect(collection.getLength()).to.be(0);
});
it('keeps the source\'s features in sync with the collection', function() {
var feature = new ol.Feature();
collection.push(feature);
expect(source.getFeatures().length).to.be(1);
collection.remove(feature);
expect(source.getFeatures().length).to.be(0);
collection.extend([feature]);
expect(source.getFeatures().length).to.be(1);
collection.clear();
expect(source.getFeatures().length).to.be(0);
});
});
describe('with a collection of features plus spatial index', function() {
var collection, source; var collection, source;
beforeEach(function() { beforeEach(function() {
collection = new ol.Collection(); collection = new ol.Collection();