From 4937f08971192cec1346ef85ef453d5864b68d20 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Thu, 3 Oct 2013 11:41:02 -0600 Subject: [PATCH] Adding counter expression function This function can e.g. be used to get a symbolizer literal with an ever increasing zIndex. --- src/ol/expr/expression.js | 18 ++++++++++++++++++ test/spec/ol/expr/expression.test.js | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/src/ol/expr/expression.js b/src/ol/expr/expression.js index f1376a0d9b..65a6411147 100644 --- a/src/ol/expr/expression.js +++ b/src/ol/expr/expression.js @@ -96,6 +96,7 @@ ol.expr.lib = {}; */ ol.expr.functions = { CONCAT: 'concat', + COUNTER: 'counter', EXTENT: 'extent', FID: 'fid', GEOMETRY_TYPE: 'geometryType', @@ -126,6 +127,23 @@ ol.expr.lib[ol.expr.functions.CONCAT] = function(var_args) { }; +/** + * Returns a counter which increases every time this function is called. + * @param {number=} opt_start Start. If not provided, the counter starts at 0. + * @return {number} Counter. + */ +ol.expr.lib[ol.expr.functions.COUNTER] = (function() { + var counter = 0; + return function(opt_start) { + var result = ++counter; + if (goog.isDef(opt_start)) { + result += opt_start; + } + return result; + }; +})(); + + /** * Determine if a feature's extent intersects the provided extent. * @param {number} minX Minimum x-coordinate value. diff --git a/test/spec/ol/expr/expression.test.js b/test/spec/ol/expr/expression.test.js index 9157867f3e..22e8b68606 100644 --- a/test/spec/ol/expr/expression.test.js +++ b/test/spec/ol/expr/expression.test.js @@ -674,6 +674,15 @@ describe('ol.expr.lib', function() { }); + describe('counter()', function() { + var counter = parse('counter()'); + var counterWithStart = parse('counter(1000)'); + var start = evaluate(counter); + expect(evaluate(counter).to.be(start + 1)); + expect(evaluate(counter).to.be(start + 2)); + expect(evaluate(counterWithStart).to.be(start + 3 + 1000)); + }); + describe('extent()', function() { var nw = new ol.Feature({