Enric Tobella
4 years ago
6 changed files with 97 additions and 60 deletions
-
2kpi_dashboard/__manifest__.py
-
3kpi_dashboard/models/kpi_kpi.py
-
14kpi_dashboard/static/src/js/widget/counter_widget.js
-
71kpi_dashboard/static/src/js/widget/integer_widget.js
-
63kpi_dashboard/static/src/js/widget/number_widget.js
-
2kpi_dashboard/templates/assets.xml
@ -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; |
|||
}); |
@ -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; |
|||
}); |
@ -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) { |
|||
return field_utils.format.float(num, false, { |
|||
digits: [3, 1]}); |
|||
} |
|||
if (Math.abs(num) < 10) { |
|||
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); |
|||
} |
|||
} |
|||
return this._super.apply(this, arguments) |
|||
}, |
|||
}); |
|||
|
|||
registry.add('number', NumberWidget); |
|||
return NumberWidget; |
|||
}); |
Write
Preview
Loading…
Cancel
Save
Reference in new issue