adding getBy and related methods to map - this allows flexible retrieval of things like controls and layers - thanks elem for the review (closes #1153).
git-svn-id: http://svn.openlayers.org/trunk/openlayers@5361 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -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<list.length; ++i) {
|
||||
item = list[i];
|
||||
if(item[property] == match ||
|
||||
(test && match.test(item[property]))) {
|
||||
found.push(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
return found;
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: getLayersBy
|
||||
* Get a list of layers with properties matching the given criteria.
|
||||
*
|
||||
* Parameter:
|
||||
* property - {String} A layer 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(<OpenLayers.Layer>)} 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(<OpenLayers.Layer>)} 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(<OpenLayers.Layer>)} 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(<OpenLayers.Control>)} 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(<OpenLayers.Control>)} 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 */
|
||||
|
||||
Reference in New Issue
Block a user