Give Layer.Vector a proper clone() method. r=crschmidt (closes #2391)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9942 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
ahocevar
2010-01-09 12:01:45 +00:00
parent 44ba385429
commit 88b05f55a1
2 changed files with 42 additions and 0 deletions

View File

@@ -302,6 +302,36 @@ OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
OpenLayers.Layer.prototype.destroy.apply(this, arguments);
},
/**
* Method: clone
* Create a clone of this layer.
*
* Note: Features of the layer are also cloned.
*
* Returns:
* {<OpenLayers.Layer.Vector>} An exact clone of this layer
*/
clone: function (obj) {
if (obj == null) {
obj = new OpenLayers.Layer.Vector(this.name, this.options);
}
//get all additions from superclasses
obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]);
// copy/set any non-init, non-simple values here
var features = this.features;
var len = features.length;
var clonedFeatures = new Array(len);
for(var i=0; i<len; ++i) {
clonedFeatures[i] = features[i].clone();
}
obj.features = clonedFeatures;
return obj;
},
/**
* Method: refresh
* Ask the layer to request features again and redraw them. Triggers

View File

@@ -427,6 +427,18 @@
}
}
function test_Layer_Vector_clone(t) {
t.plan(5);
var original = new OpenLayers.Layer.Vector(name, {dummyOption: "foo"});
original.addFeatures([new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(1,2), {foo: "bar"})]);
var clone = original.clone();
t.ok(clone instanceof OpenLayers.Layer.Vector, "clone is an instance of OpenLayers.Layer.Vector");
t.ok(clone.name, original.name, "clone has the same name as the original");
t.ok(clone.features[0] != original.features[0], "clone's feature does not equal the original's feature");
t.eq(clone.features[0].attributes.foo, original.features[0].attributes.foo, "clone's feature has the same attributes as the original's feature");
t.eq(clone.dummyOption, original.dummyOption, "clone's dummyOption equals the original's dummy option");
}
function test_Layer_Vector_externalGraphic(t) {
t.plan(11);