Using ol.expression.parse

This commit is contained in:
Tim Schaub
2013-06-12 17:45:17 -06:00
parent 53abedaada
commit 5e309e244b
13 changed files with 145 additions and 298 deletions

View File

@@ -519,34 +519,35 @@
/**
* @typedef {Object} ol.style.IconOptions
* @property {string|ol.Expression} url Icon image url.
* @property {number|ol.Expression|undefined} width Width of the icon in pixels.
* Default is the width of the icon image.
* @property {number|ol.Expression|undefined} height Height of the icon in
* pixels. Default is the height of the icon image.
* @property {number|ol.Expression|undefined} opacity Icon opacity (0-1).
* @property {number|ol.Expression|undefined} rotation Rotation in degrees
* (0-360).
* @property {string|ol.expression.Expression} url Icon image url.
* @property {number|ol.expression.Expression|undefined} width Width of the icon
* in pixels. Default is the width of the icon image.
* @property {number|ol.expression.Expression|undefined} height Height of the
* icon in pixels. Default is the height of the icon image.
* @property {number|ol.expression.Expression|undefined} opacity Icon opacity
* (0-1).
* @property {number|ol.expression.Expression|undefined} rotation Rotation in
* degrees (0-360).
*/
/**
* @typedef {Object} ol.style.LineOptions
* @property {string|ol.Expression|undefined} strokeColor Stroke color as hex
* color code.
* @property {number|ol.Expression|undefined} strokeWidth Stroke width in
* pixels.
* @property {number|ol.Expression|undefined} opacity Opacity (0-1).
* @property {string|ol.expression.Expression|undefined} strokeColor Stroke
* color as hex color code.
* @property {number|ol.expression.Expression|undefined} strokeWidth Stroke
* width in pixels.
* @property {number|ol.expression.Expression|undefined} opacity Opacity (0-1).
*/
/**
* @typedef {Object} ol.style.PolygonOptions
* @property {string|ol.Expression|undefined} fillColor Fill color as hex color
* code.
* @property {string|ol.Expression|undefined} strokeColor Stroke color as hex
* color code.
* @property {number|ol.Expression|undefined} strokeWidth Stroke width in
* pixels.
* @property {number|ol.Expression|undefined} opacity Opacity (0-1).
* @property {string|ol.expression.Expression|undefined} fillColor Fill color as
* hex color code.
* @property {string|ol.expression.Expression|undefined} strokeColor Stroke
* color as hex color code.
* @property {number|ol.expression.Expression|undefined} strokeWidth Stroke
* width in pixels.
* @property {number|ol.expression.Expression|undefined} opacity Opacity (0-1).
*/
/**
@@ -558,14 +559,14 @@
/**
* @typedef {Object} ol.style.ShapeOptions
* @property {ol.style.ShapeType|undefined} type Type.
* @property {number|ol.Expression|undefined} size Size in pixels.
* @property {string|ol.Expression|undefined} fillColor Fill color as hex color
* code.
* @property {string|ol.Expression|undefined} strokeColor Stroke color as hex
* color code.
* @property {number|ol.Expression|undefined} strokeWidth Stroke width in
* pixels.
* @property {number|ol.Expression|undefined} opacity Opacity (0-1).
* @property {number|ol.expression.Expression|undefined} size Size in pixels.
* @property {string|ol.expression.Expression|undefined} fillColor Fill color as
* hex color code.
* @property {string|ol.expression.Expression|undefined} strokeColor Stroke
* color as hex color code.
* @property {number|ol.expression.Expression|undefined} strokeWidth Stroke
* width in pixels.
* @property {number|ol.expression.Expression|undefined} opacity Opacity (0-1).
*/
/**

View File

@@ -1 +0,0 @@
@exportSymbol ol.Expression

View File

@@ -1,80 +0,0 @@
goog.provide('ol.Expression');
goog.provide('ol.ExpressionLiteral');
/**
* Create a new expression. Expressions are used for instance to bind
* symbolizer properties to feature attributes.
*
* Example:
*
* // take the color from the color attribute
* color: new ol.Expression('color');
* // take the strokeWidth from the width attribute and multiply by 2.
* strokeWidth: new ol.Expression('width*2');
*
* @constructor
* @param {string} source Expression to be evaluated.
*/
ol.Expression = function(source) {
/**
* @type {string}
* @private
*/
this.source_ = source;
};
/**
* Evaluate the expression and return the result.
*
* @param {Object=} opt_thisArg Object to use as this when evaluating the
* expression. If not provided, the global object will be used.
* @param {Object=} opt_scope Evaluation scope. All properties of this object
* will be available as variables when evaluating the expression. If not
* provided, the global object will be used.
* @return {*} Result of the expression.
*/
ol.Expression.prototype.evaluate = function(opt_thisArg, opt_scope) {
var thisArg = goog.isDef(opt_thisArg) ? opt_thisArg : goog.global,
scope = goog.isDef(opt_scope) ? opt_scope : goog.global,
names = [],
values = [];
for (var name in scope) {
names.push(name);
values.push(scope[name]);
}
var evaluator = new Function(names.join(','), 'return ' + this.source_);
return evaluator.apply(thisArg, values);
};
/**
* @constructor
* @extends {ol.Expression}
* @param {*} value Literal value.
*/
ol.ExpressionLiteral = function(value) {
/**
* @type {*}
* @private
*/
this.value_ = value;
};
goog.inherits(ol.ExpressionLiteral, ol.Expression);
/**
* @inheritDoc
*/
ol.ExpressionLiteral.prototype.evaluate = function(opt_thisArg, opt_scope) {
return this.value_;
};

