Accept an expression string for rule filter

This commit is contained in:
Tim Schaub
2013-06-17 17:30:44 -06:00
parent 38b784d672
commit a663d8fcae
6 changed files with 30 additions and 22 deletions
+3 -10
View File
@@ -3,9 +3,6 @@ goog.require('ol.RendererHint');
goog.require('ol.View2D'); goog.require('ol.View2D');
goog.require('ol.control.defaults'); goog.require('ol.control.defaults');
goog.require('ol.expression'); goog.require('ol.expression');
goog.require('ol.filter.Filter');
goog.require('ol.filter.Geometry');
goog.require('ol.geom.GeometryType');
goog.require('ol.layer.Vector'); goog.require('ol.layer.Vector');
goog.require('ol.parser.GeoJSON'); goog.require('ol.parser.GeoJSON');
goog.require('ol.proj'); goog.require('ol.proj');
@@ -19,9 +16,7 @@ goog.require('ol.style.Text');
var style = new ol.style.Style({rules: [ var style = new ol.style.Style({rules: [
new ol.style.Rule({ new ol.style.Rule({
filter: new ol.filter.Filter(function(feature) { filter: 'where == "outer"',
return feature.get('where') == 'outer';
}),
symbolizers: [ symbolizers: [
new ol.style.Line({ new ol.style.Line({
strokeColor: ol.expression.parse('color'), strokeColor: ol.expression.parse('color'),
@@ -31,9 +26,7 @@ var style = new ol.style.Style({rules: [
] ]
}), }),
new ol.style.Rule({ new ol.style.Rule({
filter: new ol.filter.Filter(function(feature) { filter: 'where == "inner"',
return feature.get('where') == 'inner';
}),
symbolizers: [ symbolizers: [
new ol.style.Line({ new ol.style.Line({
strokeColor: '#013', strokeColor: '#013',
@@ -48,7 +41,7 @@ var style = new ol.style.Style({rules: [
] ]
}), }),
new ol.style.Rule({ new ol.style.Rule({
filter: new ol.filter.Geometry(ol.geom.GeometryType.POINT), filter: 'geometryType("point")',
symbolizers: [ symbolizers: [
new ol.style.Shape({ new ol.style.Shape({
size: 40, size: 40,
+1 -1
View File
@@ -552,7 +552,7 @@
/** /**
* @typedef {Object} ol.style.RuleOptions * @typedef {Object} ol.style.RuleOptions
* @property {ol.filter.Filter|undefined} filter Filter. * @property {ol.expression.Expression|string|undefined} filter Filter.
* @property {Array.<ol.style.Symbolizer>|undefined} symbolizers Symbolizers. * @property {Array.<ol.style.Symbolizer>|undefined} symbolizers Symbolizers.
*/ */
+3
View File
@@ -0,0 +1,3 @@
/**
* @namespace ol.expression
*/
+19 -4
View File
@@ -1,7 +1,10 @@
goog.provide('ol.style.Rule'); goog.provide('ol.style.Rule');
goog.require('goog.asserts');
goog.require('ol.Feature'); goog.require('ol.Feature');
goog.require('ol.filter.Filter'); goog.require('ol.expression');
goog.require('ol.expression.Expression');
goog.require('ol.style.Symbolizer'); goog.require('ol.style.Symbolizer');
@@ -12,11 +15,22 @@ goog.require('ol.style.Symbolizer');
*/ */
ol.style.Rule = function(options) { ol.style.Rule = function(options) {
var filter = null;
if (goog.isDef(options.filter)) {
if (goog.isString(options.filter)) {
filter = ol.expression.parse(options.filter);
} else {
goog.asserts.assert(options.filter instanceof ol.expression.Expression);
filter = options.filter;
}
}
/** /**
* @type {ol.filter.Filter} * @type {ol.expression.Expression}
* @private * @private
*/ */
this.filter_ = goog.isDef(options.filter) ? options.filter : null; this.filter_ = filter;
/** /**
* @type {Array.<ol.style.Symbolizer>} * @type {Array.<ol.style.Symbolizer>}
@@ -33,7 +47,8 @@ ol.style.Rule = function(options) {
* @return {boolean} Does the rule apply to the feature? * @return {boolean} Does the rule apply to the feature?
*/ */
ol.style.Rule.prototype.applies = function(feature) { ol.style.Rule.prototype.applies = function(feature) {
return goog.isNull(this.filter_) ? true : this.filter_.applies(feature); return goog.isNull(this.filter_) ?
true : !!ol.expression.evaluateFeature(this.filter_, feature);
}; };
+3 -3
View File
@@ -13,14 +13,14 @@ describe('ol.style.Rule', function() {
it('returns false when the rule does not apply', function() { it('returns false when the rule does not apply', function() {
rule = new ol.style.Rule({ rule = new ol.style.Rule({
filter: new ol.filter.Filter(function() { return false; }) filter: new ol.expression.Literal(false)
}); });
expect(rule.applies(feature)).to.be(false); expect(rule.applies(feature)).to.be(false);
}); });
it('returns true when the rule applies', function() { it('returns true when the rule applies', function() {
rule = new ol.style.Rule({ rule = new ol.style.Rule({
filter: new ol.filter.Filter(function() { return true; }) filter: new ol.expression.Literal(true)
}); });
expect(rule.applies(feature)).to.be(true); expect(rule.applies(feature)).to.be(true);
}); });
@@ -29,5 +29,5 @@ describe('ol.style.Rule', function() {
}); });
goog.require('ol.Feature'); goog.require('ol.Feature');
goog.require('ol.filter.Filter'); goog.require('ol.expression.Literal');
goog.require('ol.style.Rule'); goog.require('ol.style.Rule');
+1 -4
View File
@@ -9,9 +9,7 @@ describe('ol.style.Style', function() {
var style = new ol.style.Style({ var style = new ol.style.Style({
rules: [ rules: [
new ol.style.Rule({ new ol.style.Rule({
filter: new ol.filter.Filter(function(feature) { filter: 'foo == "bar"',
return feature.get('foo') == 'bar';
}),
symbolizers: [ symbolizers: [
new ol.style.Shape({ new ol.style.Shape({
size: 4, size: 4,
@@ -66,7 +64,6 @@ goog.require('ol.Feature');
goog.require('ol.geom.LineString'); goog.require('ol.geom.LineString');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.filter.Filter');
goog.require('ol.style.Rule'); goog.require('ol.style.Rule');
goog.require('ol.style.Shape'); goog.require('ol.style.Shape');
goog.require('ol.style.ShapeLiteral'); goog.require('ol.style.ShapeLiteral');