1 | /* Copyright (c) 2006-2012 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the 2-clause BSD license. |
---|
3 | * See license.txt in the OpenLayers distribution or repository for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/SingleFile.js |
---|
8 | */ |
---|
9 | |
---|
10 | /** |
---|
11 | * Header: OpenLayers Base Types |
---|
12 | * OpenLayers custom string, number and function functions are described here. |
---|
13 | */ |
---|
14 | |
---|
15 | /** |
---|
16 | * Namespace: OpenLayers.String |
---|
17 | * Contains convenience functions for string manipulation. |
---|
18 | */ |
---|
19 | OpenLayers.String = { |
---|
20 | |
---|
21 | /** |
---|
22 | * APIFunction: startsWith |
---|
23 | * Test whether a string starts with another string. |
---|
24 | * |
---|
25 | * Parameters: |
---|
26 | * str - {String} The string to test. |
---|
27 | * sub - {String} The substring to look for. |
---|
28 | * |
---|
29 | * Returns: |
---|
30 | * {Boolean} The first string starts with the second. |
---|
31 | */ |
---|
32 | startsWith: function(str, sub) { |
---|
33 | return (str.indexOf(sub) == 0); |
---|
34 | }, |
---|
35 | |
---|
36 | /** |
---|
37 | * APIFunction: contains |
---|
38 | * Test whether a string contains another string. |
---|
39 | * |
---|
40 | * Parameters: |
---|
41 | * str - {String} The string to test. |
---|
42 | * sub - {String} The substring to look for. |
---|
43 | * |
---|
44 | * Returns: |
---|
45 | * {Boolean} The first string contains the second. |
---|
46 | */ |
---|
47 | contains: function(str, sub) { |
---|
48 | return (str.indexOf(sub) != -1); |
---|
49 | }, |
---|
50 | |
---|
51 | /** |
---|
52 | * APIFunction: trim |
---|
53 | * Removes leading and trailing whitespace characters from a string. |
---|
54 | * |
---|
55 | * Parameters: |
---|
56 | * str - {String} The (potentially) space padded string. This string is not |
---|
57 | * modified. |
---|
58 | * |
---|
59 | * Returns: |
---|
60 | * {String} A trimmed version of the string with all leading and |
---|
61 | * trailing spaces removed. |
---|
62 | */ |
---|
63 | trim: function(str) { |
---|
64 | return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); |
---|
65 | }, |
---|
66 | |
---|
67 | /** |
---|
68 | * APIFunction: camelize |
---|
69 | * Camel-case a hyphenated string. |
---|
70 | * Ex. "chicken-head" becomes "chickenHead", and |
---|
71 | * "-chicken-head" becomes "ChickenHead". |
---|
72 | * |
---|
73 | * Parameters: |
---|
74 | * str - {String} The string to be camelized. The original is not modified. |
---|
75 | * |
---|
76 | * Returns: |
---|
77 | * {String} The string, camelized |
---|
78 | */ |
---|
79 | camelize: function(str) { |
---|
80 | var oStringList = str.split('-'); |
---|
81 | var camelizedString = oStringList[0]; |
---|
82 | for (var i=1, len=oStringList.length; i<len; i++) { |
---|
83 | var s = oStringList[i]; |
---|
84 | camelizedString += s.charAt(0).toUpperCase() + s.substring(1); |
---|
85 | } |
---|
86 | return camelizedString; |
---|
87 | }, |
---|
88 | |
---|
89 | /** |
---|
90 | * APIFunction: format |
---|
91 | * Given a string with tokens in the form ${token}, return a string |
---|
92 | * with tokens replaced with properties from the given context |
---|
93 | * object. Represent a literal "${" by doubling it, e.g. "${${". |
---|
94 | * |
---|
95 | * Parameters: |
---|
96 | * template - {String} A string with tokens to be replaced. A template |
---|
97 | * has the form "literal ${token}" where the token will be replaced |
---|
98 | * by the value of context["token"]. |
---|
99 | * context - {Object} An optional object with properties corresponding |
---|
100 | * to the tokens in the format string. If no context is sent, the |
---|
101 | * window object will be used. |
---|
102 | * args - {Array} Optional arguments to pass to any functions found in |
---|
103 | * the context. If a context property is a function, the token |
---|
104 | * will be replaced by the return from the function called with |
---|
105 | * these arguments. |
---|
106 | * |
---|
107 | * Returns: |
---|
108 | * {String} A string with tokens replaced from the context object. |
---|
109 | */ |
---|
110 | format: function(template, context, args) { |
---|
111 | if(!context) { |
---|
112 | context = window; |
---|
113 | } |
---|
114 | |
---|
115 | // Example matching: |
---|
116 | // str = ${foo.bar} |
---|
117 | // match = foo.bar |
---|
118 | var replacer = function(str, match) { |
---|
119 | var replacement; |
---|
120 | |
---|
121 | // Loop through all subs. Example: ${a.b.c} |
---|
122 | // 0 -> replacement = context[a]; |
---|
123 | // 1 -> replacement = context[a][b]; |
---|
124 | // 2 -> replacement = context[a][b][c]; |
---|
125 | var subs = match.split(/\.+/); |
---|
126 | for (var i=0; i< subs.length; i++) { |
---|
127 | if (i == 0) { |
---|
128 | replacement = context; |
---|
129 | } |
---|
130 | |
---|
131 | replacement = replacement[subs[i]]; |
---|
132 | } |
---|
133 | |
---|
134 | if(typeof replacement == "function") { |
---|
135 | replacement = args ? |
---|
136 | replacement.apply(null, args) : |
---|
137 | replacement(); |
---|
138 | } |
---|
139 | |
---|
140 | // If replacement is undefined, return the string 'undefined'. |
---|
141 | // This is a workaround for a bugs in browsers not properly |
---|
142 | // dealing with non-participating groups in regular expressions: |
---|
143 | // http://blog.stevenlevithan.com/archives/npcg-javascript |
---|
144 | if (typeof replacement == 'undefined') { |
---|
145 | return 'undefined'; |
---|
146 | } else { |
---|
147 | return replacement; |
---|
148 | } |
---|
149 | }; |
---|
150 | |
---|
151 | return template.replace(OpenLayers.String.tokenRegEx, replacer); |
---|
152 | }, |
---|
153 | |
---|
154 | /** |
---|
155 | * Property: tokenRegEx |
---|
156 | * Used to find tokens in a string. |
---|
157 | * Examples: ${a}, ${a.b.c}, ${a-b}, ${5} |
---|
158 | */ |
---|
159 | tokenRegEx: /\$\{([\w.]+?)\}/g, |
---|
160 | |
---|
161 | /** |
---|
162 | * Property: numberRegEx |
---|
163 | * Used to test strings as numbers. |
---|
164 | */ |
---|
165 | numberRegEx: /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/, |
---|
166 | |
---|
167 | /** |
---|
168 | * APIFunction: isNumeric |
---|
169 | * Determine whether a string contains only a numeric value. |
---|
170 | * |
---|
171 | * Examples: |
---|
172 | * (code) |
---|
173 | * OpenLayers.String.isNumeric("6.02e23") // true |
---|
174 | * OpenLayers.String.isNumeric("12 dozen") // false |
---|
175 | * OpenLayers.String.isNumeric("4") // true |
---|
176 | * OpenLayers.String.isNumeric(" 4 ") // false |
---|
177 | * (end) |
---|
178 | * |
---|
179 | * Returns: |
---|
180 | * {Boolean} String contains only a number. |
---|
181 | */ |
---|
182 | isNumeric: function(value) { |
---|
183 | return OpenLayers.String.numberRegEx.test(value); |
---|
184 | }, |
---|
185 | |
---|
186 | /** |
---|
187 | * APIFunction: numericIf |
---|
188 | * Converts a string that appears to be a numeric value into a number. |
---|
189 | * |
---|
190 | * Parameters: |
---|
191 | * value - {String} |
---|
192 | * |
---|
193 | * Returns: |
---|
194 | * {Number|String} a Number if the passed value is a number, a String |
---|
195 | * otherwise. |
---|
196 | */ |
---|
197 | numericIf: function(value) { |
---|
198 | return OpenLayers.String.isNumeric(value) ? parseFloat(value) : value; |
---|
199 | } |
---|
200 | |
---|
201 | }; |
---|
202 | |
---|
203 | /** |
---|
204 | * Namespace: OpenLayers.Number |
---|
205 | * Contains convenience functions for manipulating numbers. |
---|
206 | */ |
---|
207 | OpenLayers.Number = { |
---|
208 | |
---|
209 | /** |
---|
210 | * Property: decimalSeparator |
---|
211 | * Decimal separator to use when formatting numbers. |
---|
212 | */ |
---|
213 | decimalSeparator: ".", |
---|
214 | |
---|
215 | /** |
---|
216 | * Property: thousandsSeparator |
---|
217 | * Thousands separator to use when formatting numbers. |
---|
218 | */ |
---|
219 | thousandsSeparator: ",", |
---|
220 | |
---|
221 | /** |
---|
222 | * APIFunction: limitSigDigs |
---|
223 | * Limit the number of significant digits on a float. |
---|
224 | * |
---|
225 | * Parameters: |
---|
226 | * num - {Float} |
---|
227 | * sig - {Integer} |
---|
228 | * |
---|
229 | * Returns: |
---|
230 | * {Float} The number, rounded to the specified number of significant |
---|
231 | * digits. |
---|
232 | */ |
---|
233 | limitSigDigs: function(num, sig) { |
---|
234 | var fig = 0; |
---|
235 | if (sig > 0) { |
---|
236 | fig = parseFloat(num.toPrecision(sig)); |
---|
237 | } |
---|
238 | return fig; |
---|
239 | }, |
---|
240 | |
---|
241 | /** |
---|
242 | * APIFunction: format |
---|
243 | * Formats a number for output. |
---|
244 | * |
---|
245 | * Parameters: |
---|
246 | * num - {Float} |
---|
247 | * dec - {Integer} Number of decimal places to round to. |
---|
248 | * Defaults to 0. Set to null to leave decimal places unchanged. |
---|
249 | * tsep - {String} Thousands separator. |
---|
250 | * Default is ",". |
---|
251 | * dsep - {String} Decimal separator. |
---|
252 | * Default is ".". |
---|
253 | * |
---|
254 | * Returns: |
---|
255 | * {String} A string representing the formatted number. |
---|
256 | */ |
---|
257 | format: function(num, dec, tsep, dsep) { |
---|
258 | dec = (typeof dec != "undefined") ? dec : 0; |
---|
259 | tsep = (typeof tsep != "undefined") ? tsep : |
---|
260 | OpenLayers.Number.thousandsSeparator; |
---|
261 | dsep = (typeof dsep != "undefined") ? dsep : |
---|
262 | OpenLayers.Number.decimalSeparator; |
---|
263 | |
---|
264 | if (dec != null) { |
---|
265 | num = parseFloat(num.toFixed(dec)); |
---|
266 | } |
---|
267 | |
---|
268 | var parts = num.toString().split("."); |
---|
269 | if (parts.length == 1 && dec == null) { |
---|
270 | // integer where we do not want to touch the decimals |
---|
271 | dec = 0; |
---|
272 | } |
---|
273 | |
---|
274 | var integer = parts[0]; |
---|
275 | if (tsep) { |
---|
276 | var thousands = /(-?[0-9]+)([0-9]{3})/; |
---|
277 | while(thousands.test(integer)) { |
---|
278 | integer = integer.replace(thousands, "$1" + tsep + "$2"); |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | var str; |
---|
283 | if (dec == 0) { |
---|
284 | str = integer; |
---|
285 | } else { |
---|
286 | var rem = parts.length > 1 ? parts[1] : "0"; |
---|
287 | if (dec != null) { |
---|
288 | rem = rem + new Array(dec - rem.length + 1).join("0"); |
---|
289 | } |
---|
290 | str = integer + dsep + rem; |
---|
291 | } |
---|
292 | return str; |
---|
293 | } |
---|
294 | }; |
---|
295 | |
---|
296 | /** |
---|
297 | * Namespace: OpenLayers.Function |
---|
298 | * Contains convenience functions for function manipulation. |
---|
299 | */ |
---|
300 | OpenLayers.Function = { |
---|
301 | /** |
---|
302 | * APIFunction: bind |
---|
303 | * Bind a function to an object. Method to easily create closures with |
---|
304 | * 'this' altered. |
---|
305 | * |
---|
306 | * Parameters: |
---|
307 | * func - {Function} Input function. |
---|
308 | * object - {Object} The object to bind to the input function (as this). |
---|
309 | * |
---|
310 | * Returns: |
---|
311 | * {Function} A closure with 'this' set to the passed in object. |
---|
312 | */ |
---|
313 | bind: function(func, object) { |
---|
314 | // create a reference to all arguments past the second one |
---|
315 | var args = Array.prototype.slice.apply(arguments, [2]); |
---|
316 | return function() { |
---|
317 | // Push on any additional arguments from the actual function call. |
---|
318 | // These will come after those sent to the bind call. |
---|
319 | var newArgs = args.concat( |
---|
320 | Array.prototype.slice.apply(arguments, [0]) |
---|
321 | ); |
---|
322 | return func.apply(object, newArgs); |
---|
323 | }; |
---|
324 | }, |
---|
325 | |
---|
326 | /** |
---|
327 | * APIFunction: bindAsEventListener |
---|
328 | * Bind a function to an object, and configure it to receive the event |
---|
329 | * object as first parameter when called. |
---|
330 | * |
---|
331 | * Parameters: |
---|
332 | * func - {Function} Input function to serve as an event listener. |
---|
333 | * object - {Object} A reference to this. |
---|
334 | * |
---|
335 | * Returns: |
---|
336 | * {Function} |
---|
337 | */ |
---|
338 | bindAsEventListener: function(func, object) { |
---|
339 | return function(event) { |
---|
340 | return func.call(object, event || window.event); |
---|
341 | }; |
---|
342 | }, |
---|
343 | |
---|
344 | /** |
---|
345 | * APIFunction: False |
---|
346 | * A simple function to that just does "return false". We use this to |
---|
347 | * avoid attaching anonymous functions to DOM event handlers, which |
---|
348 | * causes "issues" on IE<8. |
---|
349 | * |
---|
350 | * Usage: |
---|
351 | * document.onclick = OpenLayers.Function.False; |
---|
352 | * |
---|
353 | * Returns: |
---|
354 | * {Boolean} |
---|
355 | */ |
---|
356 | False : function() { |
---|
357 | return false; |
---|
358 | }, |
---|
359 | |
---|
360 | /** |
---|
361 | * APIFunction: True |
---|
362 | * A simple function to that just does "return true". We use this to |
---|
363 | * avoid attaching anonymous functions to DOM event handlers, which |
---|
364 | * causes "issues" on IE<8. |
---|
365 | * |
---|
366 | * Usage: |
---|
367 | * document.onclick = OpenLayers.Function.True; |
---|
368 | * |
---|
369 | * Returns: |
---|
370 | * {Boolean} |
---|
371 | */ |
---|
372 | True : function() { |
---|
373 | return true; |
---|
374 | }, |
---|
375 | |
---|
376 | /** |
---|
377 | * APIFunction: Void |
---|
378 | * A reusable function that returns ``undefined``. |
---|
379 | * |
---|
380 | * Returns: |
---|
381 | * {undefined} |
---|
382 | */ |
---|
383 | Void: function() {} |
---|
384 | |
---|
385 | }; |
---|
386 | |
---|
387 | /** |
---|
388 | * Namespace: OpenLayers.Array |
---|
389 | * Contains convenience functions for array manipulation. |
---|
390 | */ |
---|
391 | OpenLayers.Array = { |
---|
392 | |
---|
393 | /** |
---|
394 | * APIMethod: filter |
---|
395 | * Filter an array. Provides the functionality of the |
---|
396 | * Array.prototype.filter extension to the ECMA-262 standard. Where |
---|
397 | * available, Array.prototype.filter will be used. |
---|
398 | * |
---|
399 | * Based on well known example from http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter |
---|
400 | * |
---|
401 | * Parameters: |
---|
402 | * array - {Array} The array to be filtered. This array is not mutated. |
---|
403 | * Elements added to this array by the callback will not be visited. |
---|
404 | * callback - {Function} A function that is called for each element in |
---|
405 | * the array. If this function returns true, the element will be |
---|
406 | * included in the return. The function will be called with three |
---|
407 | * arguments: the element in the array, the index of that element, and |
---|
408 | * the array itself. If the optional caller parameter is specified |
---|
409 | * the callback will be called with this set to caller. |
---|
410 | * caller - {Object} Optional object to be set as this when the callback |
---|
411 | * is called. |
---|
412 | * |
---|
413 | * Returns: |
---|
414 | * {Array} An array of elements from the passed in array for which the |
---|
415 | * callback returns true. |
---|
416 | */ |
---|
417 | filter: function(array, callback, caller) { |
---|
418 | var selected = []; |
---|
419 | if (Array.prototype.filter) { |
---|
420 | selected = array.filter(callback, caller); |
---|
421 | } else { |
---|
422 | var len = array.length; |
---|
423 | if (typeof callback != "function") { |
---|
424 | throw new TypeError(); |
---|
425 | } |
---|
426 | for(var i=0; i<len; i++) { |
---|
427 | if (i in array) { |
---|
428 | var val = array[i]; |
---|
429 | if (callback.call(caller, val, i, array)) { |
---|
430 | selected.push(val); |
---|
431 | } |
---|
432 | } |
---|
433 | } |
---|
434 | } |
---|
435 | return selected; |
---|
436 | } |
---|
437 | |
---|
438 | }; |
---|