View File

@@ -3,8 +3,8 @@ goog.provide('ol.style.IconLiteral');
goog.provide('ol.style.IconType');
goog.require('goog.asserts');
goog.require('ol.Expression');
goog.require('ol.ExpressionLiteral');
goog.require('ol.expression.Expression');
goog.require('ol.expression.Literal');
goog.require('ol.style.Point');
goog.require('ol.style.PointLiteral');
@@ -69,47 +69,47 @@ ol.style.Icon = function(options) {
goog.asserts.assert(options.url, 'url must be set');
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.url_ = (options.url instanceof ol.Expression) ?
options.url : new ol.ExpressionLiteral(options.url);
this.url_ = (options.url instanceof ol.expression.Expression) ?
options.url : new ol.expression.Literal(options.url);
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.width_ = !goog.isDef(options.width) ?
null :
(options.width instanceof ol.Expression) ?
options.width : new ol.ExpressionLiteral(options.width);
(options.width instanceof ol.expression.Expression) ?
options.width : new ol.expression.Literal(options.width);
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.height_ = !goog.isDef(options.height) ?
null :
(options.height instanceof ol.Expression) ?
options.height : new ol.ExpressionLiteral(options.height);
(options.height instanceof ol.expression.Expression) ?
options.height : new ol.expression.Literal(options.height);
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.opacity_ = !goog.isDef(options.opacity) ?
new ol.ExpressionLiteral(ol.style.IconDefaults.opacity) :
(options.opacity instanceof ol.Expression) ?
options.opacity : new ol.ExpressionLiteral(options.opacity);
new ol.expression.Literal(ol.style.IconDefaults.opacity) :
(options.opacity instanceof ol.expression.Expression) ?
options.opacity : new ol.expression.Literal(options.opacity);
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.rotation_ = !goog.isDef(options.rotation) ?
new ol.ExpressionLiteral(ol.style.IconDefaults.rotation) :
(options.rotation instanceof ol.Expression) ?
options.rotation : new ol.ExpressionLiteral(options.rotation);
new ol.expression.Literal(ol.style.IconDefaults.rotation) :
(options.rotation instanceof ol.expression.Expression) ?
options.rotation : new ol.expression.Literal(options.rotation);
};
@@ -121,24 +121,25 @@ ol.style.Icon = function(options) {
ol.style.Icon.prototype.createLiteral = function(feature) {
var attrs = feature && feature.getAttributes();
var url = /** @type {string} */ (this.url_.evaluate(feature, attrs));
var url = /** @type {string} */ (this.url_.evaluate(attrs, null, feature));
goog.asserts.assert(goog.isString(url) && url != '#', 'url must be a string');
var width = /** @type {number|undefined} */ (goog.isNull(this.width_) ?
undefined : this.width_.evaluate(feature, attrs));
undefined : this.width_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(width) || goog.isNumber(width),
'width must be undefined or a number');
var height = /** @type {number|undefined} */ (goog.isNull(this.height_) ?
undefined : this.height_.evaluate(feature, attrs));
undefined : this.height_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(height) || goog.isNumber(height),
'height must be undefined or a number');
var opacity = /** {@type {number} */ (this.opacity_.evaluate(feature, attrs));
var opacity = /** {@type {number} */ (this.opacity_.evaluate(attrs, null,
feature));
goog.asserts.assertNumber(opacity, 'opacity must be a number');
var rotation =
/** {@type {number} */ (this.rotation_.evaluate(feature, attrs));
/** {@type {number} */ (this.rotation_.evaluate(attrs, null, feature));
goog.asserts.assertNumber(rotation, 'rotation must be a number');
return new ol.style.IconLiteral({

View File

@@ -2,8 +2,8 @@ goog.provide('ol.style.Line');
goog.provide('ol.style.LineLiteral');
goog.require('goog.asserts');
goog.require('ol.Expression');
goog.require('ol.ExpressionLiteral');
goog.require('ol.expression.Expression');
goog.require('ol.expression.Literal');
goog.require('ol.style.Symbolizer');
goog.require('ol.style.SymbolizerLiteral');
@@ -63,31 +63,31 @@ ol.style.Line = function(options) {
goog.base(this);
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.strokeColor_ = !goog.isDef(options.strokeColor) ?
new ol.ExpressionLiteral(ol.style.LineDefaults.strokeColor) :
(options.strokeColor instanceof ol.Expression) ?
options.strokeColor : new ol.ExpressionLiteral(options.strokeColor);
new ol.expression.Literal(ol.style.LineDefaults.strokeColor) :
(options.strokeColor instanceof ol.expression.Expression) ?
options.strokeColor : new ol.expression.Literal(options.strokeColor);
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.strokeWidth_ = !goog.isDef(options.strokeWidth) ?
new ol.ExpressionLiteral(ol.style.LineDefaults.strokeWidth) :
(options.strokeWidth instanceof ol.Expression) ?
options.strokeWidth : new ol.ExpressionLiteral(options.strokeWidth);
new ol.expression.Literal(ol.style.LineDefaults.strokeWidth) :
(options.strokeWidth instanceof ol.expression.Expression) ?
options.strokeWidth : new ol.expression.Literal(options.strokeWidth);
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.opacity_ = !goog.isDef(options.opacity) ?
new ol.ExpressionLiteral(ol.style.LineDefaults.opacity) :
(options.opacity instanceof ol.Expression) ?
options.opacity : new ol.ExpressionLiteral(options.opacity);
new ol.expression.Literal(ol.style.LineDefaults.opacity) :
(options.opacity instanceof ol.expression.Expression) ?
options.opacity : new ol.expression.Literal(options.opacity);
};
goog.inherits(ol.style.Line, ol.style.Symbolizer);
@@ -104,13 +104,13 @@ ol.style.Line.prototype.createLiteral = function(opt_feature) {
attrs = feature.getAttributes();
}
var strokeColor = this.strokeColor_.evaluate(feature, attrs);
var strokeColor = this.strokeColor_.evaluate(attrs, null, feature);
goog.asserts.assertString(strokeColor, 'strokeColor must be a string');
var strokeWidth = this.strokeWidth_.evaluate(feature, attrs);
var strokeWidth = this.strokeWidth_.evaluate(attrs, null, feature);
goog.asserts.assertNumber(strokeWidth, 'strokeWidth must be a number');
var opacity = this.opacity_.evaluate(feature, attrs);
var opacity = this.opacity_.evaluate(attrs, null, feature);
goog.asserts.assertNumber(opacity, 'opacity must be a number');
return new ol.style.LineLiteral({

View File

@@ -2,8 +2,8 @@ goog.provide('ol.style.Polygon');
goog.provide('ol.style.PolygonLiteral');
goog.require('goog.asserts');
goog.require('ol.Expression');
goog.require('ol.ExpressionLiteral');
goog.require('ol.expression.Expression');
goog.require('ol.expression.Literal');
goog.require('ol.style.Symbolizer');
goog.require('ol.style.SymbolizerLiteral');
@@ -80,13 +80,13 @@ ol.style.Polygon = function(options) {
goog.base(this);
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.fillColor_ = !goog.isDefAndNotNull(options.fillColor) ?
null :
(options.fillColor instanceof ol.Expression) ?
options.fillColor : new ol.ExpressionLiteral(options.fillColor);
(options.fillColor instanceof ol.expression.Expression) ?
options.fillColor : new ol.expression.Literal(options.fillColor);
// stroke handling - if any stroke property is supplied, use defaults
var strokeColor = null,
@@ -96,24 +96,26 @@ ol.style.Polygon = function(options) {
goog.isDefAndNotNull(options.strokeWidth)) {
strokeColor = !goog.isDefAndNotNull(options.strokeColor) ?
new ol.ExpressionLiteral(ol.style.PolygonDefaults.strokeColor) :
(options.strokeColor instanceof ol.Expression) ?
options.strokeColor : new ol.ExpressionLiteral(options.strokeColor);
new ol.expression.Literal(ol.style.PolygonDefaults.strokeColor) :
(options.strokeColor instanceof ol.expression.Expression) ?
options.strokeColor :
new ol.expression.Literal(options.strokeColor);
strokeWidth = !goog.isDef(options.strokeWidth) ?
new ol.ExpressionLiteral(ol.style.PolygonDefaults.strokeWidth) :
(options.strokeWidth instanceof ol.Expression) ?
options.strokeWidth : new ol.ExpressionLiteral(options.strokeWidth);
new ol.expression.Literal(ol.style.PolygonDefaults.strokeWidth) :
(options.strokeWidth instanceof ol.expression.Expression) ?
options.strokeWidth :
new ol.expression.Literal(options.strokeWidth);
}
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.strokeColor_ = strokeColor;
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.strokeWidth_ = strokeWidth;
@@ -124,13 +126,13 @@ ol.style.Polygon = function(options) {
'Stroke or fill properties must be provided');
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.opacity_ = !goog.isDef(options.opacity) ?
new ol.ExpressionLiteral(ol.style.PolygonDefaults.opacity) :
(options.opacity instanceof ol.Expression) ?
options.opacity : new ol.ExpressionLiteral(options.opacity);
new ol.expression.Literal(ol.style.PolygonDefaults.opacity) :
(options.opacity instanceof ol.expression.Expression) ?
options.opacity : new ol.expression.Literal(options.opacity);
};
goog.inherits(ol.style.Polygon, ol.style.Symbolizer);
@@ -149,17 +151,17 @@ ol.style.Polygon.prototype.createLiteral = function(opt_feature) {
var fillColor = goog.isNull(this.fillColor_) ?
undefined :
/** @type {string} */ (this.fillColor_.evaluate(feature, attrs));
/** @type {string} */ (this.fillColor_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(fillColor) || goog.isString(fillColor));
var strokeColor = goog.isNull(this.strokeColor_) ?
undefined :
/** @type {string} */ (this.strokeColor_.evaluate(feature, attrs));
/** @type {string} */ (this.strokeColor_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(strokeColor) || goog.isString(strokeColor));
var strokeWidth = goog.isNull(this.strokeWidth_) ?
undefined :
/** @type {number} */ (this.strokeWidth_.evaluate(feature, attrs));
/** @type {number} */ (this.strokeWidth_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(strokeWidth) || goog.isNumber(strokeWidth));
goog.asserts.assert(
@@ -167,7 +169,7 @@ ol.style.Polygon.prototype.createLiteral = function(opt_feature) {
(goog.isDef(strokeColor) && goog.isDef(strokeWidth)),
'either fill style or strokeColor and strokeWidth must be defined');
var opacity = this.opacity_.evaluate(feature, attrs);
var opacity = this.opacity_.evaluate(attrs, null, feature);
goog.asserts.assertNumber(opacity, 'opacity must be a number');
return new ol.style.PolygonLiteral({

View File

@@ -3,8 +3,8 @@ goog.provide('ol.style.ShapeLiteral');
goog.provide('ol.style.ShapeType');
goog.require('goog.asserts');
goog.require('ol.Expression');
goog.require('ol.ExpressionLiteral');
goog.require('ol.expression.Expression');
goog.require('ol.expression.Literal');
goog.require('ol.style.Point');
goog.require('ol.style.PointLiteral');
@@ -106,22 +106,22 @@ ol.style.Shape = function(options) {
options.type : ol.style.ShapeDefaults.type);
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.size_ = !goog.isDef(options.size) ?
new ol.ExpressionLiteral(ol.style.ShapeDefaults.size) :
(options.size instanceof ol.Expression) ?
options.size : new ol.ExpressionLiteral(options.size);
new ol.expression.Literal(ol.style.ShapeDefaults.size) :
(options.size instanceof ol.expression.Expression) ?
options.size : new ol.expression.Literal(options.size);
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.fillColor_ = !goog.isDefAndNotNull(options.fillColor) ?
null :
(options.fillColor instanceof ol.Expression) ?
options.fillColor : new ol.ExpressionLiteral(options.fillColor);
(options.fillColor instanceof ol.expression.Expression) ?
options.fillColor : new ol.expression.Literal(options.fillColor);
// stroke handling - if any stroke property is supplied, use defaults
var strokeColor = null,
@@ -131,24 +131,26 @@ ol.style.Shape = function(options) {
goog.isDefAndNotNull(options.strokeWidth)) {
strokeColor = !goog.isDefAndNotNull(options.strokeColor) ?
new ol.ExpressionLiteral(ol.style.ShapeDefaults.strokeColor) :
(options.strokeColor instanceof ol.Expression) ?
options.strokeColor : new ol.ExpressionLiteral(options.strokeColor);
new ol.expression.Literal(ol.style.ShapeDefaults.strokeColor) :
(options.strokeColor instanceof ol.expression.Expression) ?
options.strokeColor :
new ol.expression.Literal(options.strokeColor);
strokeWidth = !goog.isDef(options.strokeWidth) ?
new ol.ExpressionLiteral(ol.style.ShapeDefaults.strokeWidth) :
(options.strokeWidth instanceof ol.Expression) ?
options.strokeWidth : new ol.ExpressionLiteral(options.strokeWidth);
new ol.expression.Literal(ol.style.ShapeDefaults.strokeWidth) :
(options.strokeWidth instanceof ol.expression.Expression) ?
options.strokeWidth :
new ol.expression.Literal(options.strokeWidth);
}
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.strokeColor_ = strokeColor;
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.strokeWidth_ = strokeWidth;
@@ -159,13 +161,13 @@ ol.style.Shape = function(options) {
'Stroke or fill properties must be provided');
/**
* @type {ol.Expression}
* @type {ol.expression.Expression}
* @private
*/
this.opacity_ = !goog.isDef(options.opacity) ?
new ol.ExpressionLiteral(ol.style.ShapeDefaults.opacity) :
(options.opacity instanceof ol.Expression) ?
options.opacity : new ol.ExpressionLiteral(options.opacity);
new ol.expression.Literal(ol.style.ShapeDefaults.opacity) :
(options.opacity instanceof ol.expression.Expression) ?
options.opacity : new ol.expression.Literal(options.opacity);
};
@@ -181,22 +183,22 @@ ol.style.Shape.prototype.createLiteral = function(opt_feature) {
attrs = feature.getAttributes();
}
var size = this.size_.evaluate(feature, attrs);
var size = this.size_.evaluate(attrs, null, feature);
goog.asserts.assertNumber(size, 'size must be a number');
var fillColor = goog.isNull(this.fillColor_) ?
undefined :
/** @type {string} */ (this.fillColor_.evaluate(feature, attrs));
/** @type {string} */ (this.fillColor_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(fillColor) || goog.isString(fillColor));
var strokeColor = goog.isNull(this.strokeColor_) ?
undefined :
/** @type {string} */ (this.strokeColor_.evaluate(feature, attrs));
/** @type {string} */ (this.strokeColor_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(strokeColor) || goog.isString(strokeColor));
var strokeWidth = goog.isNull(this.strokeWidth_) ?
undefined :
/** @type {number} */ (this.strokeWidth_.evaluate(feature, attrs));
/** @type {number} */ (this.strokeWidth_.evaluate(attrs, null, feature));
goog.asserts.assert(!goog.isDef(strokeWidth) || goog.isNumber(strokeWidth));
goog.asserts.assert(
@@ -204,7 +206,7 @@ ol.style.Shape.prototype.createLiteral = function(opt_feature) {
(goog.isDef(strokeColor) && goog.isDef(strokeWidth)),
'either fill style or strokeColor and strokeWidth must be defined');
var opacity = this.opacity_.evaluate(feature, attrs);
var opacity = this.opacity_.evaluate(attrs, null, feature);
goog.asserts.assertNumber(opacity, 'opacity must be a number');
return new ol.style.ShapeLiteral({