Merge pull request #11481 from michalzielanski/additional-props-in-cloned-geom
Adding original properties to cloned geometry
This commit is contained in:
@@ -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.
|
||||
* @param {string} key Key name.
|
||||
|
||||
@@ -37,7 +37,13 @@ class Circle extends SimpleGeometry {
|
||||
* @api
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,6 +69,7 @@ class GeometryCollection extends Geometry {
|
||||
clone() {
|
||||
const geometryCollection = new GeometryCollection(null);
|
||||
geometryCollection.setGeometries(this.geometries_);
|
||||
geometryCollection.applyProperties(this);
|
||||
return geometryCollection;
|
||||
}
|
||||
|
||||
|
||||
@@ -87,7 +87,12 @@ class LineString extends SimpleGeometry {
|
||||
* @api
|
||||
*/
|
||||
clone() {
|
||||
return new LineString(this.flatCoordinates.slice(), this.layout);
|
||||
const lineString = new LineString(
|
||||
this.flatCoordinates.slice(),
|
||||
this.layout
|
||||
);
|
||||
lineString.applyProperties(this);
|
||||
return lineString;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -102,11 +102,13 @@ class MultiLineString extends SimpleGeometry {
|
||||
* @api
|
||||
*/
|
||||
clone() {
|
||||
return new MultiLineString(
|
||||
const multiLineString = new MultiLineString(
|
||||
this.flatCoordinates.slice(),
|
||||
this.layout,
|
||||
this.ends_.slice()
|
||||
);
|
||||
multiLineString.applyProperties(this);
|
||||
return multiLineString;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -61,6 +61,7 @@ class MultiPoint extends SimpleGeometry {
|
||||
this.flatCoordinates.slice(),
|
||||
this.layout
|
||||
);
|
||||
multiPoint.applyProperties(this);
|
||||
return multiPoint;
|
||||
}
|
||||
|
||||
|
||||
@@ -155,11 +155,14 @@ class MultiPolygon extends SimpleGeometry {
|
||||
newEndss[i] = this.endss_[i].slice();
|
||||
}
|
||||
|
||||
return new MultiPolygon(
|
||||
const multiPolygon = new MultiPolygon(
|
||||
this.flatCoordinates.slice(),
|
||||
this.layout,
|
||||
newEndss
|
||||
);
|
||||
multiPolygon.applyProperties(this);
|
||||
|
||||
return multiPolygon;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -30,6 +30,7 @@ class Point extends SimpleGeometry {
|
||||
*/
|
||||
clone() {
|
||||
const point = new Point(this.flatCoordinates.slice(), this.layout);
|
||||
point.applyProperties(this);
|
||||
return point;
|
||||
}
|
||||
|
||||
|
||||
@@ -118,11 +118,13 @@ class Polygon extends SimpleGeometry {
|
||||
* @api
|
||||
*/
|
||||
clone() {
|
||||
return new Polygon(
|
||||
const polygon = new Polygon(
|
||||
this.flatCoordinates.slice(),
|
||||
this.layout,
|
||||
this.ends_.slice()
|
||||
);
|
||||
polygon.applyProperties(this);
|
||||
return polygon;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -9,11 +9,14 @@ describe('ol.geom.Circle', function () {
|
||||
|
||||
describe('#clone', function () {
|
||||
it('returns a clone', function () {
|
||||
circle.setProperties({foo: 'bar', baz: null});
|
||||
|
||||
const clone = circle.clone();
|
||||
expect(clone).to.be.an(Circle);
|
||||
expect(clone.getCenter()).to.eql(circle.getCenter());
|
||||
expect(clone.getCenter()).not.to.be(circle.getCenter());
|
||||
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 multi = new GeometryCollection([point, line, poly]);
|
||||
multi.setProperties({foo: 'bar', baz: null});
|
||||
const clone = multi.clone();
|
||||
expect(clone).to.not.be(multi);
|
||||
const geometries = clone.getGeometries();
|
||||
@@ -106,6 +107,7 @@ describe('ol.geom.GeometryCollection', function () {
|
||||
[30, 40],
|
||||
]);
|
||||
expect(geometries[2].getCoordinates()).to.eql([outer, inner1, inner2]);
|
||||
expect(clone.getProperties()).to.eql({foo: 'bar', baz: null});
|
||||
});
|
||||
|
||||
it('does a deep clone', function () {
|
||||
|
||||
@@ -276,8 +276,11 @@ describe('ol.geom.MultiPolygon', function () {
|
||||
|
||||
describe('#clone()', function () {
|
||||
it('has the expected endss_', function () {
|
||||
multiPolygon.setProperties({foo: 'bar', baz: null});
|
||||
|
||||
const clone = multiPolygon.clone();
|
||||
expect(multiPolygon.endss_).to.eql(clone.endss_);
|
||||
expect(clone.getProperties()).to.eql({foo: 'bar', baz: null});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user