Skip to content

Commit

Permalink
Improve colorizer to use expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Nechyporenko committed Nov 21, 2023
1 parent e69b30a commit 472e5b1
Showing 1 changed file with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,73 @@ odoo.define('crnd_web_tree_colored_field', function (require) {
var label_color = record.data[nodeOptions.field_label_color];
$td.css('color', label_color);
}
if (nodeOptions.field_label_color_expression) {
var expression = nodeOptions.field_label_color_expression;
var label_color = this._getColorBasedOnExpression(record.data, 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)
$td.css('background-color', bg_color);
}
return $td;
},
_getColorBasedOnExpression: function (obj, 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
var [color, statement] = condition.split(':');

// Remove leading and trailing spaces from the statement
statement = statement.trim();

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

// Default color if no condition is met
return '$table-dark-color';
},
_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;
}
});
});

0 comments on commit 472e5b1

Please sign in to comment.