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;
|
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 */
|
/* Layer Functions */
|
||||||
|
|||||||
@@ -262,6 +262,289 @@
|
|||||||
t.ok( gotLayer == null, "getLayer correctly returns null when layer not found");
|
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<cases.length; ++i) {
|
||||||
|
t.eq(cases[i].got, cases[i].expected, cases[i].message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_Map_getLayersByName(t) {
|
||||||
|
|
||||||
|
var map = {
|
||||||
|
getBy: OpenLayers.Map.prototype.getBy,
|
||||||
|
getLayersBy: OpenLayers.Map.prototype.getLayersBy,
|
||||||
|
getLayersByName: OpenLayers.Map.prototype.getLayersByName,
|
||||||
|
layers: [
|
||||||
|
{name: "foo", id: Math.random()},
|
||||||
|
{name: "bar", id: Math.random()},
|
||||||
|
{name: "foobar", id: Math.random()},
|
||||||
|
{name: "foo bar", id: Math.random()},
|
||||||
|
{name: "foo", id: Math.random()}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
var cases = [
|
||||||
|
{
|
||||||
|
got: map.getLayersByName("foo"),
|
||||||
|
expected: [map.layers[0], map.layers[4]],
|
||||||
|
message: "(string literal) got two layers matching name"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByName("bar"),
|
||||||
|
expected: [map.layers[1]],
|
||||||
|
message: "(string literal) got one layer matching name"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByName("barfoo"),
|
||||||
|
expected: [],
|
||||||
|
message: "(string literal) got empty array for no match"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByName(/foo/),
|
||||||
|
expected: [map.layers[0], map.layers[2], map.layers[3], map.layers[4]],
|
||||||
|
message: "(regexp literal) got three layers containing string"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByName(/foo$/),
|
||||||
|
expected: [map.layers[0], map.layers[4]],
|
||||||
|
message: "(regexp literal) got three layers ending with string"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByName(/\s/),
|
||||||
|
expected: [map.layers[3]],
|
||||||
|
message: "(regexp literal) got layer containing space"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByName(new RegExp("BAR", "i")),
|
||||||
|
expected: [map.layers[1], map.layers[2], map.layers[3]],
|
||||||
|
message: "(regexp object) got layers ignoring case"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByName({test: function(str) {return str.length > 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<cases.length; ++i) {
|
||||||
|
t.eq(cases[i].got, cases[i].expected, cases[i].message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_Map_getLayersByType(t) {
|
||||||
|
|
||||||
|
var map = {
|
||||||
|
getBy: OpenLayers.Map.prototype.getBy,
|
||||||
|
getLayersBy: OpenLayers.Map.prototype.getLayersBy,
|
||||||
|
getLayersByType: OpenLayers.Map.prototype.getLayersByType,
|
||||||
|
layers: [
|
||||||
|
{CLASS_NAME: "foo", id: Math.random()},
|
||||||
|
{CLASS_NAME: "bar", id: Math.random()},
|
||||||
|
{CLASS_NAME: "foobar", id: Math.random()},
|
||||||
|
{CLASS_NAME: "foo bar", id: Math.random()},
|
||||||
|
{CLASS_NAME: "foo", id: Math.random()}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
var cases = [
|
||||||
|
{
|
||||||
|
got: map.getLayersByType("foo"),
|
||||||
|
expected: [map.layers[0], map.layers[4]],
|
||||||
|
message: "(string literal) got two layers matching type"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByType("bar"),
|
||||||
|
expected: [map.layers[1]],
|
||||||
|
message: "(string literal) got one layer matching type"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByType("barfoo"),
|
||||||
|
expected: [],
|
||||||
|
message: "(string literal) got empty array for no match"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByType(/foo/),
|
||||||
|
expected: [map.layers[0], map.layers[2], map.layers[3], map.layers[4]],
|
||||||
|
message: "(regexp literal) got three layers containing string"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByType(/foo$/),
|
||||||
|
expected: [map.layers[0], map.layers[4]],
|
||||||
|
message: "(regexp literal) got three layers ending with string"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByType(/\s/),
|
||||||
|
expected: [map.layers[3]],
|
||||||
|
message: "(regexp literal) got layer containing space"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByType(new RegExp("BAR", "i")),
|
||||||
|
expected: [map.layers[1], map.layers[2], map.layers[3]],
|
||||||
|
message: "(regexp object) got layers ignoring case"
|
||||||
|
}, {
|
||||||
|
got: map.getLayersByType({test: function(str) {return str.length > 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<cases.length; ++i) {
|
||||||
|
t.eq(cases[i].got, cases[i].expected, cases[i].message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_Map_getControlsBy(t) {
|
||||||
|
|
||||||
|
var map = {
|
||||||
|
getBy: OpenLayers.Map.prototype.getBy,
|
||||||
|
getControlsBy: OpenLayers.Map.prototype.getControlsBy,
|
||||||
|
controls: [
|
||||||
|
{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.getControlsBy("foo", "foo"),
|
||||||
|
expected: [map.controls[0], map.controls[4]],
|
||||||
|
message: "(string literal) got two controls matching foo"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsBy("foo", "bar"),
|
||||||
|
expected: [map.controls[1]],
|
||||||
|
message: "(string literal) got one control matching foo"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsBy("foo", "barfoo"),
|
||||||
|
expected: [],
|
||||||
|
message: "(string literal) got empty array for no foo match"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsBy("foo", /foo/),
|
||||||
|
expected: [map.controls[0], map.controls[2], map.controls[3], map.controls[4]],
|
||||||
|
message: "(regexp literal) got three controls containing string"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsBy("foo", /foo$/),
|
||||||
|
expected: [map.controls[0], map.controls[4]],
|
||||||
|
message: "(regexp literal) got three controls ending with string"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsBy("foo", /\s/),
|
||||||
|
expected: [map.controls[3]],
|
||||||
|
message: "(regexp literal) got control containing space"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsBy("foo", new RegExp("BAR", "i")),
|
||||||
|
expected: [map.controls[1], map.controls[2], map.controls[3]],
|
||||||
|
message: "(regexp object) got layers ignoring case"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsBy("foo", {test: function(str) {return str.length > 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<cases.length; ++i) {
|
||||||
|
t.eq(cases[i].got, cases[i].expected, cases[i].message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_Map_getControlsByType(t) {
|
||||||
|
|
||||||
|
var map = {
|
||||||
|
getBy: OpenLayers.Map.prototype.getBy,
|
||||||
|
getControlsBy: OpenLayers.Map.prototype.getControlsBy,
|
||||||
|
getControlsByType: OpenLayers.Map.prototype.getControlsByType,
|
||||||
|
controls: [
|
||||||
|
{CLASS_NAME: "foo", id: Math.random()},
|
||||||
|
{CLASS_NAME: "bar", id: Math.random()},
|
||||||
|
{CLASS_NAME: "foobar", id: Math.random()},
|
||||||
|
{CLASS_NAME: "foo bar", id: Math.random()},
|
||||||
|
{CLASS_NAME: "foo", id: Math.random()}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
var cases = [
|
||||||
|
{
|
||||||
|
got: map.getControlsByType("foo"),
|
||||||
|
expected: [map.controls[0], map.controls[4]],
|
||||||
|
message: "(string literal) got two controls matching type"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsByType("bar"),
|
||||||
|
expected: [map.controls[1]],
|
||||||
|
message: "(string literal) got one control matching type"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsByType("barfoo"),
|
||||||
|
expected: [],
|
||||||
|
message: "(string literal) got empty array for no match"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsByType(/foo/),
|
||||||
|
expected: [map.controls[0], map.controls[2], map.controls[3], map.controls[4]],
|
||||||
|
message: "(regexp literal) got three controls containing string"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsByType(/foo$/),
|
||||||
|
expected: [map.controls[0], map.controls[4]],
|
||||||
|
message: "(regexp literal) got three controls ending with string"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsByType(/\s/),
|
||||||
|
expected: [map.controls[3]],
|
||||||
|
message: "(regexp literal) got control containing space"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsByType(new RegExp("BAR", "i")),
|
||||||
|
expected: [map.controls[1], map.controls[2], map.controls[3]],
|
||||||
|
message: "(regexp object) got controls ignoring case"
|
||||||
|
}, {
|
||||||
|
got: map.getControlsByType({test: function(str) {return str.length > 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<cases.length; ++i) {
|
||||||
|
t.eq(cases[i].got, cases[i].expected, cases[i].message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
function test_11_Map_double_addLayer(t) {
|
function test_11_Map_double_addLayer(t) {
|
||||||
t.plan( 1 );
|
t.plan( 1 );
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user