You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.1 KiB

  1. openerp.web_tree_dynamic_colored_field = function(instance){
  2. var _t = instance.web._t,
  3. _lt = instance.web._lt;
  4. var QWeb = instance.web.qweb;
  5. var pair_colors = function(pair_color){
  6. if (pair_color != ""){
  7. var pair_list = pair_color.split(':'),
  8. color = pair_list[0],
  9. expression = pair_list[1];
  10. return [color, py.parse(py.tokenize(expression)), expression]
  11. }
  12. };
  13. var colorize_helper = function(obj, record, column, field_attribute, css_attribute){
  14. var result = '';
  15. if (column[field_attribute]){
  16. var colors = _(column[field_attribute].split(';'))
  17. .chain()
  18. .map(pair_colors)
  19. .value();
  20. var colors = colors.filter(function CheckUndefined(value, index, ar) {
  21. return value != undefined;
  22. })
  23. var ctx = _.extend(
  24. {},
  25. record.attributes,
  26. {
  27. uid: obj.session.uid,
  28. current_date: new Date().toString('yyyy-MM-dd')
  29. }
  30. );
  31. for(i=0, len=colors.length; i<len; ++i) {
  32. pair = colors[i];
  33. var color = pair[0];
  34. var expression = pair[1];
  35. if (py.evaluate(expression, ctx).toJSON()) {
  36. result = css_attribute + ': ' + color + ';';
  37. }
  38. }
  39. }
  40. return result
  41. };
  42. var colorize = function(record, column){
  43. var res = '';
  44. res += colorize_helper(this, record, column, 'bg_color', 'background-color');
  45. res += colorize_helper(this, record, column, 'fg_color', 'color');
  46. return res;
  47. };
  48. instance.web.ListView.List.include({
  49. init: function(group, opts){
  50. this._super(group, opts);
  51. this.columns.fct_colorize = colorize;
  52. },
  53. fct_colorize: colorize,
  54. render: function() {
  55. this.$current.empty().append(
  56. QWeb.render('ListView.rows', _.extend({
  57. render_cell: function () {
  58. return self.render_cell.apply(self, arguments); },
  59. fct_colorize: function(){
  60. return self.fct_colorize.apply(self, arguments);
  61. }
  62. }, this)));
  63. this.pad_table_to(4);
  64. },
  65. render_record: function(record) {
  66. var self = this;
  67. var index = this.records.indexOf(record);
  68. return QWeb.render('ListView.row', {
  69. columns: this.columns,
  70. options: this.options,
  71. record: record,
  72. row_parity: (index % 2 === 0) ? 'even' : 'odd',
  73. view: this.view,
  74. render_cell: function () {
  75. return self.render_cell.apply(self, arguments); },
  76. fct_colorize: function(){
  77. return self.fct_colorize.apply(self, arguments);
  78. }
  79. });
  80. }
  81. });
  82. }