Adding OpenLayers.Array.filter to mimic Array.prototype.filter. Adding panel.getControlsBy and related methods that use filter. Reworking map.getBy to use filter as well. r=elemoine (closes #1203)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@5532 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -349,3 +349,58 @@ if (!Function.prototype.bindAsEventListener) {
|
||||
return OpenLayers.Function.bindAsEventListener(this, object);
|
||||
};
|
||||
}
|
||||
|
||||
/*********************
|
||||
* *
|
||||
* ARRAY *
|
||||
* *
|
||||
*********************/
|
||||
|
||||
OpenLayers.Array = {
|
||||
|
||||
/**
|
||||
* APIMethod: OpenLayers.Array.filter
|
||||
* Filter an array. Provides the functionality of the
|
||||
* Array.prototype.filter extension to the ECMA-262 standard. Where
|
||||
* available, Array.prototype.filter will be used.
|
||||
*
|
||||
* Based on well known example from http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter
|
||||
*
|
||||
* Parameters:
|
||||
* array - {Array} The array to be filtered. This array is not mutated.
|
||||
* Elements added to this array by the callback will not be visited.
|
||||
* callback - {Function} A function that is called for each element in
|
||||
* the array. If this function returns true, the element will be
|
||||
* included in the return. The function will be called with three
|
||||
* arguments: the element in the array, the index of that element, and
|
||||
* the array itself. If the optional caller parameter is specified
|
||||
* the callback will be called with this set to caller.
|
||||
* caller - {Object} Optional object to be set as this when the callback
|
||||
* is called.
|
||||
*
|
||||
* Returns:
|
||||
* {Array} An array of elements from the passed in array for which the
|
||||
* callback returns true.
|
||||
*/
|
||||
filter: function(array, callback, caller) {
|
||||
var selected = [];
|
||||
if (Array.prototype.filter) {
|
||||
selected = array.filter(callback, caller);
|
||||
} else {
|
||||
var len = array.length;
|
||||
if (typeof callback != "function") {
|
||||
throw new TypeError();
|
||||
}
|
||||
for(var i=0; i<len; i++) {
|
||||
if (i in array) {
|
||||
var val = array[i];
|
||||
if (callback.call(caller, val, i, array)) {
|
||||
selected.push(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return selected;
|
||||
}
|
||||
|
||||
};
|
||||
@@ -192,6 +192,71 @@ OpenLayers.Control.Panel = OpenLayers.Class(OpenLayers.Control, {
|
||||
this.activateControl(ctrl);
|
||||
},
|
||||
|
||||
/**
|
||||
* 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(control[property]) evaluates to true, the control will be
|
||||
* included in the array returned. If no controls 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) {
|
||||
var test = (typeof match.test == "function");
|
||||
var found = OpenLayers.Array.filter(this.controls, function(item) {
|
||||
return item[property] == match || (test && match.test(item[property]));
|
||||
});
|
||||
return found;
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: getControlsByName
|
||||
* Get a list of contorls with names matching the given name.
|
||||
*
|
||||
* Parameter:
|
||||
* match - {String | Object} A control 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(control.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 name.
|
||||
* An empty array is returned if no matches are found.
|
||||
*/
|
||||
getControlsByName: function(match) {
|
||||
return this.getControlsBy("name", match);
|
||||
},
|
||||
|
||||
/**
|
||||
* APIMethod: getControlsByClass
|
||||
* 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.
|
||||
*/
|
||||
getControlsByClass: function(match) {
|
||||
return this.getControlsBy("CLASS_NAME", match);
|
||||
},
|
||||
|
||||
CLASS_NAME: "OpenLayers.Control.Panel"
|
||||
});
|
||||
|
||||
|
||||
@@ -475,19 +475,10 @@ OpenLayers.Map = OpenLayers.Class({
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
var found = OpenLayers.Array.filter(this[array], function(item) {
|
||||
return item[property] == match || (test && match.test(item[property]));
|
||||
});
|
||||
return found;
|
||||
},
|
||||
|
||||
|
||||
@@ -47,6 +47,63 @@
|
||||
"activated the other tool control, the first one is inactive and the toggle control still active.");
|
||||
}
|
||||
|
||||
function test_Control_Panel_getBy(t) {
|
||||
|
||||
var panel = {
|
||||
getBy: OpenLayers.Control.Panel.prototype.getBy,
|
||||
getControlsBy: OpenLayers.Control.Panel.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: panel.getControlsBy("foo", "foo"),
|
||||
expected: [panel.controls[0], panel.controls[4]],
|
||||
message: "(string literal) got two controls matching foo"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", "bar"),
|
||||
expected: [panel.controls[1]],
|
||||
message: "(string literal) got one control matching foo"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", "barfoo"),
|
||||
expected: [],
|
||||
message: "(string literal) got empty array for no foo match"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", /foo/),
|
||||
expected: [panel.controls[0], panel.controls[2], panel.controls[3], panel.controls[4]],
|
||||
message: "(regexp literal) got three controls containing string"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", /foo$/),
|
||||
expected: [panel.controls[0], panel.controls[4]],
|
||||
message: "(regexp literal) got three controls ending with string"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", /\s/),
|
||||
expected: [panel.controls[3]],
|
||||
message: "(regexp literal) got control containing space"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", new RegExp("BAR", "i")),
|
||||
expected: [panel.controls[1], panel.controls[2], panel.controls[3]],
|
||||
message: "(regexp object) got layers ignoring case"
|
||||
}, {
|
||||
got: panel.getControlsBy("foo", {test: function(str) {return str.length > 3;}}),
|
||||
expected: [panel.controls[2], panel.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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
@@ -187,6 +187,40 @@
|
||||
t.ok(g_X == window.event, "window.event properly passed as first argument when nothing specified");
|
||||
}
|
||||
|
||||
function test_Array_filter(t) {
|
||||
|
||||
t.plan(8);
|
||||
|
||||
OpenLayers.Array.filter(["foo"], function(item, index, array) {
|
||||
t.eq(item, "foo", "callback called with proper item");
|
||||
t.eq(index, 0, "callback called with proper index");
|
||||
t.eq(array, ["foo"], "callback called with proper array");
|
||||
t.eq(this, {"foo": "bar"}, "callback called with this set properly");
|
||||
}, {"foo": "bar"});
|
||||
|
||||
var array = [0, 1, 2, 3];
|
||||
var select = OpenLayers.Array.filter(array, function(value) {
|
||||
return value > 1;
|
||||
});
|
||||
t.eq(select, [2, 3], "filter works for basic callback");
|
||||
t.eq(array, [0, 1, 2, 3], "filter doesn't modify original");
|
||||
|
||||
var obj = {
|
||||
test: function(value) {
|
||||
if(value > 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
};
|
||||
var select = OpenLayers.Array.filter(array, function(value) {
|
||||
return this.test(value);
|
||||
}, obj);
|
||||
t.eq(select, [2, 3], "filter works for callback and caller");
|
||||
t.eq(array, [0, 1, 2, 3], "filter doesn't modify original");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user