Allow cluster source to unlisten from its source

This adds a setSource method to change or remove the cluster source's source.
This commit is contained in:
Maximilian Kroeg
2020-02-25 10:33:02 +01:00
parent e02ede8f76
commit 01b3f9a97b
+33 -10
View File
@@ -29,7 +29,7 @@ import VectorSource from './Vector.js';
* ``` * ```
* See {@link module:ol/geom/Polygon~Polygon#getInteriorPoint} for a way to get a cluster * See {@link module:ol/geom/Polygon~Polygon#getInteriorPoint} for a way to get a cluster
* calculation point for polygons. * calculation point for polygons.
* @property {VectorSource} source Source. * @property {VectorSource} [source=null] Source.
* @property {boolean} [wrapX=true] Whether to wrap the world horizontally. * @property {boolean} [wrapX=true] Whether to wrap the world horizontally.
*/ */
@@ -39,6 +39,10 @@ import VectorSource from './Vector.js';
* Layer source to cluster vector data. Works out of the box with point * Layer source to cluster vector data. Works out of the box with point
* geometries. For other geometry types, or if not all geometries should be * geometries. For other geometry types, or if not all geometries should be
* considered for clustering, a custom `geometryFunction` can be defined. * considered for clustering, a custom `geometryFunction` can be defined.
*
* If the instance is disposed without also disposing the underlying
* source `setSource(null)` has to be called to remove the listener reference
* from the wrapped source.
* @api * @api
*/ */
class Cluster extends VectorSource { class Cluster extends VectorSource {
@@ -81,13 +85,17 @@ class Cluster extends VectorSource {
return geometry; return geometry;
}; };
/** this.boundRefresh_ = this.refresh.bind(this);
* @type {VectorSource}
* @protected
*/
this.source = options.source;
this.source.addEventListener(EventType.CHANGE, this.refresh.bind(this)); this.setSource(options.source || null);
}
/**
* @override
*/
clear(opt_fast) {
this.features.length = 0;
super.clear(opt_fast);
} }
/** /**
@@ -132,7 +140,23 @@ class Cluster extends VectorSource {
} }
/** /**
* handle the source changing * Replace the wrapped source.
* @param {VectorSource|null} source The new source for this instance.
* @api
*/
setSource(source) {
if (this.source) {
this.source.removeEventListener(EventType.CHANGE, this.boundRefresh_);
}
this.source = source;
if (source) {
this.sourceListenKey_ = source.addEventListener(EventType.CHANGE, this.boundRefresh_);
}
this.refresh();
}
/**
* Handle the source changing.
* @override * @override
*/ */
refresh() { refresh() {
@@ -145,10 +169,9 @@ class Cluster extends VectorSource {
* @protected * @protected
*/ */
cluster() { cluster() {
if (this.resolution === undefined) { if (this.resolution === undefined || !this.source) {
return; return;
} }
this.features.length = 0;
const extent = createEmpty(); const extent = createEmpty();
const mapDistance = this.distance * this.resolution; const mapDistance = this.distance * this.resolution;
const features = this.source.getFeatures(); const features = this.source.getFeatures();