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.

150 lines
5.3 KiB

  1. odoo.define('kpi_dashboard.DashboardController', function (require) {
  2. "use strict";
  3. var BasicController = require('web.BasicController');
  4. var core = require('web.core');
  5. var qweb = core.qweb;
  6. var _t = core._t;
  7. var DashboardController = BasicController.extend({
  8. init: function () {
  9. this._super.apply(this, arguments);
  10. this.dashboard_context = {};
  11. this.dashboard_color_data = []
  12. },
  13. custom_events: _.extend({}, BasicController.prototype.custom_events, {
  14. addDashboard: '_addDashboard',
  15. refresh_on_fly: '_refreshOnFly',
  16. modify_context: '_modifyContext',
  17. add_modify_color: '_addModifyColor',
  18. refresh_colors: '_refreshColors',
  19. }),
  20. _refreshOnFly: function (event) {
  21. var self = this;
  22. this._rpc({
  23. model: this.modelName,
  24. method: 'read_dashboard_on_fly',
  25. args: [[this.renderer.state.res_id]],
  26. context: this._getContext(),
  27. }).then(function (data) {
  28. _.each(data, function (item) {
  29. // We will follow the same logic used on Bus Notifications
  30. self.renderer._onNotification([[
  31. "kpi_dashboard_" + self.renderer.state.res_id,
  32. item
  33. ]])
  34. });
  35. });
  36. },
  37. renderPager: function ($node, options) {
  38. options = _.extend({}, options, {
  39. validate: this.canBeDiscarded.bind(this),
  40. });
  41. this._super($node, options);
  42. },
  43. _pushState: function (state) {
  44. state = state || {};
  45. var env = this.model.get(this.handle, {env: true});
  46. state.id = env.currentId;
  47. this._super(state);
  48. },
  49. _addDashboard: function () {
  50. var self = this;
  51. var action = self.initialState.specialData.action_id;
  52. var name = self.initialState.specialData.name;
  53. if (! action) {
  54. self.do_warn(_t("First you must create the Menu"));
  55. }
  56. return self._rpc({
  57. route: '/board/add_to_dashboard',
  58. params: {
  59. action_id: action,
  60. context_to_save: {'res_id': self.initialState.res_id},
  61. domain: [('id', '=', self.initialState.res_id)],
  62. view_mode: 'dashboard',
  63. name: name,
  64. },
  65. })
  66. .then(function (r) {
  67. if (r) {
  68. self.do_notify(
  69. _.str.sprintf(_t("'%s' added to dashboard"), name),
  70. _t('Please refresh your browser for the changes to take effect.')
  71. );
  72. } else {
  73. self.do_warn(_t("Could not add KPI dashboard to dashboard"));
  74. }
  75. });
  76. },
  77. _updateButtons: function () {
  78. // HOOK Function
  79. this.$buttons.on(
  80. 'click', '.o_dashboard_button_add',
  81. this._addDashboard.bind(this));
  82. },
  83. renderButtons: function ($node) {
  84. if (! $node) {
  85. return;
  86. }
  87. this.$buttons = $('<div/>');
  88. this.$buttons.append(qweb.render(
  89. "kpi_dashboard.buttons", {widget: this}));
  90. this._updateButtons();
  91. this.$buttons.appendTo($node);
  92. },
  93. _getContext: function () {
  94. return _.extend(
  95. {},
  96. this.model.get(this.handle, {raw: true}).getContext(),
  97. {bin_size: true},
  98. this.dashboard_context,
  99. )
  100. },
  101. _modifyContext: function (event) {
  102. var ctx = this._getContext();
  103. this.dashboard_context = _.extend(
  104. this.dashboard_context,
  105. py.eval(event.data.context, {context: _.extend(
  106. ctx,
  107. {__getattr__: function() {return false}}
  108. // We need to add this in order to allow to use undefined
  109. // context items
  110. )}),
  111. );
  112. this._refreshOnFly(event);
  113. this._refreshColors();
  114. },
  115. _addModifyColor: function (event) {
  116. this.dashboard_color_data.push([
  117. event.data.element_id,
  118. event.data.expression,
  119. ]);
  120. },
  121. _refreshColors: function () {
  122. var self = this;
  123. var ctx = this._getContext();
  124. _.each(this.dashboard_color_data, function (data) {
  125. var color = py.eval(data[1], {
  126. context: _.extend(ctx, {
  127. __getattr__: function() {return false},
  128. }),
  129. check_if: function(args) {
  130. if (args[0].toJSON()) {
  131. return args[1];
  132. }
  133. return args[2];
  134. }
  135. });
  136. var $element = self.renderer.$el.find('#' + data[0]);
  137. $element.css('background-color', color);
  138. });
  139. },
  140. });
  141. return DashboardController;
  142. });