Remove ol.geom.GeometryEvent
This removes geometry specific change events. Since geometries are mere observables, we only get generic change events. To minimize changes in other places, as a workaround, we cache geometry bounds on features. This is not the way things should be long term, but the objective is to remove the geometry specific event.
This commit is contained in:
@@ -8,7 +8,6 @@ goog.require('goog.events.Event');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.Object');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.GeometryEvent');
|
||||
|
||||
|
||||
|
||||
@@ -28,6 +27,12 @@ goog.require('ol.geom.GeometryEvent');
|
||||
*/
|
||||
ol.Feature = function(opt_values) {
|
||||
|
||||
/**
|
||||
* @type {ol.Extent}
|
||||
* @private
|
||||
*/
|
||||
this.geometryExtent_ = null;
|
||||
|
||||
goog.base(this, opt_values);
|
||||
|
||||
/**
|
||||
@@ -118,12 +123,14 @@ ol.Feature.prototype.getSymbolizers = function() {
|
||||
|
||||
/**
|
||||
* Listener for geometry change events.
|
||||
* @param {ol.geom.GeometryEvent} evt Geometry event.
|
||||
* @param {goog.events.Event} evt Change event.
|
||||
* @private
|
||||
*/
|
||||
ol.Feature.prototype.handleGeometryChange_ = function(evt) {
|
||||
var oldExtent = this.geometryExtent_;
|
||||
this.geometryExtent_ = this.getGeometry().getBounds();
|
||||
this.dispatchEvent(new ol.FeatureEvent(
|
||||
ol.FeatureEventType.CHANGE, this, evt.oldExtent));
|
||||
ol.FeatureEventType.CHANGE, this, oldExtent));
|
||||
};
|
||||
|
||||
|
||||
@@ -135,10 +142,10 @@ ol.Feature.prototype.handleGeometryChange_ = function(evt) {
|
||||
*/
|
||||
ol.Feature.prototype.set = function(key, value) {
|
||||
var geometry = this.getGeometry();
|
||||
var oldExtent = null;
|
||||
var oldExtent = this.geometryExtent_;
|
||||
if (goog.isDefAndNotNull(geometry)) {
|
||||
oldExtent = geometry.getBounds();
|
||||
if (key === this.geometryName_) {
|
||||
this.geometryExtent_ = null;
|
||||
goog.events.unlisten(geometry, goog.events.EventType.CHANGE,
|
||||
this.handleGeometryChange_, false, this);
|
||||
}
|
||||
@@ -148,6 +155,7 @@ ol.Feature.prototype.set = function(key, value) {
|
||||
this.geometryName_ = key;
|
||||
}
|
||||
if (key === this.geometryName_) {
|
||||
this.geometryExtent_ = value.getBounds();
|
||||
goog.events.listen(value, goog.events.EventType.CHANGE,
|
||||
this.handleGeometryChange_, false, this);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
goog.provide('ol.geom.AbstractCollection');
|
||||
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.extent');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.GeometryEvent');
|
||||
|
||||
|
||||
|
||||
@@ -77,21 +75,12 @@ ol.geom.AbstractCollection.prototype.getType = goog.abstractMethod;
|
||||
|
||||
/**
|
||||
* Listener for component change events.
|
||||
* @param {ol.geom.GeometryEvent} evt Geometry event.
|
||||
* @param {goog.events.Event} evt Change event.
|
||||
* @protected
|
||||
*/
|
||||
ol.geom.AbstractCollection.prototype.handleComponentChange = function(evt) {
|
||||
this.bounds = null;
|
||||
var oldExtent = ol.extent.createEmpty();
|
||||
var components = this.components;
|
||||
for (var i = components.length - 1; i >= 0; --i) {
|
||||
var component = components[i];
|
||||
ol.extent.extend(oldExtent,
|
||||
component === evt.target && !goog.isNull(evt.oldExtent) ?
|
||||
evt.oldExtent : component.getBounds());
|
||||
}
|
||||
this.dispatchEvent(new ol.geom.GeometryEvent(goog.events.EventType.CHANGE,
|
||||
this, oldExtent));
|
||||
this.dispatchChangeEvent();
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
goog.provide('ol.geom.Geometry');
|
||||
goog.provide('ol.geom.GeometryEvent');
|
||||
goog.provide('ol.geom.GeometryType');
|
||||
|
||||
goog.require('goog.events.Event');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('goog.object');
|
||||
goog.require('ol.Extent');
|
||||
goog.require('ol.Observable');
|
||||
@@ -75,18 +74,9 @@ ol.geom.Geometry.prototype.getType = goog.abstractMethod;
|
||||
ol.geom.Geometry.prototype.transform = goog.abstractMethod;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructor for geometry events.
|
||||
* @constructor
|
||||
* @extends {goog.events.Event}
|
||||
* @param {string} type Event type.
|
||||
* @param {ol.geom.Geometry} target The target geometry.
|
||||
* @param {ol.Extent} oldExtent The previous geometry extent.
|
||||
* Dispatch a generic event with type "change."
|
||||
*/
|
||||
ol.geom.GeometryEvent = function(type, target, oldExtent) {
|
||||
goog.base(this, type, target);
|
||||
|
||||
this.oldExtent = oldExtent;
|
||||
ol.geom.Geometry.prototype.dispatchChangeEvent = function() {
|
||||
this.dispatchEvent(goog.events.EventType.CHANGE);
|
||||
};
|
||||
goog.inherits(ol.geom.GeometryEvent, goog.events.Event);
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
goog.provide('ol.geom.LineString');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.CoordinateArray');
|
||||
goog.require('ol.coordinate');
|
||||
goog.require('ol.extent');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.GeometryEvent');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
|
||||
|
||||
@@ -116,11 +114,9 @@ ol.geom.LineString.prototype.distanceFromCoordinate = function(coordinate) {
|
||||
* @param {ol.CoordinateArray} coordinates Coordinates array.
|
||||
*/
|
||||
ol.geom.LineString.prototype.setCoordinates = function(coordinates) {
|
||||
var oldBounds = this.bounds_;
|
||||
this.bounds_ = null;
|
||||
this.coordinates_ = coordinates;
|
||||
this.dispatchEvent(new ol.geom.GeometryEvent(goog.events.EventType.CHANGE,
|
||||
this, oldBounds));
|
||||
this.dispatchChangeEvent();
|
||||
};
|
||||
|
||||
|
||||
@@ -134,5 +130,5 @@ ol.geom.LineString.prototype.transform = function(transform) {
|
||||
coord = coordinates[i];
|
||||
transform(coord, coord, coord.length);
|
||||
}
|
||||
this.setCoordinates(coordinates); // for change event
|
||||
this.setCoordinates(coordinates); // for invalidating bounds
|
||||
};
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
goog.provide('ol.geom.Point');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.Coordinate');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.GeometryEvent');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
|
||||
|
||||
@@ -79,11 +77,9 @@ ol.geom.Point.prototype.getType = function() {
|
||||
* @param {ol.Coordinate} coordinates Coordinates array.
|
||||
*/
|
||||
ol.geom.Point.prototype.setCoordinates = function(coordinates) {
|
||||
var oldBounds = this.bounds_;
|
||||
this.bounds_ = null;
|
||||
this.coordinates_ = coordinates;
|
||||
this.dispatchEvent(new ol.geom.GeometryEvent(goog.events.EventType.CHANGE,
|
||||
this, oldBounds));
|
||||
this.dispatchChangeEvent();
|
||||
};
|
||||
|
||||
|
||||
@@ -93,5 +89,5 @@ ol.geom.Point.prototype.setCoordinates = function(coordinates) {
|
||||
ol.geom.Point.prototype.transform = function(transform) {
|
||||
var coordinates = this.getCoordinates();
|
||||
transform(coordinates, coordinates, coordinates.length);
|
||||
this.setCoordinates(coordinates); // for change event
|
||||
this.setCoordinates(coordinates); // for invalidating bounds
|
||||
};
|
||||
|
||||
@@ -6,7 +6,6 @@ goog.require('goog.events.EventType');
|
||||
goog.require('ol.CoordinateArray');
|
||||
goog.require('ol.extent');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.GeometryEvent');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.LinearRing');
|
||||
|
||||
@@ -108,19 +107,11 @@ ol.geom.Polygon.prototype.getRings = function() {
|
||||
|
||||
/**
|
||||
* Listener for ring change events.
|
||||
* @param {ol.geom.GeometryEvent} evt Geometry event.
|
||||
* @param {goog.events.Event} evt Change event.
|
||||
* @private
|
||||
*/
|
||||
ol.geom.Polygon.prototype.handleRingChange_ = function(evt) {
|
||||
var ring = evt.target;
|
||||
var oldExtent = null;
|
||||
if (ring === this.rings_[0]) {
|
||||
oldExtent = evt.oldExtent;
|
||||
} else {
|
||||
oldExtent = this.getBounds();
|
||||
}
|
||||
this.dispatchEvent(new ol.geom.GeometryEvent(goog.events.EventType.CHANGE,
|
||||
this, oldExtent));
|
||||
this.dispatchChangeEvent();
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -26,27 +26,6 @@ describe('ol.geom.Geometry', function() {
|
||||
|
||||
});
|
||||
|
||||
describe('ol.geom.GeometryEvent', function() {
|
||||
|
||||
describe('constructor', function() {
|
||||
|
||||
it('creates a new event', function() {
|
||||
var point = new ol.geom.Point([1, 2]);
|
||||
var bounds = point.getBounds();
|
||||
var evt = new ol.geom.GeometryEvent('change', point, bounds);
|
||||
expect(evt).to.be.a(ol.geom.GeometryEvent);
|
||||
expect(evt).to.be.a(goog.events.Event);
|
||||
expect(evt.target).to.be(point);
|
||||
expect(evt.oldExtent).to.be(bounds);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
goog.require('goog.events.Event');
|
||||
goog.require('ol.Observable');
|
||||
goog.require('ol.geom.Geometry');
|
||||
goog.require('ol.geom.GeometryEvent');
|
||||
goog.require('ol.geom.Point');
|
||||
goog.require('ol.geom.LineString');
|
||||
|
||||
@@ -53,7 +53,6 @@ describe('ol.geom.LineString', function() {
|
||||
expect(line.getBounds()).to.eql([10, 20, 30, 40]);
|
||||
goog.events.listen(line, 'change', function(evt) {
|
||||
expect(evt.target).to.equal(line);
|
||||
expect(evt.oldExtent).to.eql([10, 20, 30, 40]);
|
||||
expect(evt.target.getBounds()).to.eql([30, 40, 50, 60]);
|
||||
expect(evt.target.getCoordinates()).to.eql([[30, 40], [50, 60]]);
|
||||
done();
|
||||
|
||||
@@ -81,10 +81,8 @@ describe('ol.geom.MultiPolygon', function() {
|
||||
it('is fired when outer ring is modified', function(done) {
|
||||
var multi = new ol.geom.MultiPolygon([[outer, inner], [outer, inner]]);
|
||||
var components = multi.getComponents();
|
||||
var bounds = multi.getBounds();
|
||||
goog.events.listen(multi, 'change', function(evt) {
|
||||
expect(evt.target).to.be(multi);
|
||||
expect(evt.oldExtent).to.eql(bounds);
|
||||
expect(evt.target.getBounds()).to.eql([0, 0, 11, 10]);
|
||||
done();
|
||||
});
|
||||
@@ -98,10 +96,8 @@ describe('ol.geom.MultiPolygon', function() {
|
||||
it('is fired when inner ring is modified', function(done) {
|
||||
var multi = new ol.geom.MultiPolygon([[outer, inner], [outer, inner]]);
|
||||
var components = multi.getComponents();
|
||||
var bounds = multi.getBounds();
|
||||
goog.events.listen(multi, 'change', function(evt) {
|
||||
expect(evt.target).to.be(multi);
|
||||
expect(evt.oldExtent).to.eql(bounds);
|
||||
expect(evt.target.getBounds()).to.eql([0, 0, 10, 10]);
|
||||
done();
|
||||
});
|
||||
|
||||
@@ -53,7 +53,6 @@ describe('ol.geom.Point', function() {
|
||||
expect(point.getBounds()).to.eql([10, 20, 10, 20]);
|
||||
goog.events.listen(point, 'change', function(evt) {
|
||||
expect(evt.target).to.equal(point);
|
||||
expect(evt.oldExtent).to.eql([10, 20, 10, 20]);
|
||||
expect(evt.target.getBounds()).to.eql([30, 40, 30, 40]);
|
||||
expect(evt.target.getCoordinates()).to.eql([30, 40]);
|
||||
done();
|
||||
|
||||
@@ -143,10 +143,8 @@ describe('ol.geom.Polygon', function() {
|
||||
it('is fired when outer ring is modified', function(done) {
|
||||
var poly = new ol.geom.Polygon([outer, inner]);
|
||||
var rings = poly.getRings();
|
||||
var bounds = poly.getBounds();
|
||||
goog.events.listen(poly, 'change', function(evt) {
|
||||
expect(evt.target).to.be(poly);
|
||||
expect(evt.oldExtent).to.eql(bounds);
|
||||
expect(evt.target.getBounds()).to.eql([0, 0, 11, 10]);
|
||||
done();
|
||||
});
|
||||
@@ -159,10 +157,8 @@ describe('ol.geom.Polygon', function() {
|
||||
it('is fired when inner ring is modified', function(done) {
|
||||
var poly = new ol.geom.Polygon([outer, inner]);
|
||||
var rings = poly.getRings();
|
||||
var bounds = poly.getBounds();
|
||||
goog.events.listen(poly, 'change', function(evt) {
|
||||
expect(evt.target).to.be(poly);
|
||||
expect(evt.oldExtent).to.eql(bounds);
|
||||
expect(evt.target.getBounds()).to.eql([0, 0, 10, 10]);
|
||||
done();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user