Adding an args argument to OpenLayers.String.format. This lets you set context properties as functions that will be executed with the given arguments where tokens match. For styles, this means you can specify a context that contains functions that return some value based on the feature being styled. See the styles-context.html example for use. r=ahocevar (closes #1434)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@6512 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2008-03-12 22:24:33 +00:00
parent 7a25a14f93
commit 106e73618a
4 changed files with 263 additions and 11 deletions
+7 -5
View File
@@ -196,7 +196,7 @@ OpenLayers.Style = OpenLayers.Class({
var context = this.context || feature.attributes || feature.data;
for (var i in this.propertyStyles) {
style[i] = OpenLayers.Style.createLiteral(style[i], context);
style[i] = OpenLayers.Style.createLiteral(style[i], context, feature);
}
return style;
},
@@ -315,20 +315,22 @@ OpenLayers.Style = OpenLayers.Class({
* into a Literal, taking the property values from the passed features.
*
* Parameters:
* value {String} value to parse. If this string contains a construct like
* value - {String} value to parse. If this string contains a construct like
* "foo ${bar}", then "foo " will be taken as literal, and "${bar}"
* will be replaced by the value of the "bar" attribute of the passed
* 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
* to <OpenLayers.String.format> for evaluating functions in the context.
*
* Returns:
* {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
* attribute named "bar" with the value "valueOfBar".
*/
OpenLayers.Style.createLiteral = function(value, context) {
OpenLayers.Style.createLiteral = function(value, context, feature) {
if (typeof value == "string" && value.indexOf("${") != -1) {
value = OpenLayers.String.format(value, context)
value = OpenLayers.String.format(value, context, [feature]);
value = isNaN(value) ? value : parseFloat(value);
}
return value;