Browse Source

[IMP] kpi_dashboard: edit color

12.0
Enric Tobella 4 years ago
parent
commit
ee275c03bf
No known key found for this signature in database GPG Key ID: D76663C0B4023597
  1. 15
      kpi_dashboard/demo/demo_dashboard.xml
  2. 5
      kpi_dashboard/models/kpi_dashboard.py
  3. 2
      kpi_dashboard/models/kpi_kpi.py
  4. 30
      kpi_dashboard/static/src/js/dashboard_controller.js
  5. 11
      kpi_dashboard/static/src/js/dashboard_renderer.js
  6. 4
      kpi_dashboard/views/kpi_dashboard.xml

15
kpi_dashboard/demo/demo_dashboard.xml

@ -165,21 +165,24 @@ result = {"value": self.env.context.get('counter', 990)}
<record id="dashboard_widget_add_counter" model="kpi.dashboard.item">
<field name="name">+1 to Counter</field>
<field name="dashboard_id" ref="demo_dashboard"/>
<field name="column">4</field>
<field name="column">3</field>
<field name="row">10</field>
<field name="size_y">3</field>
<field name="size_y">1</field>
<field name="size_x">2</field>
<field name="color">#B41F1F</field>
<field name="font_color">#EEBF77</field>
<field name="modify_context" eval="True"/>
<field name="modify_context_expression">{'counter': (context.counter or 990) + 1}</field>
<field name="modify_color" eval="True"/>
<field name="modify_color_expression">check_if(((context.counter or 990) + 1) % 2, '#ff0000', '#00ff00')</field>
</record>
<record id="dashboard_widget_counter" model="kpi.dashboard.item">
<field name="name">Counter</field>
<field name="dashboard_id" ref="demo_dashboard"/>
<field name="kpi_id" ref="widget_counter"/>
<field name="column">2</field>
<field name="row">10</field>
<field name="column">3</field>
<field name="row">11</field>
<field name="size_y">3</field>
<field name="color">#4B0082</field>
<field name="font_color">#ffffff</field>
@ -189,8 +192,8 @@ result = {"value": self.env.context.get('counter', 990)}
<field name="name">Integer</field>
<field name="dashboard_id" ref="demo_dashboard"/>
<field name="kpi_id" ref="widget_integer"/>
<field name="column">3</field>
<field name="row">10</field>
<field name="column">4</field>
<field name="row">11</field>
<field name="size_y">3</field>
<field name="color">#ffffff</field>
<field name="font_color">#4B0082</field>

5
kpi_dashboard/models/kpi_dashboard.py

@ -119,6 +119,8 @@ class KpiDashboardItem(models.Model):
font_color = fields.Char()
modify_context = fields.Boolean()
modify_context_expression = fields.Char()
modify_color = fields.Boolean()
modify_color_expression = fields.Char()
@api.depends('row', 'size_y')
def _compute_end_row(self):
@ -176,9 +178,12 @@ class KpiDashboardItem(models.Model):
"color": self.color,
"font_color": self.font_color or "000000",
"modify_context": self.modify_context,
"modify_color": self.modify_color,
}
if self.modify_context:
vals['modify_context_expression'] = self.modify_context_expression
if self.modify_color:
vals['modify_color_expression'] = self.modify_color_expression
if self.kpi_id:
vals.update(
{

2
kpi_dashboard/models/kpi_kpi.py

@ -10,6 +10,7 @@ from odoo.tools.float_utils import float_compare
import re
import json
import datetime
from dateutil import relativedelta
class KpiKpi(models.Model):
@ -137,6 +138,7 @@ class KpiKpi(models.Model):
"model": self.browse(),
"datetime": datetime,
"float_compare": float_compare,
"relativedelta": relativedelta.relativedelta,
}
def _forbidden_code(self):

30
kpi_dashboard/static/src/js/dashboard_controller.js

@ -11,11 +11,14 @@ odoo.define('kpi_dashboard.DashboardController', function (require) {
init: function () {
this._super.apply(this, arguments);
this.dashboard_context = {};
this.dashboard_color_data = []
},
custom_events: _.extend({}, BasicController.prototype.custom_events, {
addDashboard: '_addDashboard',
refresh_on_fly: '_refreshOnFly',
modify_context: '_modifyContext',
add_modify_color: '_addModifyColor',
refresh_colors: '_refreshColors',
}),
_refreshOnFly: function (event) {
var self = this;
@ -112,8 +115,35 @@ odoo.define('kpi_dashboard.DashboardController', function (require) {
)}),
);
this._refreshOnFly(event);
this._refreshColors();
},
_addModifyColor: function (event) {
this.dashboard_color_data.push([
event.data.element_id,
event.data.expression,
]);
},
_refreshColors: function () {
var self = this;
var ctx = this._getContext();
_.each(this.dashboard_color_data, function (data) {
var color = py.eval(data[1], {
context: _.extend(ctx, {
__getattr__: function() {return false},
}),
check_if: function(args) {
if (args[0].toJSON()) {
return args[1];
}
return args[2];
}
});
var $element = self.renderer.$el.find('#' + data[0]);
$element.css('background-color', color);
});
},
});
return DashboardController;

11
kpi_dashboard/static/src/js/dashboard_renderer.js

@ -36,7 +36,14 @@ odoo.define('kpi_dashboard.DashboardRenderer', function (require) {
'kpi_dashboard.kpi', {widget: kpi}));
element.css('background-color', kpi.color);
element.css('color', kpi.font_color);
element.attr('id', _.uniqueId('kpi_'));
self.$grid.append(element);
if (kpi.modify_color) {
self.trigger_up("add_modify_color", {
element_id: element.attr("id"),
expression: kpi.modify_color_expression,
})
}
if (kpi.modify_context) {
element.on("click", self._onClickModifyContext.bind(
self, kpi.modify_context_expression));
@ -71,6 +78,10 @@ odoo.define('kpi_dashboard.DashboardRenderer', function (require) {
self.trigger_up('refresh_on_fly');
}, this.state.specialData.compute_on_fly_refresh *1000);
};
this.trigger_up('refresh_colors');
this.trigger_up('refresh_on_fly');
// We need to refreshs data in order compute with the current
// context
return $.when();
},
on_detach_callback: function () {

4
kpi_dashboard/views/kpi_dashboard.xml

@ -150,6 +150,10 @@
<field name="modify_context_expression"
attrs="{'invisible': [('modify_context', '=', False)]}"
widget="ace" options="{'mode': 'python'}"/>
<field name="modify_color"/>
<field name="modify_color_expression"
attrs="{'invisible': [('modify_color', '=', False)]}"
widget="ace" options="{'mode': 'python'}"/>
</group>
</sheet>
<footer>

Loading…
Cancel
Save