Browse Source

[IMP] kpi_dashboard: set new widgets

12.0
Enric Tobella 4 years ago
parent
commit
f82821f150
No known key found for this signature in database GPG Key ID: D76663C0B4023597
  1. 2
      kpi_dashboard/__manifest__.py
  2. 3
      kpi_dashboard/models/kpi_kpi.py
  3. 14
      kpi_dashboard/static/src/js/widget/counter_widget.js
  4. 71
      kpi_dashboard/static/src/js/widget/integer_widget.js
  5. 65
      kpi_dashboard/static/src/js/widget/number_widget.js
  6. 2
      kpi_dashboard/templates/assets.xml

2
kpi_dashboard/__manifest__.py

@ -15,8 +15,8 @@
"wizards/kpi_dashboard_menu.xml",
"security/security.xml",
"security/ir.model.access.csv",
"templates/assets.xml",
"views/kpi_menu.xml",
"views/webclient_templates.xml",
"views/kpi_kpi.xml",
"views/kpi_dashboard.xml",
],

3
kpi_dashboard/models/kpi_kpi.py

@ -29,7 +29,8 @@ class KpiKpi(models.Model):
args = fields.Char()
kwargs = fields.Char()
widget = fields.Selection(
[("number", "Number"), ("meter", "Meter"), ("graph", "Graph")],
[('integer', 'Integer'), ("number", "Number"), ("meter", "Meter"),
('counter', 'Counter'), ("graph", "Graph")],
required=True,
default="number",
)

14
kpi_dashboard/static/src/js/widget/counter_widget.js

@ -0,0 +1,14 @@
odoo.define('kpi_dashboard.CounterWidget', function (require) {
"use strict";
var IntegerWidget = require('kpi_dashboard.IntegerWidget');
var registry = require('kpi_dashboard.widget_registry');
var field_utils = require('web.field_utils');
var CounterWidget = IntegerWidget.extend({
shortList: [],
});
registry.add('counter', CounterWidget);
return CounterWidget;
});

71
kpi_dashboard/static/src/js/widget/integer_widget.js

@ -0,0 +1,71 @@
odoo.define('kpi_dashboard.IntegerWidget', function (require) {
"use strict";
var AbstractWidget = require('kpi_dashboard.AbstractWidget');
var registry = require('kpi_dashboard.widget_registry');
var field_utils = require('web.field_utils');
var IntegerWidget = AbstractWidget.extend({
template: 'kpi_dashboard.number',
digits: [3, 0],
shortList: [
[1000000000000, 'T', [3, 1]],
[1000000000, 'G', [3, 1]],
[1000000, 'M', [3, 1]],
[1000, 'K', [3, 1]]
],
shortNumber: function (num) {
var suffix = '';
var shortened = false;
var digits = this.digits;
_.each(this.shortList, function (shortItem) {
if (!shortened && Math.abs(num) >= shortItem[0]) {
shortened = true;
suffix = shortItem[1];
num = num / shortItem[0];
digits = shortItem[2];
}
});
return field_utils.format.float(num, false, {
digits: digits}) + suffix;
},
fillWidget: function (values) {
var widget = this.$el;
var value = values.value.value;
if (value === undefined) {
value = 0;
}
var item = widget.find('[data-bind="value"]');
if (item) {
item.text(this.shortNumber(value));
}
var previous = values.value.previous;
var $change_rate = widget.find('.change-rate');
if (previous === undefined) {
$change_rate.toggleClass('active', false);
} else {
var difference = 0;
if (previous !== 0) {
difference = field_utils.format.integer(
(100 * value / previous) - 100) + '%';
}
$change_rate.toggleClass('active', true);
var $difference = widget.find('[data-bind="difference"]');
$difference.text(difference);
var $arrow = widget.find('[data-bind="arrow"]');
if (value < previous) {
$arrow.toggleClass('fa-arrow-up', false);
$arrow.toggleClass('fa-arrow-down', true);
} else {
$arrow.toggleClass('fa-arrow-up', true);
$arrow.toggleClass('fa-arrow-down', false);
}
}
},
});
registry.add('integer', IntegerWidget);
return IntegerWidget;
});

