OpenLayers OpenLayers

Changeset 6512

Show
Ignore:
Timestamp:
03/12/08 18:24:33 (10 months ago)
Author:
tschaub
Message:

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)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/BaseTypes.js

    r6313 r6512  
    107107     *     to the tokens in the format string.  If no context is sent, the 
    108108     *     window object will be used. 
     109     * args - {Array} Optional arguments to pass to any functions found in 
     110     *     the context.  If a context property is a function, the token 
     111     *     will be replaced by the return from the function called with 
     112     *     these arguments. 
    109113     * 
    110114     * Returns: 
    111115     * {String} A string with tokens replaced from the context object. 
    112116     */ 
    113     format: function(template, context) { 
     117    format: function(template, context, args) { 
    114118        if(!context) { 
    115119            context = window; 
    116120        } 
    117121        var tokens = template.split("${"); 
    118         var item, last
     122        var item, last, replacement
    119123        for(var i=1; i<tokens.length; i++) { 
    120124            item = tokens[i]; 
    121125            last = item.indexOf("}");  
    122             if(last > 0) {  
    123                 tokens[i] = context[item.substring(0, last)] + 
    124                             item.substring(++last);  
     126            if(last > 0) { 
     127                replacement = context[item.substring(0, last)]; 
     128                if(typeof replacement == "function") { 
     129                    replacement = args ? 
     130                        replacement.apply(null, args) : 
     131                        replacement(); 
     132                } 
     133                tokens[i] = replacement + item.substring(++last);  
    125134            } else { 
    126135                tokens[i] = "${" + item; 
  • trunk/openlayers/lib/OpenLayers/Style.js

    r6505 r6512  
    197197         
    198198        for (var i in this.propertyStyles) { 
    199             style[i] = OpenLayers.Style.createLiteral(style[i], context); 
     199            style[i] = OpenLayers.Style.createLiteral(style[i], context, feature); 
    200200        } 
    201201        return style; 
     
    316316 *  
    317317 * Parameters: 
    318  * value {String} value to parse. If this string contains a construct like 
     318 * value - {String} value to parse. If this string contains a construct like 
    319319 *         "foo ${bar}", then "foo " will be taken as literal, and "${bar}" 
    320320 *         will be replaced by the value of the "bar" attribute of the passed 
    321321 *         feature. 
    322  * context {Object} context to take attribute values from 
     322 * context - {Object} context to take attribute values from 
     323 * feature - {OpenLayers.Feature.Vector} The feature that will be passed 
     324 *     to <OpenLayers.String.format> for evaluating functions in the context. 
    323325 *  
    324326 * Returns: 
     
    327329 * attribute named "bar" with the value "valueOfBar". 
    328330 */ 
    329 OpenLayers.Style.createLiteral = function(value, context) { 
     331OpenLayers.Style.createLiteral = function(value, context, feature) { 
    330332    if (typeof value == "string" && value.indexOf("${") != -1) { 
    331         value = OpenLayers.String.format(value, context) 
     333        value = OpenLayers.String.format(value, context, [feature]); 
    332334        value = isNaN(value) ? value : parseFloat(value); 
    333335    } 
  • trunk/openlayers/tests/test_BaseTypes.html

    r6131 r6512  
    9090            "}", "${${} }" 
    9191        ] 
    92         t.plan(4 + unchanged.length); 
     92        t.plan(6 + unchanged.length); 
    9393 
    9494        var format = OpenLayers.String.format; 
     
    115115        t.eq(format("a ${bar} is a ${foo}", context), "a foo is a bar", 
    116116             "multiple properties replaced correctly"); 
     117         
     118        // test context with properties that are functions 
     119        var context = { 
     120            bar: "church", 
     121            getDrunk: function() { 
     122                return arguments[0]; 
     123            } 
     124        }; 
     125        t.eq( 
     126            format("I go to the ${bar} to ${getDrunk}.", context, ["eat pretzels"]), 
     127            "I go to the church to eat pretzels.", 
     128            "function correctly called in context with arguments" 
     129        ); 
     130         
     131        // test that things don't break 
     132        var context = { 
     133            meaning: function(truth) { 
     134                return truth; 
     135            } 
     136        }; 
     137        t.eq( 
     138            format("In life, truth is ${meaning}.", context), 
     139            "In life, truth is undefined.", 
     140            "still works if arguments are not supplied" 
     141        ); 
    117142 
    118143    }