Change event for polygons
This commit is contained in:
+26
-2
@@ -1,9 +1,12 @@
|
|||||||
goog.provide('ol.geom.Polygon');
|
goog.provide('ol.geom.Polygon');
|
||||||
|
|
||||||
goog.require('goog.asserts');
|
goog.require('goog.asserts');
|
||||||
|
goog.require('goog.events');
|
||||||
|
goog.require('goog.events.EventType');
|
||||||
goog.require('ol.CoordinateArray');
|
goog.require('ol.CoordinateArray');
|
||||||
goog.require('ol.extent');
|
goog.require('ol.extent');
|
||||||
goog.require('ol.geom.Geometry');
|
goog.require('ol.geom.Geometry');
|
||||||
|
goog.require('ol.geom.GeometryEvent');
|
||||||
goog.require('ol.geom.GeometryType');
|
goog.require('ol.geom.GeometryType');
|
||||||
goog.require('ol.geom.LinearRing');
|
goog.require('ol.geom.LinearRing');
|
||||||
|
|
||||||
@@ -38,7 +41,7 @@ ol.geom.Polygon = function(coordinates) {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
this.rings_ = new Array(numRings);
|
this.rings_ = new Array(numRings);
|
||||||
var ringCoords;
|
var ringCoords, ring;
|
||||||
for (var i = 0; i < numRings; ++i) {
|
for (var i = 0; i < numRings; ++i) {
|
||||||
ringCoords = coordinates[i];
|
ringCoords = coordinates[i];
|
||||||
if (i === 0) {
|
if (i === 0) {
|
||||||
@@ -52,7 +55,10 @@ ol.geom.Polygon = function(coordinates) {
|
|||||||
ringCoords.reverse();
|
ringCoords.reverse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.rings_[i] = new ol.geom.LinearRing(ringCoords);
|
ring = new ol.geom.LinearRing(ringCoords);
|
||||||
|
goog.events.listen(ring, goog.events.EventType.CHANGE,
|
||||||
|
this.handleRingChange_, false, this);
|
||||||
|
this.rings_[i] = ring;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -98,6 +104,24 @@ ol.geom.Polygon.prototype.getRings = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listener for ring change events.
|
||||||
|
* @param {ol.geom.GeometryEvent} evt Geometry 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));
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether a given coordinate is inside this polygon. Note that this is a
|
* Check whether a given coordinate is inside this polygon. Note that this is a
|
||||||
* fast and simple check - points on an edge or vertex of the polygon or one of
|
* fast and simple check - points on an edge or vertex of the polygon or one of
|
||||||
|
|||||||
@@ -132,8 +132,51 @@ describe('ol.geom.Polygon', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('change event', function() {
|
||||||
|
|
||||||
|
var outer, inner;
|
||||||
|
beforeEach(function() {
|
||||||
|
outer = [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]];
|
||||||
|
inner = [[2, 2], [2, 8], [8, 8], [8, 2], [2, 2]];
|
||||||
|
});
|
||||||
|
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
|
||||||
|
var outerCoords = rings[0].getCoordinates();
|
||||||
|
outerCoords[1][0] = 11;
|
||||||
|
rings[0].setCoordinates(outerCoords);
|
||||||
|
});
|
||||||
|
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
|
||||||
|
var innerCoords = rings[1].getCoordinates();
|
||||||
|
innerCoords[1][0] = 3;
|
||||||
|
rings[1].setCoordinates(innerCoords);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
goog.require('goog.events');
|
||||||
goog.require('ol.geom.Geometry');
|
goog.require('ol.geom.Geometry');
|
||||||
goog.require('ol.geom.LinearRing');
|
goog.require('ol.geom.LinearRing');
|
||||||
goog.require('ol.geom.Polygon');
|
goog.require('ol.geom.Polygon');
|
||||||
|
|||||||
Reference in New Issue
Block a user