Changeset 6512
- Timestamp:
- 03/12/08 18:24:33 (10 months ago)
- Files:
-
- trunk/openlayers/examples/styles-context.html (added)
- trunk/openlayers/lib/OpenLayers/BaseTypes.js (modified) (1 diff)
- trunk/openlayers/lib/OpenLayers/Style.js (modified) (3 diffs)
- trunk/openlayers/tests/test_BaseTypes.html (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openlayers/lib/OpenLayers/BaseTypes.js
r6313 r6512 107 107 * to the tokens in the format string. If no context is sent, the 108 108 * 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. 109 113 * 110 114 * Returns: 111 115 * {String} A string with tokens replaced from the context object. 112 116 */ 113 format: function(template, context ) {117 format: function(template, context, args) { 114 118 if(!context) { 115 119 context = window; 116 120 } 117 121 var tokens = template.split("${"); 118 var item, last ;122 var item, last, replacement; 119 123 for(var i=1; i<tokens.length; i++) { 120 124 item = tokens[i]; 121 125 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); 125 134 } else { 126 135 tokens[i] = "${" + item; trunk/openlayers/lib/OpenLayers/Style.js
r6505 r6512 197 197 198 198 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); 200 200 } 201 201 return style; … … 316 316 * 317 317 * Parameters: 318 * value {String} value to parse. If this string contains a construct like318 * value - {String} value to parse. If this string contains a construct like 319 319 * "foo ${bar}", then "foo " will be taken as literal, and "${bar}" 320 320 * will be replaced by the value of the "bar" attribute of the passed 321 321 * 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. 323 325 * 324 326 * Returns: … … 327 329 * attribute named "bar" with the value "valueOfBar". 328 330 */ 329 OpenLayers.Style.createLiteral = function(value, context ) {331 OpenLayers.Style.createLiteral = function(value, context, feature) { 330 332 if (typeof value == "string" && value.indexOf("${") != -1) { 331 value = OpenLayers.String.format(value, context )333 value = OpenLayers.String.format(value, context, [feature]); 332 334 value = isNaN(value) ? value : parseFloat(value); 333 335 } trunk/openlayers/tests/test_BaseTypes.html
r6131 r6512 90 90 "}", "${${} }" 91 91 ] 92 t.plan( 4+ unchanged.length);92 t.plan(6 + unchanged.length); 93 93 94 94 var format = OpenLayers.String.format; … … 115 115 t.eq(format("a ${bar} is a ${foo}", context), "a foo is a bar", 116 116 "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 ); 117 142 118 143 }
