-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for multiple expressions with passing tests #5232
Changes from 4 commits
ae9745c
1b681c3
647fdfc
c1a11d0
2be73f6
995929a
b18a351
b08c03c
8930949
be3933e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,6 @@ define([ | |
Expression) { | ||
'use strict'; | ||
|
||
var expressionPlaceholder = /\$\{expression}/g; | ||
|
||
/** | ||
* Evaluates a conditions expression defined using the | ||
* {@link https://github.com/AnalyticalGraphicsInc/3d-tiles/tree/master/Styling|3D Tiles Styling language}. | ||
|
@@ -32,9 +30,14 @@ define([ | |
* @example | ||
* var expression = new Cesium.Expression({ | ||
* expression : 'regExp("^1(\\d)").exec(${id})', | ||
* expressions : { | ||
* id : "RegEx('^1(\\d)$').exec(${id})", | ||
* Area : "${length} * ${height}" | ||
* }, | ||
* conditions : [ | ||
* ['${expression} === "1"', 'color("#FF0000")'], | ||
* ['${expression} === "2"', 'color("#00FF00")'], | ||
* ["(${id} !== 1) && (${Area} > 0)", "color('#0000FF')"], | ||
* ['true', 'color("#FFFFFF")'] | ||
* ] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For clarity, remove the new line and use |
||
* }); | ||
|
@@ -45,8 +48,13 @@ define([ | |
function ConditionsExpression(conditionsExpression) { | ||
this._conditionsExpression = clone(conditionsExpression, true); | ||
this._conditions = conditionsExpression.conditions; | ||
this._expression = conditionsExpression.expression; | ||
|
||
// Insert expression to expressions if it exists | ||
// Then evaluate using expressions throughout class | ||
// this._expression has to stay in prototype for specs to keep passing, but it can be removed in the future. | ||
this._expression = conditionsExpression.expression; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As part of this PR can you remove any uses of |
||
this._expressions = conditionsExpression.expressions || {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We try to avoid this type of syntax, and also the allocation. Instead use |
||
this._expressions.expression = conditionsExpression.expression; | ||
this._runtimeConditions = undefined; | ||
|
||
setRuntime(this); | ||
|
@@ -79,19 +87,27 @@ define([ | |
var runtimeConditions = []; | ||
var conditions = expression._conditions; | ||
if (defined(conditions)) { | ||
var exp = expression._expression; | ||
var expressions = expression._expressions; | ||
var length = conditions.length; | ||
for (var i = 0; i < length; ++i) { | ||
var statement = conditions[i]; | ||
var cond = String(statement[0]); | ||
var condExpression = String(statement[1]); | ||
if (defined(exp)) { | ||
cond = cond.replace(expressionPlaceholder, exp); | ||
condExpression = condExpression.replace(expressionPlaceholder, exp); | ||
} else { | ||
cond = cond.replace(expressionPlaceholder, 'undefined'); | ||
condExpression = condExpression.replace(expressionPlaceholder, 'undefined'); | ||
|
||
//Loop over all expressions for replacement instead of only replacing one | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shorten the description, just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also style note, change to |
||
for (var key in expressions) { | ||
if (expressions.hasOwnProperty((key))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unneeded parentheses. |
||
var expressionPlaceholder = new RegExp('\\$\\{' + key + '\\}', 'g'); | ||
if (defined(expressions[key])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Factor |
||
cond = cond.replace(expressionPlaceholder, expressions[key]); | ||
condExpression = condExpression.replace(expressionPlaceholder, expressions[key]); | ||
} else { | ||
cond = cond.replace(expressionPlaceholder, 'undefined'); | ||
condExpression = condExpression.replace(expressionPlaceholder, 'undefined'); | ||
} | ||
} | ||
} | ||
|
||
runtimeConditions.push(new Statement( | ||
new Expression(cond), | ||
new Expression(condExpression) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is technically valid if
${id}
is a built-in tile property, but as an example it is confusing to seeid
reference${id}
. Maybe use${name}
instead.(yeah... this was my example in #5153)