Style constructor accepts a rules array. These rules will be added to the style. r=ahocevar (closes #1964)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@8975 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-03-09 22:26:05 +00:00
parent a10b319413
commit 1ed1584320
2 changed files with 41 additions and 10 deletions

View File

@@ -85,21 +85,29 @@ OpenLayers.Style = OpenLayers.Class({
* used as default style for this style object. This style
* applies if no rules are specified. Symbolizers defined in
* rules will extend this default style.
* options - {Object} An optional object with properties to set on the
* userStyle
* options - {Object} An optional object with properties to set on the
* style.
*
* Valid options:
* rules - {Array(<OpenLayers.Rule>)} List of rules to be added to the
* style.
*
* Return:
* {<OpenLayers.Style>}
*/
initialize: function(style, options) {
OpenLayers.Util.extend(this, options);
this.rules = [];
if(options && options.rules) {
this.addRules(options.rules);
}
// use the default style from OpenLayers.Feature.Vector if no style
// was given in the constructor
this.setDefaultStyle(style ||
OpenLayers.Feature.Vector.style["default"]);
OpenLayers.Util.extend(this, options);
this.setDefaultStyle(style ||
OpenLayers.Feature.Vector.style["default"]);
},
/**

View File

@@ -4,13 +4,36 @@
<script type="text/javascript">
function test_Style_constructor(t) {
t.plan(3);
t.plan(6);
var options = {'foo': 'bar'};
var style = new OpenLayers.Style(null, options);
var rules = [
new OpenLayers.Rule({
symbolizer: {fillColor: "red"},
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "type",
value: "fire engine"
})
}),
new OpenLayers.Rule({
symbolizer: {fillColor: "yellow"},
filter: new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "type",
value: "sports car"
})
})
];
var style = new OpenLayers.Style(null, {
foo: "bar",
rules: rules
});
t.ok(style instanceof OpenLayers.Style,
"new OpenLayers.Style returns object" );
t.eq(style.foo, "bar", "constructor sets options correctly");
t.eq(style.foo, "bar", "constructor sets options correctly");
t.eq(style.rules.length, 2, "correct number of rules added");
t.ok(style.rules[0] === rules[0], "correct first rule added");
t.ok(style.rules[1] === rules[1], "correct second rule added");
t.eq(typeof style.createSymbolizer, "function", "style has a createSymbolizer function");
}