give Style a unique id and a clone method. r=elemoine (closes #2605)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@10315 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -15,6 +15,12 @@
|
|||||||
*/
|
*/
|
||||||
OpenLayers.Style = OpenLayers.Class({
|
OpenLayers.Style = OpenLayers.Class({
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Property: id
|
||||||
|
* {String} A unique id for this session.
|
||||||
|
*/
|
||||||
|
id: null,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* APIProperty: name
|
* APIProperty: name
|
||||||
* {String}
|
* {String}
|
||||||
@@ -119,6 +125,7 @@ OpenLayers.Style = OpenLayers.Class({
|
|||||||
this.setDefaultStyle(style ||
|
this.setDefaultStyle(style ||
|
||||||
OpenLayers.Feature.Vector.style["default"]);
|
OpenLayers.Feature.Vector.style["default"]);
|
||||||
|
|
||||||
|
this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -369,6 +376,29 @@ OpenLayers.Style = OpenLayers.Class({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIMethod: clone
|
||||||
|
* Clones this style.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* {<OpenLayers.Style>} Clone of this style.
|
||||||
|
*/
|
||||||
|
clone: function() {
|
||||||
|
var options = OpenLayers.Util.extend({}, this);
|
||||||
|
// clone rules
|
||||||
|
if(this.rules) {
|
||||||
|
options.rules = [];
|
||||||
|
for(var i=0, len=this.rules.length; i<len; ++i) {
|
||||||
|
options.rules.push(this.rules[i].clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// clone context
|
||||||
|
options.context = this.context && OpenLayers.Util.extend({}, this.context);
|
||||||
|
//clone default style
|
||||||
|
var defaultStyle = OpenLayers.Util.extend({}, this.defaultStyle);
|
||||||
|
return new OpenLayers.Style(defaultStyle, options);
|
||||||
|
},
|
||||||
|
|
||||||
CLASS_NAME: "OpenLayers.Style"
|
CLASS_NAME: "OpenLayers.Style"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -341,6 +341,44 @@
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_clone(t) {
|
||||||
|
|
||||||
|
t.plan(7);
|
||||||
|
|
||||||
|
var style = new OpenLayers.Style({bar: "baz"}, {
|
||||||
|
name: "test style",
|
||||||
|
rules: [new OpenLayers.Rule({
|
||||||
|
name: "test rule"
|
||||||
|
})],
|
||||||
|
context: {
|
||||||
|
foo: "bar"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
var clone = style.clone();
|
||||||
|
t.eq(clone.name, "test style", "name copied");
|
||||||
|
t.eq(clone.rules[0].name, "test rule", "clone has correct rule");
|
||||||
|
|
||||||
|
// modify original
|
||||||
|
style.name = "new";
|
||||||
|
style.addRules([new OpenLayers.Rule({
|
||||||
|
name: "new rule"
|
||||||
|
})]);
|
||||||
|
style.context.foo = "baz";
|
||||||
|
|
||||||
|
// confirm that clone didn't change
|
||||||
|
t.eq(clone.defaultStyle.bar, "baz", "clone has clone of defaultStyle");
|
||||||
|
t.eq(clone.name, "test style", "clone has clone of name");
|
||||||
|
t.eq(clone.rules.length, 1, "clone has clone of rules");
|
||||||
|
t.eq(clone.context.foo, "bar", "clone has clone of context");
|
||||||
|
|
||||||
|
// confirm that ids are different
|
||||||
|
t.ok(clone.id !== style.id, "clone has different id");
|
||||||
|
|
||||||
|
style.destroy();
|
||||||
|
clone.destroy();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
function test_Style_destroy(t) {
|
function test_Style_destroy(t) {
|
||||||
t.plan(1);
|
t.plan(1);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user