[OpenLayers-Commits] r5163 - sandbox/ahocevar/sldRenderer/lib/OpenLayers

commits at openlayers.org commits at openlayers.org
Fri Nov 9 17:01:00 EST 2007


Author: ahocevar
Date: 2007-11-09 17:00:59 -0500 (Fri, 09 Nov 2007)
New Revision: 5163

Modified:
   sandbox/ahocevar/sldRenderer/lib/OpenLayers/BaseTypes.js
   sandbox/ahocevar/sldRenderer/lib/OpenLayers/Style.js
Log:
applied patch proposed in #1133; modified OpenLayers.Style.createLiteral to use the new method OpenLayers.String.format

Modified: sandbox/ahocevar/sldRenderer/lib/OpenLayers/BaseTypes.js
===================================================================
--- sandbox/ahocevar/sldRenderer/lib/OpenLayers/BaseTypes.js	2007-11-09 20:13:31 UTC (rev 5162)
+++ sandbox/ahocevar/sldRenderer/lib/OpenLayers/BaseTypes.js	2007-11-09 22:00:59 UTC (rev 5163)
@@ -87,7 +87,42 @@
             camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
         }
         return camelizedString;
+    },
+
+    /**
+     * APIFunction: OpenLayers.String.format
+     * Given a string with tokens in the form ${token}, return a string
+     *     with tokens replaced with properties from the given context
+     *     object.  Represent a literal "${" by escaping the curly brace.
+     *     E.g. "$\{".
+     *
+     * Parameters:
+     * template - {String} A string with tokens to be replaced.  A template
+     *     has the form "literal ${token}" where the token will be replaced
+     *     by the value of context["token"].
+     * context - {Object} An optional object with properties corresponding
+     *     to the tokens in the format string.  If no context is sent, the
+     *     window object will be used.
+     *
+     * Returns:
+     * {String} A string with tokens replaced from the context object.
+     */
+    format: function(template, context) {
+        if(!context) {
+            context = window;
+        }
+        var tokens = template.split("${");
+        var last;
+        for(var i=0; i<tokens.length; i++) { 
+            var last = tokens[i].indexOf("}"); 
+            if(last != -1) { 
+                tokens[i] = context[tokens[i].substring(0, last)] +
+                            tokens[i].substring(++last); 
+            }
+        }
+        return tokens.join("");
     }
+
 };
 
 if (!String.prototype.startsWith) {

Modified: sandbox/ahocevar/sldRenderer/lib/OpenLayers/Style.js
===================================================================
--- sandbox/ahocevar/sldRenderer/lib/OpenLayers/Style.js	2007-11-09 20:13:31 UTC (rev 5162)
+++ sandbox/ahocevar/sldRenderer/lib/OpenLayers/Style.js	2007-11-09 22:00:59 UTC (rev 5163)
@@ -226,15 +226,7 @@
 OpenLayers.Style.createLiteral = function(value, feature) {
     if (typeof value == "string" && value.indexOf("${") != -1) {
         var attributes = feature.attributes || feature.data;
-        var tokens = value.split("${");
-        for (var i=0; i<tokens.length; i++) {
-            var close = tokens[i].indexOf("}");
-            if (close != -1) {
-                tokens[i] = attributes[tokens[i].substring(0, close)] +
-                        tokens[i].substring(++close);
-            }
-        }
-        value = tokens.join(""); 
+        value = OpenLayers.String.format(value, attributes)
         value = isNaN(value) ? value : parseFloat(value);
     }
     return value;



More information about the Commits mailing list