diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index ed6593a97b..eec24f08f4 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -449,6 +449,144 @@ OpenLayers.Map = OpenLayers.Class({ return this.tileSize; }, + + /** + * APIMethod: getBy + * Get a list of objects given a property and a match item. + * + * Parameters: + * array - {String} A property on the map whose value is an array. + * property - {String} A property on each item of the given array. + * match - {String | Object} A string to match. Can also be a regular + * expression literal or object. In addition, it can be any object + * with a method named test. For reqular expressions or other, if + * match.test(map[array][i][property]) evaluates to true, the item will + * be included in the array returned. If no items are found, an empty + * array is returned. + * + * Returns: + * {Array} An array of items where the given property matches the given + * criteria. + */ + getBy: function(array, property, match) { + var found = []; + var item; + var list = this[array]; + var test = (typeof match.test == "function"); + if(list instanceof Array) { + for(var i=0; i)} A list of layers matching the given criteria. + * An empty array is returned if no matches are found. + */ + getLayersBy: function(property, match) { + return this.getBy("layers", property, match); + }, + + /** + * APIMethod: getLayersByName + * Get a list of layers with names matching the given name. + * + * Parameter: + * match - {String | Object} A layer name. The name can also be a regular + * expression literal or object. In addition, it can be any object + * with a method named test. For reqular expressions or other, if + * name.test(layer.name) evaluates to true, the layer will be included + * in the list of layers returned. If no layers are found, an empty + * array is returned. + * + * Returns: + * {Array()} A list of layers matching the given name. + * An empty array is returned if no matches are found. + */ + getLayersByName: function(match) { + return this.getLayersBy("name", match); + }, + + /** + * APIMethod: getLayersByType + * Get a list of layers of a given type (CLASS_NAME). + * + * Parameter: + * match - {String | Object} A layer class name. The type can also be a + * regular expression literal or object. In addition, it can be any + * object with a method named test. For reqular expressions or other, + * if type.test(layer.CLASS_NAME) evaluates to true, the layer will + * be included in the list of layers returned. If no layers are + * found, an empty array is returned. + * + * Returns: + * {Array()} A list of layers matching the given type. + * An empty array is returned if no matches are found. + */ + getLayersByType: function(match) { + return this.getLayersBy("CLASS_NAME", match); + }, + + /** + * APIMethod: getControlsBy + * Get a list of controls with properties matching the given criteria. + * + * Parameter: + * property - {String} A control property to be matched. + * match - {String | Object} A string to match. Can also be a regular + * expression literal or object. In addition, it can be any object + * with a method named test. For reqular expressions or other, if + * match.test(layer[property]) evaluates to true, the layer will be + * included in the array returned. If no layers are found, an empty + * array is returned. + * + * Returns: + * {Array()} A list of controls matching the given + * criteria. An empty array is returned if no matches are found. + */ + getControlsBy: function(property, match) { + return this.getBy("controls", property, match); + }, + + /** + * APIMethod: getControlsByType + * Get a list of controls of a given type (CLASS_NAME). + * + * Parameter: + * match - {String | Object} A control class name. The type can also be a + * regular expression literal or object. In addition, it can be any + * object with a method named test. For reqular expressions or other, + * if type.test(control.CLASS_NAME) evaluates to true, the control will + * be included in the list of controls returned. If no controls are + * found, an empty array is returned. + * + * Returns: + * {Array()} A list of controls matching the given type. + * An empty array is returned if no matches are found. + */ + getControlsByType: function(match) { + return this.getControlsBy("CLASS_NAME", match); + }, + /********************************************************/ /* */ /* Layer Functions */ diff --git a/tests/test_Map.html b/tests/test_Map.html index 98830758c1..df6f5c1666 100644 --- a/tests/test_Map.html +++ b/tests/test_Map.html @@ -262,6 +262,289 @@ t.ok( gotLayer == null, "getLayer correctly returns null when layer not found"); } + function test_Map_getLayersBy(t) { + + var map = { + getBy: OpenLayers.Map.prototype.getBy, + getLayersBy: OpenLayers.Map.prototype.getLayersBy, + layers: [ + {foo: "foo", id: Math.random()}, + {foo: "bar", id: Math.random()}, + {foo: "foobar", id: Math.random()}, + {foo: "foo bar", id: Math.random()}, + {foo: "foo", id: Math.random()} + ] + }; + + var cases = [ + { + got: map.getLayersBy("foo", "foo"), + expected: [map.layers[0], map.layers[4]], + message: "(string literal) got two layers matching foo" + }, { + got: map.getLayersBy("foo", "bar"), + expected: [map.layers[1]], + message: "(string literal) got one layer matching foo" + }, { + got: map.getLayersBy("foo", "barfoo"), + expected: [], + message: "(string literal) got empty array for no foo match" + }, { + got: map.getLayersBy("foo", /foo/), + expected: [map.layers[0], map.layers[2], map.layers[3], map.layers[4]], + message: "(regexp literal) got three layers containing string" + }, { + got: map.getLayersBy("foo", /foo$/), + expected: [map.layers[0], map.layers[4]], + message: "(regexp literal) got three layers ending with string" + }, { + got: map.getLayersBy("foo", /\s/), + expected: [map.layers[3]], + message: "(regexp literal) got layer containing space" + }, { + got: map.getLayersBy("foo", new RegExp("BAR", "i")), + expected: [map.layers[1], map.layers[2], map.layers[3]], + message: "(regexp object) got layers ignoring case" + }, { + got: map.getLayersBy("foo", {test: function(str) {return str.length > 3;}}), + expected: [map.layers[2], map.layers[3]], + message: "(custom object) got layers with foo length greater than 3" + } + ]; + t.plan(cases.length); + for(var i=0; i 3;}}), + expected: [map.layers[2], map.layers[3]], + message: "(custom object) got layers with name length greater than 3" + } + ]; + t.plan(cases.length); + for(var i=0; i 3;}}), + expected: [map.layers[2], map.layers[3]], + message: "(custom object) got layers with type length greater than 3" + } + ]; + t.plan(cases.length); + for(var i=0; i 3;}}), + expected: [map.controls[2], map.controls[3]], + message: "(custom object) got controls with foo length greater than 3" + } + ]; + t.plan(cases.length); + for(var i=0; i 3;}}), + expected: [map.controls[2], map.controls[3]], + message: "(custom object) got controls with type length greater than 3" + } + ]; + t.plan(cases.length); + for(var i=0; i