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.

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