Skip to content

Commit

Permalink
Refactor code using pyUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Nechyporenko committed Nov 24, 2023
1 parent 390e275 commit c796370
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,71 +26,40 @@ odoo.define('crnd_web_tree_colored_field', function (require) {
}
if (nodeOptions.field_label_color_expression) {
var expression = nodeOptions.field_label_color_expression;
var label_color = this._getColorBasedOnExpression(record.data, expression);
var ctx = _.extend({}, record.data, pyUtils.context());
var label_color = this._getColorBasedOnExpression(ctx, expression);
$td.css('color', label_color);
}
if (nodeOptions.field_bg_color_expression) {
var expression = nodeOptions.field_bg_color_expression;
var bg_color = this._getColorBasedOnExpression(record.data, expression)
var ctx = _.extend({}, record.data, pyUtils.context());
var bg_color = this._getColorBasedOnExpression(ctx, expression)
$td.css('background-color', bg_color);
}
return $td;
},
_getColorBasedOnExpression: function (obj, expr) {
_getColorBasedOnExpression: function (ctx, expr) {
// Split the expression into conditions
var conditions = expr.split(';');

// Iterate through conditions
for (var i = 0; i < conditions.length; i++) {
var condition = conditions[i].trim();

// Split each condition into color and expression
// Split each condition into color and statement
var [color, statement] = condition.split(':');

// Remove leading and trailing spaces from the statement
statement = statement.trim();
// Convert statement to evaluate it, remove leading and trailing spaces
statement = py.parse(py.tokenize(statement.trim()));

// Evaluate the expression
if (this._checkCondition(obj, statement)) {
// Evaluate the statement
if (py.evaluate(statement, ctx).toJSON()){
return color;
}
}

// Return false if no condition is met
return false;
},
_checkCondition: function (obj, statement) {

// Split the statement into property, operator, and value
var [property, operator, value] = statement.split(/(===|!==|==|!=)/);

// Remove leading and trailing spaces
property = property.trim();
operator = operator.trim();
value = value.trim();

// Remove both single and double quotes around the value if present
value = value.replace(/^['"]|['"]$/g, '');

// Check if the property exists and evaluate the condition based on the operator
if (obj.hasOwnProperty(property)) {
switch (operator) {
case '===':
return obj[property] === value;
case '!==':
return obj[property] !== value;
case '==':
return obj[property] == value;
case '!=':
return obj[property] != value;
default:
// Default to '===' if the operator is not recognized
return obj[property] === value;
}
}

// Return false if the property doesn't exist
return false;
}
});
});
2 changes: 1 addition & 1 deletion test_crnd_web_models/views/tree_colored_field.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#2F4F4F:label_state == 'warning';
#00008B:label_state == 'ok'&quot;,
&quot;field_bg_color_expression&quot;: &quot;
#E6E6FA:bg_state == fail;
#E6E6FA:bg_state == 'fail';
#98FB98:bg_state == 'warning';
#D3D3D3:bg_state == 'ok'&quot;}"/>
<field name="label_state"/>
Expand Down

0 comments on commit c796370

Please sign in to comment.