(Closes #2146) enhance createLiterals to merge context, attributes and pass attribute name through to context functions

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9676 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Paul Spencer
2009-09-17 17:17:11 +00:00
parent 507fd0b86d
commit 6088de516c
2 changed files with 23 additions and 7 deletions

View File

@@ -256,10 +256,11 @@ OpenLayers.Style = OpenLayers.Class({
* {Object} the modified style * {Object} the modified style
*/ */
createLiterals: function(style, feature) { createLiterals: function(style, feature) {
var context = this.context || feature.attributes || feature.data; var context = OpenLayers.Util.extend({}, feature.attributes || feature.data);
OpenLayers.Util.extend(context, this.context);
for (var i in this.propertyStyles) { for (var i in this.propertyStyles) {
style[i] = OpenLayers.Style.createLiteral(style[i], context, feature); style[i] = OpenLayers.Style.createLiteral(style[i], context, feature, i);
} }
return style; return style;
}, },
@@ -383,17 +384,20 @@ OpenLayers.Style = OpenLayers.Class({
* will be replaced by the value of the "bar" attribute of the passed * will be replaced by the value of the "bar" attribute of the passed
* feature. * feature.
* context - {Object} context to take attribute values from * context - {Object} context to take attribute values from
* feature - {OpenLayers.Feature.Vector} The feature that will be passed * feature - {<OpenLayers.Feature.Vector>} optional feature to pass to
* to <OpenLayers.String.format> for evaluating functions in the context. * <OpenLayers.String.format> for evaluating functions in the
* context.
* property - {String} optional, name of the property for which the literal is
* being created for evaluating functions in the context.
* *
* Returns: * Returns:
* {String} the parsed value. In the example of the value parameter above, the * {String} the parsed value. In the example of the value parameter above, the
* result would be "foo valueOfBar", assuming that the passed feature has an * result would be "foo valueOfBar", assuming that the passed feature has an
* attribute named "bar" with the value "valueOfBar". * attribute named "bar" with the value "valueOfBar".
*/ */
OpenLayers.Style.createLiteral = function(value, context, feature) { OpenLayers.Style.createLiteral = function(value, context, feature, property) {
if (typeof value == "string" && value.indexOf("${") != -1) { if (typeof value == "string" && value.indexOf("${") != -1) {
value = OpenLayers.String.format(value, context, [feature]); value = OpenLayers.String.format(value, context, [feature, property]);
value = (isNaN(value) || !value) ? value : parseFloat(value); value = (isNaN(value) || !value) ? value : parseFloat(value);
} }
return value; return value;

View File

@@ -237,7 +237,7 @@
} }
function test_Style_context(t) { function test_Style_context(t) {
t.plan(2); t.plan(4);
var rule = new OpenLayers.Rule({ var rule = new OpenLayers.Rule({
symbolizer: {"Point": {externalGraphic: "${img1}"}}, symbolizer: {"Point": {externalGraphic: "${img1}"}},
filter: new OpenLayers.Filter.Comparison({ filter: new OpenLayers.Filter.Comparison({
@@ -265,6 +265,18 @@
} }
}}); }});
t.eq(style.createSymbolizer(feature).externalGraphic, "foo10.png", "correctly evaluated symbolizer without rule"); t.eq(style.createSymbolizer(feature).externalGraphic, "foo10.png", "correctly evaluated symbolizer without rule");
style = new OpenLayers.Style(
{externalGraphic: "${getExternalGraphic}",
pointRadius: "${size}"},
{context: {
getExternalGraphic: function(feature) {
return "foo" + feature.attributes.size + ".png";
}
}});
t.eq(style.createSymbolizer(feature).externalGraphic, "foo10.png", "correctly evaluated symbolizer from context");
t.eq(style.createSymbolizer(feature).pointRadius, 10, "correctly evaluated symbolizer from attributes");
}; };
function test_Style_findPropertyStyles(t) { function test_Style_findPropertyStyles(t) {