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.

110 lines
4.5 KiB

  1. odoo.define('kpi_dashboard.DashboardRenderer', function (require) {
  2. "use strict";
  3. var BasicRenderer = require('web.BasicRenderer');
  4. var core = require('web.core');
  5. var registry = require('kpi_dashboard.widget_registry');
  6. var BusService = require('bus.BusService');
  7. var qweb = core.qweb;
  8. var DashboardRenderer= BasicRenderer.extend({
  9. className: "o_dashboard_view",
  10. _getDashboardWidget: function (kpi) {
  11. var Widget = registry.getAny([
  12. kpi.widget, 'abstract',
  13. ]);
  14. var widget = new Widget(this, kpi);
  15. return widget;
  16. },
  17. _onClickModifyContext: function (modify_context_expression, event) {
  18. this.trigger_up('modify_context', {
  19. context: modify_context_expression,
  20. event: event,
  21. })
  22. },
  23. _renderView: function () {
  24. this.$el.html($(qweb.render('dashboard_kpi.dashboard')));
  25. this.$el.css(
  26. 'background-color', this.state.specialData.background_color);
  27. this.$el.find('.gridster')
  28. .css('width', this.state.specialData.width);
  29. this.$grid = this.$el.find('.gridster ul');
  30. var self = this;
  31. this.kpi_widget = {};
  32. _.each(this.state.specialData.item_ids, function (kpi) {
  33. var element = $(qweb.render(
  34. 'kpi_dashboard.kpi', {widget: kpi}));
  35. element.css('background-color', kpi.color);
  36. element.css('color', kpi.font_color);
  37. element.attr('id', _.uniqueId('kpi_'));
  38. self.$grid.append(element);
  39. if (kpi.modify_color) {
  40. self.trigger_up("add_modify_color", {
  41. element_id: element.attr("id"),
  42. expression: kpi.modify_color_expression,
  43. })
  44. }
  45. if (kpi.modify_context) {
  46. element.on("click", self._onClickModifyContext.bind(
  47. self, kpi.modify_context_expression));
  48. element.css('cursor', 'pointer');
  49. // We want to set it show as clickable
  50. }
  51. self.kpi_widget[kpi.id] = self._getDashboardWidget(kpi);
  52. self.kpi_widget[kpi.id].appendTo(element);
  53. });
  54. this.$grid.gridster({
  55. widget_margins: [
  56. this.state.specialData.margin_x,
  57. this.state.specialData.margin_y,
  58. ],
  59. widget_base_dimensions: [
  60. this.state.specialData.widget_dimension_x,
  61. this.state.specialData.widget_dimension_y,
  62. ],
  63. cols: this.state.specialData.max_cols,
  64. }).data('gridster').disable();
  65. this.channel = 'kpi_dashboard_' + this.state.res_id;
  66. this.call(
  67. 'bus_service', 'addChannel', this.channel);
  68. this.call('bus_service', 'startPolling');
  69. this.call(
  70. 'bus_service', 'onNotification',
  71. this, this._onNotification
  72. );
  73. if (this.state.specialData.compute_on_fly_refresh > 0) {
  74. // Setting the refresh interval
  75. this.on_fly_interval = setInterval(function () {
  76. self.trigger_up('refresh_on_fly');
  77. }, this.state.specialData.compute_on_fly_refresh *1000);
  78. };
  79. this.trigger_up('refresh_colors');
  80. this.trigger_up('refresh_on_fly');
  81. // We need to refreshs data in order compute with the current
  82. // context
  83. return $.when();
  84. },
  85. on_detach_callback: function () {
  86. // We want to clear the refresh interval once we exit the view
  87. if (this.on_fly_interval) {
  88. clearInterval(this.on_fly_interval)
  89. }
  90. this._super.apply(this, arguments);
  91. },
  92. _onNotification: function (notifications) {
  93. var self = this;
  94. _.each(notifications, function (notification) {
  95. var channel = notification[0];
  96. var message = notification[1];
  97. if (channel === self.channel && message) {
  98. var widget = self.kpi_widget[message.id];
  99. if (widget !== undefined) {
  100. widget._fillWidget(message);
  101. }
  102. }
  103. });
  104. },
  105. });
  106. return DashboardRenderer;
  107. });