Changeset 5532
- Timestamp:
- 12/20/07 02:12:19 (1 year ago)
- Files:
-
- trunk/openlayers/lib/OpenLayers/BaseTypes.js (modified) (1 diff)
- trunk/openlayers/lib/OpenLayers/Control/Panel.js (modified) (1 diff)
- trunk/openlayers/lib/OpenLayers/Map.js (modified) (1 diff)
- trunk/openlayers/tests/Control/test_Panel.html (modified) (1 diff)
- trunk/openlayers/tests/test_BaseTypes.html (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openlayers/lib/OpenLayers/BaseTypes.js
r5317 r5532 350 350 }; 351 351 } 352 353 /********************* 354 * * 355 * ARRAY * 356 * * 357 *********************/ 358 359 OpenLayers.Array = { 360 361 /** 362 * APIMethod: OpenLayers.Array.filter 363 * Filter an array. Provides the functionality of the 364 * Array.prototype.filter extension to the ECMA-262 standard. Where 365 * available, Array.prototype.filter will be used. 366 * 367 * Based on well known example from http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter 368 * 369 * Parameters: 370 * array - {Array} The array to be filtered. This array is not mutated. 371 * Elements added to this array by the callback will not be visited. 372 * callback - {Function} A function that is called for each element in 373 * the array. If this function returns true, the element will be 374 * included in the return. The function will be called with three 375 * arguments: the element in the array, the index of that element, and 376 * the array itself. If the optional caller parameter is specified 377 * the callback will be called with this set to caller. 378 * caller - {Object} Optional object to be set as this when the callback 379 * is called. 380 * 381 * Returns: 382 * {Array} An array of elements from the passed in array for which the 383 * callback returns true. 384 */ 385 filter: function(array, callback, caller) { 386 var selected = []; 387 if (Array.prototype.filter) { 388 selected = array.filter(callback, caller); 389 } else { 390 var len = array.length; 391 if (typeof callback != "function") { 392 throw new TypeError(); 393 } 394 for(var i=0; i<len; i++) { 395 if (i in array) { 396 var val = array[i]; 397 if (callback.call(caller, val, i, array)) { 398 selected.push(val); 399 } 400 } 401 } 402 } 403 return selected; 404 } 405 406 }; trunk/openlayers/lib/OpenLayers/Control/Panel.js
r4985 r5532 193 193 }, 194 194 195 /** 196 * APIMethod: getControlsBy 197 * Get a list of controls with properties matching the given criteria. 198 * 199 * Parameter: 200 * property - {String} A control property to be matched. 201 * match - {String | Object} A string to match. Can also be a regular 202 * expression literal or object. In addition, it can be any object 203 * with a method named test. For reqular expressions or other, if 204 * match.test(control[property]) evaluates to true, the control will be 205 * included in the array returned. If no controls are found, an empty 206 * array is returned. 207 * 208 * Returns: 209 * {Array(<OpenLayers.Control>)} A list of controls matching the given criteria. 210 * An empty array is returned if no matches are found. 211 */ 212 getControlsBy: function(property, match) { 213 var test = (typeof match.test == "function"); 214 var found = OpenLayers.Array.filter(this.controls, function(item) { 215 return item[property] == match || (test && match.test(item[property])); 216 }); 217 return found; 218 }, 219 220 /** 221 * APIMethod: getControlsByName 222 * Get a list of contorls with names matching the given name. 223 * 224 * Parameter: 225 * match - {String | Object} A control name. The name can also be a regular 226 * expression literal or object. In addition, it can be any object 227 * with a method named test. For reqular expressions or other, if 228 * name.test(control.name) evaluates to true, the control will be included 229 * in the list of controls returned. If no controls are found, an empty 230 * array is returned. 231 * 232 * Returns: 233 * {Array(<OpenLayers.Control>)} A list of controls matching the given name. 234 * An empty array is returned if no matches are found. 235 */ 236 getControlsByName: function(match) { 237 return this.getControlsBy("name", match); 238 }, 239 240 /** 241 * APIMethod: getControlsByClass 242 * Get a list of controls of a given type (CLASS_NAME). 243 * 244 * Parameter: 245 * match - {String | Object} A control class name. The type can also be a 246 * regular expression literal or object. In addition, it can be any 247 * object with a method named test. For reqular expressions or other, 248 * if type.test(control.CLASS_NAME) evaluates to true, the control will 249 * be included in the list of controls returned. If no controls are 250 * found, an empty array is returned. 251 * 252 * Returns: 253 * {Array(<OpenLayers.Control>)} A list of controls matching the given type. 254 * An empty array is returned if no matches are found. 255 */ 256 getControlsByClass: function(match) { 257 return this.getControlsBy("CLASS_NAME", match); 258 }, 259 195 260 CLASS_NAME: "OpenLayers.Control.Panel" 196 261 }); trunk/openlayers/lib/OpenLayers/Map.js
r5519 r5532 476 476 */ 477 477 getBy: function(array, property, match) { 478 var found = [];479 var item;480 var list = this[array];481 478 var test = (typeof match.test == "function"); 482 if(list instanceof Array) { 483 for(var i=0; i<list.length; ++i) { 484 item = list[i]; 485 if(item[property] == match || 486 (test && match.test(item[property]))) { 487 found.push(item); 488 } 489 } 490 } 479 var found = OpenLayers.Array.filter(this[array], function(item) { 480 return item[property] == match || (test && match.test(item[property])); 481 }); 491 482 return found; 492 483 }, trunk/openlayers/tests/Control/test_Panel.html
r4059 r5532 48 48 } 49 49 50 function test_Control_Panel_getBy(t) { 51 52 var panel = { 53 getBy: OpenLayers.Control.Panel.prototype.getBy, 54 getControlsBy: OpenLayers.Control.Panel.prototype.getControlsBy, 55 controls: [ 56 {foo: "foo", id: Math.random()}, 57 {foo: "bar", id: Math.random()}, 58 {foo: "foobar", id: Math.random()}, 59 {foo: "foo bar", id: Math.random()}, 60 {foo: "foo", id: Math.random()} 61 ] 62 }; 63 64 var cases = [ 65 { 66 got: panel.getControlsBy("foo", "foo"), 67 expected: [panel.controls[0], panel.controls[4]], 68 message: "(string literal) got two controls matching foo" 69 }, { 70 got: panel.getControlsBy("foo", "bar"), 71 expected: [panel.controls[1]], 72 message: "(string literal) got one control matching foo" 73 }, { 74 got: panel.getControlsBy("foo", "barfoo"), 75 expected: [], 76 message: "(string literal) got empty array for no foo match" 77 }, { 78 got: panel.getControlsBy("foo", /foo/), 79 expected: [panel.controls[0], panel.controls[2], panel.controls[3], panel.controls[4]], 80 message: "(regexp literal) got three controls containing string" 81 }, { 82 got: panel.getControlsBy("foo", /foo$/), 83 expected: [panel.controls[0], panel.controls[4]], 84 message: "(regexp literal) got three controls ending with string" 85 }, { 86 got: panel.getControlsBy("foo", /\s/), 87 expected: [panel.controls[3]], 88 message: "(regexp literal) got control containing space" 89 }, { 90 got: panel.getControlsBy("foo", new RegExp("BAR", "i")), 91 expected: [panel.controls[1], panel.controls[2], panel.controls[3]], 92 message: "(regexp object) got layers ignoring case" 93 }, { 94 got: panel.getControlsBy("foo", {test: function(str) {return str.length > 3;}}), 95 expected: [panel.controls[2], panel.controls[3]], 96 message: "(custom object) got controls with foo length greater than 3" 97 } 98 ]; 99 t.plan(cases.length); 100 for(var i=0; i<cases.length; ++i) { 101 t.eq(cases[i].got, cases[i].expected, cases[i].message); 102 } 103 104 105 } 106 50 107 </script> 51 108 </head> trunk/openlayers/tests/test_BaseTypes.html
r5317 r5532 188 188 } 189 189 190 function test_Array_filter(t) { 191 192 t.plan(8); 193 194 OpenLayers.Array.filter(["foo"], function(item, index, array) { 195 t.eq(item, "foo", "callback called with proper item"); 196 t.eq(index, 0, "callback called with proper index"); 197 t.eq(array, ["foo"], "callback called with proper array"); 198 t.eq(this, {"foo": "bar"}, "callback called with this set properly"); 199 }, {"foo": "bar"}); 200 201 var array = [0, 1, 2, 3]; 202 var select = OpenLayers.Array.filter(array, function(value) { 203 return value > 1; 204 }); 205 t.eq(select, [2, 3], "filter works for basic callback"); 206 t.eq(array, [0, 1, 2, 3], "filter doesn't modify original"); 207 208 var obj = { 209 test: function(value) { 210 if(value > 1) { 211 return true; 212 } 213 } 214 }; 215 var select = OpenLayers.Array.filter(array, function(value) { 216 return this.test(value); 217 }, obj); 218 t.eq(select, [2, 3], "filter works for callback and caller"); 219 t.eq(array, [0, 1, 2, 3], "filter doesn't modify original"); 220 221 222 } 223 190 224 191 225
