@@ -1,6 +1,10 @@
|
||||
goog.provide('ol.filter.Logical');
|
||||
goog.provide('ol.filter.LogicalOperator');
|
||||
goog.provide('ol.filter.and');
|
||||
goog.provide('ol.filter.not');
|
||||
goog.provide('ol.filter.or');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('ol.filter.Filter');
|
||||
|
||||
|
||||
@@ -9,7 +13,7 @@ goog.require('ol.filter.Filter');
|
||||
* @constructor
|
||||
* @extends {ol.filter.Filter}
|
||||
* @param {Array.<ol.filter.Filter>} filters Filters to and-combine.
|
||||
* @param {!ol.filter.LogicalOperator} operator Operator.
|
||||
* @param {ol.filter.LogicalOperator} operator Operator.
|
||||
*/
|
||||
ol.filter.Logical = function(filters, operator) {
|
||||
goog.base(this);
|
||||
@@ -19,9 +23,10 @@ ol.filter.Logical = function(filters, operator) {
|
||||
* @private
|
||||
*/
|
||||
this.filters_ = filters;
|
||||
goog.asserts.assert(filters.length > 0, 'Must supply at least one filter');
|
||||
|
||||
/**
|
||||
* @type {!ol.filter.LogicalOperator}
|
||||
* @type {ol.filter.LogicalOperator}
|
||||
*/
|
||||
this.operator = operator;
|
||||
|
||||
@@ -35,14 +40,29 @@ goog.inherits(ol.filter.Logical, ol.filter.Filter);
|
||||
ol.filter.Logical.prototype.applies = function(feature) {
|
||||
var filters = this.filters_,
|
||||
i = 0, ii = filters.length,
|
||||
operator = this.operator,
|
||||
start = operator(true, false),
|
||||
result = start;
|
||||
while (result === start && i < ii) {
|
||||
result = operator(result, filters[i].applies(feature));
|
||||
++i;
|
||||
result;
|
||||
switch (this.operator) {
|
||||
case ol.filter.LogicalOperator.AND:
|
||||
result = true;
|
||||
while (result && i < ii) {
|
||||
result = result && filters[i].applies(feature);
|
||||
++i;
|
||||
}
|
||||
break;
|
||||
case ol.filter.LogicalOperator.OR:
|
||||
result = false;
|
||||
while (!result && i < ii) {
|
||||
result = result || filters[i].applies(feature);
|
||||
++i;
|
||||
}
|
||||
break;
|
||||
case ol.filter.LogicalOperator.NOT:
|
||||
result = !filters[i].applies(feature);
|
||||
break;
|
||||
default:
|
||||
goog.asserts.assert(false, 'Unsupported operation: ' + this.operator);
|
||||
}
|
||||
return result;
|
||||
return !!result;
|
||||
};
|
||||
|
||||
|
||||
@@ -55,9 +75,44 @@ ol.filter.Logical.prototype.getFilters = function() {
|
||||
|
||||
|
||||
/**
|
||||
* @enum {!Function}
|
||||
* @enum {string}
|
||||
*/
|
||||
ol.filter.LogicalOperator = {
|
||||
AND: /** @return {boolean} result. */ function(a, b) { return a && b; },
|
||||
OR: /** @return {boolean} result. */ function(a, b) { return a || b; }
|
||||
AND: '&&',
|
||||
OR: '||',
|
||||
NOT: '!'
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a filter that evaluates to true if all of the provided filters
|
||||
* evaluate to true.
|
||||
* @param {...ol.filter.Filter} var_filters Filters.
|
||||
* @return {ol.filter.Logical} A logical AND filter.
|
||||
*/
|
||||
ol.filter.and = function(var_filters) {
|
||||
var filters = Array.prototype.slice.call(arguments);
|
||||
return new ol.filter.Logical(filters, ol.filter.LogicalOperator.AND);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a new filter that is the logical compliment of another.
|
||||
* @param {ol.filter.Filter} filter The filter to negate.
|
||||
* @return {ol.filter.Logical} A logical NOT filter.
|
||||
*/
|
||||
ol.filter.not = function(filter) {
|
||||
return new ol.filter.Logical([filter], ol.filter.LogicalOperator.NOT);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Create a filter that evaluates to true if any of the provided filters
|
||||
* evaluate to true.
|
||||
* @param {...ol.filter.Filter} var_filters Filters.
|
||||
* @return {ol.filter.Logical} A logical OR filter.
|
||||
*/
|
||||
ol.filter.or = function(var_filters) {
|
||||
var filters = Array.prototype.slice.call(arguments);
|
||||
return new ol.filter.Logical(filters, ol.filter.LogicalOperator.OR);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user