OCA reporting engine fork for dev and update.
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.

91 lines
3.8 KiB

  1. odoo.define('kpi_dashboard.AbstractWidget', function (require) {
  2. "use strict";
  3. var Widget = require('web.Widget');
  4. var field_utils = require('web.field_utils');
  5. var time = require('web.time');
  6. var ajax = require('web.ajax');
  7. var registry = require('kpi_dashboard.widget_registry');
  8. var AbstractWidget = Widget.extend({
  9. template: 'kpi_dashboard.base_widget', // Template used by the widget
  10. cssLibs: [], // Specific css of the widget
  11. jsLibs: [], // Specific Javascript libraries of the widget
  12. events: {
  13. 'click .o_kpi_dashboard_toggle_button': '_onClickToggleButton',
  14. 'click .direct_action': '_onClickDirectAction',
  15. },
  16. init: function (parent, kpi_values) {
  17. this._super(parent);
  18. this.col = kpi_values.col;
  19. this.row = kpi_values.row;
  20. this.sizex = kpi_values.sizex;
  21. this.sizey = kpi_values.sizey;
  22. this.color = kpi_values.color;
  23. this.values = kpi_values;
  24. this.margin_x = parent.state.specialData.margin_x;
  25. this.margin_y = parent.state.specialData.margin_y;
  26. this.widget_dimension_x = parent.state.specialData.widget_dimension_x;
  27. this.widget_dimension_y = parent.state.specialData.widget_dimension_y;
  28. this.prefix = kpi_values.prefix;
  29. this.suffix = kpi_values.suffix;
  30. this.actions = kpi_values.actions;
  31. this.widget_size_x = this.widget_dimension_x * this.sizex +
  32. (this.sizex - 1) * this.margin_x;
  33. this.widget_size_y = this.widget_dimension_y * this.sizey +
  34. (this.sizey - 1) * this.margin_y;
  35. },
  36. willStart: function () {
  37. // We need to load the libraries before the start
  38. return $.when(ajax.loadLibs(this), this._super.apply(this, arguments));
  39. },
  40. start: function () {
  41. var self = this;
  42. return this._super.apply(this, arguments).then(function () {
  43. self._fillWidget(self.values);
  44. });
  45. },
  46. _onClickToggleButton: function (event) {
  47. event.preventDefault();
  48. this.$el.toggleClass('o_dropdown_open');
  49. },
  50. _fillWidget: function (values) {
  51. // This function fills the widget values
  52. if (this.$el === undefined)
  53. return;
  54. this.fillWidget(values);
  55. var item = this.$el.find('[data-bind="value_last_update_display"]');
  56. if (item && values.value_last_update !== undefined) {
  57. var value = field_utils.parse.datetime(values.value_last_update);
  58. item.text(value.clone().add(
  59. this.getSession().getTZOffset(value), 'minutes').format(
  60. time.getLangDatetimeFormat()
  61. ));
  62. }
  63. var $manage = this.$el.find('.o_kpi_dashboard_manage');
  64. if ($manage && this.showManagePanel(values))
  65. $manage.toggleClass('hidden', false);
  66. },
  67. showManagePanel: function (values) {
  68. // Hook for extensions
  69. return (values.actions !== undefined);
  70. },
  71. fillWidget: function (values) {
  72. // Specific function that will be changed by specific widget
  73. var value = values.value;
  74. var self = this;
  75. _.each(value, function (val, key) {
  76. var item = self.$el.find('[data-bind=' + key + ']')
  77. if (item)
  78. item.text(val);
  79. })
  80. },
  81. _onClickDirectAction: function(event) {
  82. event.preventDefault();
  83. var $data = $(event.currentTarget).closest('a');
  84. return this.do_action($($data).data('id'));
  85. }
  86. });
  87. registry.add('abstract', AbstractWidget);
  88. return AbstractWidget;
  89. });