| | 90 | }, |
|---|
| | 91 | |
|---|
| | 92 | /** |
|---|
| | 93 | * APIFunction: OpenLayers.String.format |
|---|
| | 94 | * Given a string with tokens in the form ${token}, return a string |
|---|
| | 95 | * with tokens replaced with properties from the given context |
|---|
| | 96 | * object. Represent a literal "${" by doubling it, e.g. "${${". |
|---|
| | 97 | * |
|---|
| | 98 | * Parameters: |
|---|
| | 99 | * template - {String} A string with tokens to be replaced. A template |
|---|
| | 100 | * has the form "literal ${token}" where the token will be replaced |
|---|
| | 101 | * by the value of context["token"]. |
|---|
| | 102 | * context - {Object} An optional object with properties corresponding |
|---|
| | 103 | * to the tokens in the format string. If no context is sent, the |
|---|
| | 104 | * window object will be used. |
|---|
| | 105 | * |
|---|
| | 106 | * Returns: |
|---|
| | 107 | * {String} A string with tokens replaced from the context object. |
|---|
| | 108 | */ |
|---|
| | 109 | format: function(template, context) { |
|---|
| | 110 | if(!context) { |
|---|
| | 111 | context = window; |
|---|
| | 112 | } |
|---|
| | 113 | var tokens = template.split("${"); |
|---|
| | 114 | var last; |
|---|
| | 115 | for(var i=1; i<tokens.length; i++) { |
|---|
| | 116 | var last = tokens[i].indexOf("}"); |
|---|
| | 117 | if(last != -1) { |
|---|
| | 118 | tokens[i] = context[tokens[i].substring(0, last)] + |
|---|
| | 119 | tokens[i].substring(++last); |
|---|
| | 120 | } else { |
|---|
| | 121 | if (tokens[i]) { |
|---|
| | 122 | tokens[i] = tokens[i] + "}"; |
|---|
| | 123 | } else { |
|---|
| | 124 | tokens[i] = "${" + tokens[i]; |
|---|
| | 125 | } |
|---|
| | 126 | } |
|---|
| | 127 | } |
|---|
| | 128 | return tokens.join(""); |
|---|