Merge pull request #11481 from michalzielanski/additional-props-in-cloned-geom

Adding original properties to cloned geometry
This commit is contained in:
Tim Schaub
2020-08-27 08:23:46 -06:00
committed by GitHub
12 changed files with 46 additions and 5 deletions
+12
View File
@@ -188,6 +188,18 @@ class BaseObject extends Observable {
} }
} }
/**
* Apply any properties from another object without triggering events.
* @param {BaseObject} source The source object.
* @protected
*/
applyProperties(source) {
if (!source.values_) {
return;
}
assign(this.values_ || (this.values_ = {}), source.values_);
}
/** /**
* Unsets a property. * Unsets a property.
* @param {string} key Key name. * @param {string} key Key name.
+7 -1
View File
@@ -37,7 +37,13 @@ class Circle extends SimpleGeometry {
* @api * @api
*/ */
clone() { clone() {
return new Circle(this.flatCoordinates.slice(), undefined, this.layout); const circle = new Circle(
this.flatCoordinates.slice(),
undefined,
this.layout
);
circle.applyProperties(this);
return circle;
} }
/** /**
+1
View File
@@ -69,6 +69,7 @@ class GeometryCollection extends Geometry {
clone() { clone() {
const geometryCollection = new GeometryCollection(null); const geometryCollection = new GeometryCollection(null);
geometryCollection.setGeometries(this.geometries_); geometryCollection.setGeometries(this.geometries_);
geometryCollection.applyProperties(this);
return geometryCollection; return geometryCollection;
} }
+6 -1
View File
@@ -87,7 +87,12 @@ class LineString extends SimpleGeometry {
* @api * @api
*/ */
clone() { clone() {
return new LineString(this.flatCoordinates.slice(), this.layout); const lineString = new LineString(
this.flatCoordinates.slice(),
this.layout
);
lineString.applyProperties(this);
return lineString;
} }
/** /**
+3 -1
View File
@@ -102,11 +102,13 @@ class MultiLineString extends SimpleGeometry {
* @api * @api
*/ */
clone() { clone() {
return new MultiLineString( const multiLineString = new MultiLineString(
this.flatCoordinates.slice(), this.flatCoordinates.slice(),
this.layout, this.layout,
this.ends_.slice() this.ends_.slice()
); );
multiLineString.applyProperties(this);
return multiLineString;
} }
/** /**
+1
View File
@@ -61,6 +61,7 @@ class MultiPoint extends SimpleGeometry {
this.flatCoordinates.slice(), this.flatCoordinates.slice(),
this.layout this.layout
); );
multiPoint.applyProperties(this);
return multiPoint; return multiPoint;
} }
+4 -1
View File
@@ -155,11 +155,14 @@ class MultiPolygon extends SimpleGeometry {
newEndss[i] = this.endss_[i].slice(); newEndss[i] = this.endss_[i].slice();
} }
return new MultiPolygon( const multiPolygon = new MultiPolygon(
this.flatCoordinates.slice(), this.flatCoordinates.slice(),
this.layout, this.layout,
newEndss newEndss
); );
multiPolygon.applyProperties(this);
return multiPolygon;
} }
/** /**
+1
View File
@@ -30,6 +30,7 @@ class Point extends SimpleGeometry {
*/ */
clone() { clone() {
const point = new Point(this.flatCoordinates.slice(), this.layout); const point = new Point(this.flatCoordinates.slice(), this.layout);
point.applyProperties(this);
return point; return point;
} }
+3 -1
View File
@@ -118,11 +118,13 @@ class Polygon extends SimpleGeometry {
* @api * @api
*/ */
clone() { clone() {
return new Polygon( const polygon = new Polygon(
this.flatCoordinates.slice(), this.flatCoordinates.slice(),
this.layout, this.layout,
this.ends_.slice() this.ends_.slice()
); );
polygon.applyProperties(this);
return polygon;
} }
/** /**
+3
View File
@@ -9,11 +9,14 @@ describe('ol.geom.Circle', function () {
describe('#clone', function () { describe('#clone', function () {
it('returns a clone', function () { it('returns a clone', function () {
circle.setProperties({foo: 'bar', baz: null});
const clone = circle.clone(); const clone = circle.clone();
expect(clone).to.be.an(Circle); expect(clone).to.be.an(Circle);
expect(clone.getCenter()).to.eql(circle.getCenter()); expect(clone.getCenter()).to.eql(circle.getCenter());
expect(clone.getCenter()).not.to.be(circle.getCenter()); expect(clone.getCenter()).not.to.be(circle.getCenter());
expect(clone.getRadius()).to.be(circle.getRadius()); expect(clone.getRadius()).to.be(circle.getRadius());
expect(clone.getProperties()).to.eql({foo: 'bar', baz: null});
}); });
}); });
@@ -97,6 +97,7 @@ describe('ol.geom.GeometryCollection', function () {
]); ]);
const poly = new Polygon([outer, inner1, inner2]); const poly = new Polygon([outer, inner1, inner2]);
const multi = new GeometryCollection([point, line, poly]); const multi = new GeometryCollection([point, line, poly]);
multi.setProperties({foo: 'bar', baz: null});
const clone = multi.clone(); const clone = multi.clone();
expect(clone).to.not.be(multi); expect(clone).to.not.be(multi);
const geometries = clone.getGeometries(); const geometries = clone.getGeometries();
@@ -106,6 +107,7 @@ describe('ol.geom.GeometryCollection', function () {
[30, 40], [30, 40],
]); ]);
expect(geometries[2].getCoordinates()).to.eql([outer, inner1, inner2]); expect(geometries[2].getCoordinates()).to.eql([outer, inner1, inner2]);
expect(clone.getProperties()).to.eql({foo: 'bar', baz: null});
}); });
it('does a deep clone', function () { it('does a deep clone', function () {
+3
View File
@@ -276,8 +276,11 @@ describe('ol.geom.MultiPolygon', function () {
describe('#clone()', function () { describe('#clone()', function () {
it('has the expected endss_', function () { it('has the expected endss_', function () {
multiPolygon.setProperties({foo: 'bar', baz: null});
const clone = multiPolygon.clone(); const clone = multiPolygon.clone();
expect(multiPolygon.endss_).to.eql(clone.endss_); expect(multiPolygon.endss_).to.eql(clone.endss_);
expect(clone.getProperties()).to.eql({foo: 'bar', baz: null});
}); });
}); });