Listen only to change event triggered by the feature
ol.Feature catch change event from its geometry and trigger a change event itself. ol.Feature will also trigger change event when its geometryName has changed
This commit is contained in:
+10
-41
@@ -3,8 +3,6 @@ goog.provide('ol.interaction.Snap');
|
|||||||
goog.require('ol');
|
goog.require('ol');
|
||||||
goog.require('ol.Collection');
|
goog.require('ol.Collection');
|
||||||
goog.require('ol.CollectionEventType');
|
goog.require('ol.CollectionEventType');
|
||||||
goog.require('ol.Object');
|
|
||||||
goog.require('ol.Observable');
|
|
||||||
goog.require('ol.coordinate');
|
goog.require('ol.coordinate');
|
||||||
goog.require('ol.events');
|
goog.require('ol.events');
|
||||||
goog.require('ol.events.EventType');
|
goog.require('ol.events.EventType');
|
||||||
@@ -83,13 +81,7 @@ ol.interaction.Snap = function(opt_options) {
|
|||||||
* @type {Object.<number, ol.EventsKey>}
|
* @type {Object.<number, ol.EventsKey>}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
this.geometryChangeListenerKeys_ = {};
|
this.featureChangeListenerKeys_ = {};
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {Object.<number, ol.EventsKey>}
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
this.geometryModifyListenerKeys_ = {};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extents are preserved so indexed segment can be quickly removed
|
* Extents are preserved so indexed segment can be quickly removed
|
||||||
@@ -159,7 +151,7 @@ ol.inherits(ol.interaction.Snap, ol.interaction.Pointer);
|
|||||||
/**
|
/**
|
||||||
* Add a feature to the collection of features that we may snap to.
|
* Add a feature to the collection of features that we may snap to.
|
||||||
* @param {ol.Feature} feature Feature.
|
* @param {ol.Feature} feature Feature.
|
||||||
* @param {boolean=} opt_listen Whether to listen to the geometry change or not
|
* @param {boolean=} opt_listen Whether to listen to the feature change or not
|
||||||
* Defaults to `true`.
|
* Defaults to `true`.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -173,22 +165,14 @@ ol.interaction.Snap.prototype.addFeature = function(feature, opt_listen) {
|
|||||||
this.indexedFeaturesExtents_[feature_uid] = geometry.getExtent(
|
this.indexedFeaturesExtents_[feature_uid] = geometry.getExtent(
|
||||||
ol.extent.createEmpty());
|
ol.extent.createEmpty());
|
||||||
segmentWriter.call(this, feature, geometry);
|
segmentWriter.call(this, feature, geometry);
|
||||||
|
|
||||||
if (listen) {
|
|
||||||
this.geometryModifyListenerKeys_[feature_uid] = ol.events.listen(
|
|
||||||
geometry,
|
|
||||||
ol.events.EventType.CHANGE,
|
|
||||||
this.handleGeometryModify_.bind(this, feature),
|
|
||||||
this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listen) {
|
if (listen) {
|
||||||
this.geometryChangeListenerKeys_[feature_uid] = ol.events.listen(
|
this.featureChangeListenerKeys_[feature_uid] = ol.events.listen(
|
||||||
feature,
|
feature,
|
||||||
ol.Object.getChangeEventType(feature.getGeometryName()),
|
ol.events.EventType.CHANGE,
|
||||||
this.handleGeometryChange_, this);
|
this.handleFeatureChange_, this);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -260,19 +244,8 @@ ol.interaction.Snap.prototype.handleFeatureRemove_ = function(evt) {
|
|||||||
* @param {ol.events.Event} evt Event.
|
* @param {ol.events.Event} evt Event.
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
ol.interaction.Snap.prototype.handleGeometryChange_ = function(evt) {
|
ol.interaction.Snap.prototype.handleFeatureChange_ = function(evt) {
|
||||||
var feature = /** @type {ol.Feature} */ (evt.target);
|
var feature = /** @type {ol.Feature} */ (evt.target);
|
||||||
this.removeFeature(feature, true);
|
|
||||||
this.addFeature(feature, true);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {ol.Feature} feature Feature which geometry was modified.
|
|
||||||
* @param {ol.events.Event} evt Event.
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
ol.interaction.Snap.prototype.handleGeometryModify_ = function(feature, evt) {
|
|
||||||
if (this.handlingDownUpSequence) {
|
if (this.handlingDownUpSequence) {
|
||||||
var uid = ol.getUid(feature);
|
var uid = ol.getUid(feature);
|
||||||
if (!(uid in this.pendingFeatures_)) {
|
if (!(uid in this.pendingFeatures_)) {
|
||||||
@@ -287,7 +260,7 @@ ol.interaction.Snap.prototype.handleGeometryModify_ = function(feature, evt) {
|
|||||||
/**
|
/**
|
||||||
* Remove a feature from the collection of features that we may snap to.
|
* Remove a feature from the collection of features that we may snap to.
|
||||||
* @param {ol.Feature} feature Feature
|
* @param {ol.Feature} feature Feature
|
||||||
* @param {boolean=} opt_unlisten Whether to unlisten to the geometry change
|
* @param {boolean=} opt_unlisten Whether to unlisten to the feature change
|
||||||
* or not. Defaults to `true`.
|
* or not. Defaults to `true`.
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
@@ -306,15 +279,11 @@ ol.interaction.Snap.prototype.removeFeature = function(feature, opt_unlisten) {
|
|||||||
for (i = nodesToRemove.length - 1; i >= 0; --i) {
|
for (i = nodesToRemove.length - 1; i >= 0; --i) {
|
||||||
rBush.remove(nodesToRemove[i]);
|
rBush.remove(nodesToRemove[i]);
|
||||||
}
|
}
|
||||||
if (unlisten) {
|
|
||||||
ol.Observable.unByKey(this.geometryModifyListenerKeys_[feature_uid]);
|
|
||||||
delete this.geometryModifyListenerKeys_[feature_uid];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unlisten) {
|
if (unlisten) {
|
||||||
ol.Observable.unByKey(this.geometryChangeListenerKeys_[feature_uid]);
|
ol.events.unlistenByKey(this.featureChangeListenerKeys_[feature_uid]);
|
||||||
delete this.geometryChangeListenerKeys_[feature_uid];
|
delete this.featureChangeListenerKeys_[feature_uid];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -328,7 +297,7 @@ ol.interaction.Snap.prototype.setMap = function(map) {
|
|||||||
var features = this.getFeatures_();
|
var features = this.getFeatures_();
|
||||||
|
|
||||||
if (currentMap) {
|
if (currentMap) {
|
||||||
keys.forEach(ol.Observable.unByKey);
|
keys.forEach(ol.events.unlistenByKey);
|
||||||
keys.length = 0;
|
keys.length = 0;
|
||||||
features.forEach(this.forEachFeatureRemove_, this);
|
features.forEach(this.forEachFeatureRemove_, this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -122,13 +122,57 @@ describe('ol.interaction.Snap', function() {
|
|||||||
|
|
||||||
var event = {
|
var event = {
|
||||||
pixel: [7 + width / 2, height / 2 - 4],
|
pixel: [7 + width / 2, height / 2 - 4],
|
||||||
coorinate: [7, 4],
|
coordinate: [7, 4],
|
||||||
map: map
|
map: map
|
||||||
};
|
};
|
||||||
ol.interaction.Snap.handleEvent_.call(snapInteraction, event);
|
ol.interaction.Snap.handleEvent_.call(snapInteraction, event);
|
||||||
expect(event.coordinate).to.eql([10, 0]);
|
expect(event.coordinate).to.eql([10, 0]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('handle geometry changes', function() {
|
||||||
|
var line = new ol.Feature(new ol.geom.LineString([[-10, 0], [0, 0]]));
|
||||||
|
var snapInteraction = new ol.interaction.Snap({
|
||||||
|
features: new ol.Collection([line]),
|
||||||
|
pixelTolerance: 5,
|
||||||
|
edge: false
|
||||||
|
});
|
||||||
|
snapInteraction.setMap(map);
|
||||||
|
|
||||||
|
line.getGeometry().setCoordinates([[-10, 0], [10, 0]]);
|
||||||
|
|
||||||
|
var event = {
|
||||||
|
pixel: [7 + width / 2, height / 2 - 4],
|
||||||
|
coordinate: [7, 4],
|
||||||
|
map: map
|
||||||
|
};
|
||||||
|
ol.interaction.Snap.handleEvent_.call(snapInteraction, event);
|
||||||
|
expect(event.coordinate).to.eql([10, 0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handle geometry name changes', function() {
|
||||||
|
var line = new ol.Feature({
|
||||||
|
geometry: new ol.geom.LineString([[-10, 0], [0, 0]]),
|
||||||
|
alt_geometry: new ol.geom.LineString([[-10, 0], [10, 0]])
|
||||||
|
});
|
||||||
|
var snapInteraction = new ol.interaction.Snap({
|
||||||
|
features: new ol.Collection([line]),
|
||||||
|
pixelTolerance: 5,
|
||||||
|
edge: false
|
||||||
|
});
|
||||||
|
snapInteraction.setMap(map);
|
||||||
|
|
||||||
|
line.setGeometryName('alt_geometry');
|
||||||
|
|
||||||
|
var event = {
|
||||||
|
pixel: [7 + width / 2, height / 2 - 4],
|
||||||
|
coordinate: [7, 4],
|
||||||
|
map: map
|
||||||
|
};
|
||||||
|
ol.interaction.Snap.handleEvent_.call(snapInteraction, event);
|
||||||
|
expect(event.coordinate).to.eql([10, 0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user