65
kpi_dashboard/static/src/js/widget/number_widget.js

@ -1,72 +1,21 @@
odoo.define('kpi_dashboard.NumberWidget', function (require) {
"use strict";
var AbstractWidget = require('kpi_dashboard.AbstractWidget');
var IntegerWidget = require('kpi_dashboard.IntegerWidget');
var registry = require('kpi_dashboard.widget_registry');
var field_utils = require('web.field_utils');
var NumberWidget = AbstractWidget.extend({
template: 'kpi_dashboard.number',
var NumberWidget = IntegerWidget.extend({
digits: [3, 1],
shortNumber: function (num) {
if (Math.abs(num) >= 1000000000000) {
return field_utils.format.integer(num / 1000000000000, false, {
digits: [3, 1]}) + 'T';
}
if (Math.abs(num) >= 1000000000) {
return field_utils.format.integer(num / 1000000000, false, {
digits: [3,1]}) + 'G';
}
if (Math.abs(num) >= 1000000) {
return field_utils.format.integer(num / 1000000, false, {
digits: [3, 1]}) + 'M';
}
if (Math.abs(num) >= 1000) {
return field_utils.format.float(num / 1000, false, {
digits: [3, 1]}) + 'K';
}
if (Math.abs(num) >= 10) {
if (Math.abs(num) < 10) {
return field_utils.format.float(num, false, {
digits: [3, 1]});
}
return field_utils.format.float(num, false, {
digits: [3, 2]});
},
fillWidget: function (values) {
var widget = this.$el;
var value = values.value.value;
if (value === undefined) {
value = 0;
}
var item = widget.find('[data-bind="value"]');
if (item) {
item.text(this.shortNumber(value));
}
var previous = values.value.previous;
var $change_rate = widget.find('.change-rate');
if (previous === undefined) {
$change_rate.toggleClass('active', false);
} else {
var difference = 0;
if (previous !== 0) {
difference = field_utils.format.integer(
(100 * value / previous) - 100) + '%';
}
$change_rate.toggleClass('active', true);
var $difference = widget.find('[data-bind="difference"]');
$difference.text(difference);
var $arrow = widget.find('[data-bind="arrow"]');
if (value < previous) {
$arrow.toggleClass('fa-arrow-up', false);
$arrow.toggleClass('fa-arrow-down', true);
} else {
$arrow.toggleClass('fa-arrow-up', true);
$arrow.toggleClass('fa-arrow-down', false);
}
digits: [3, 2]});
}
return this._super.apply(this, arguments)
},
});
registry.add('number', NumberWidget);
return NumberWidget;
});

2
kpi_dashboard/views/webclient_templates.xml → kpi_dashboard/templates/assets.xml

@ -14,7 +14,9 @@
<script type="text/javascript" src="/kpi_dashboard/static/src/js/dashboard_controller.js"/>
<script type="text/javascript" src="/kpi_dashboard/static/src/js/dashboard_view.js"/>
<script type="text/javascript" src="/kpi_dashboard/static/src/js/widget/integer_widget.js"/>
<script type="text/javascript" src="/kpi_dashboard/static/src/js/widget/number_widget.js"/>
<script type="text/javascript" src="/kpi_dashboard/static/src/js/widget/counter_widget.js"/>
<script type="text/javascript" src="/kpi_dashboard/static/src/js/widget/meter_widget.js"/>
<script type="text/javascript" src="/kpi_dashboard/static/src/js/widget/graph_widget.js"/>
<script type="text/javascript" src="/kpi_dashboard/static/src/js/widget/text_widget.js"/>
Loading…
Cancel
Save