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.

120 lines
4.2 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. },
  12. custom_events: _.extend({}, BasicController.prototype.custom_events, {
  13. addDashboard: '_addDashboard',
  14. refresh_on_fly: '_refreshOnFly',
  15. modify_context: '_modifyContext',
  16. }),
  17. _refreshOnFly: function (event) {
  18. var self = this;
  19. this._rpc({
  20. model: this.modelName,
  21. method: 'read_dashboard_on_fly',
  22. args: [[this.renderer.state.res_id]],
  23. context: this._getContext(),
  24. }).then(function (data) {
  25. _.each(data, function (item) {
  26. // We will follow the same logic used on Bus Notifications
  27. self.renderer._onNotification([[
  28. "kpi_dashboard_" + self.renderer.state.res_id,
  29. item
  30. ]])
  31. });
  32. });
  33. },
  34. renderPager: function ($node, options) {
  35. options = _.extend({}, options, {
  36. validate: this.canBeDiscarded.bind(this),
  37. });
  38. this._super($node, options);
  39. },
  40. _pushState: function (state) {
  41. state = state || {};
  42. var env = this.model.get(this.handle, {env: true});
  43. state.id = env.currentId;
  44. this._super(state);
  45. },
  46. _addDashboard: function () {
  47. var self = this;
  48. var action = self.initialState.specialData.action_id;
  49. var name = self.initialState.specialData.name;
  50. if (! action) {
  51. self.do_warn(_t("First you must create the Menu"));
  52. }
  53. return self._rpc({
  54. route: '/board/add_to_dashboard',
  55. params: {
  56. action_id: action,
  57. context_to_save: {'res_id': self.initialState.res_id},
  58. domain: [('id', '=', self.initialState.res_id)],
  59. view_mode: 'dashboard',
  60. name: name,
  61. },
  62. })
  63. .then(function (r) {
  64. if (r) {
  65. self.do_notify(
  66. _.str.sprintf(_t("'%s' added to dashboard"), name),
  67. _t('Please refresh your browser for the changes to take effect.')
  68. );
  69. } else {
  70. self.do_warn(_t("Could not add KPI dashboard to dashboard"));
  71. }
  72. });
  73. },
  74. _updateButtons: function () {
  75. // HOOK Function
  76. this.$buttons.on(
  77. 'click', '.o_dashboard_button_add',
  78. this._addDashboard.bind(this));
  79. },
  80. renderButtons: function ($node) {
  81. if (! $node) {
  82. return;
  83. }
  84. this.$buttons = $('<div/>');
  85. this.$buttons.append(qweb.render(
  86. "kpi_dashboard.buttons", {widget: this}));
  87. this._updateButtons();
  88. this.$buttons.appendTo($node);
  89. },
  90. _getContext: function () {
  91. return _.extend(
  92. {},
  93. this.model.get(this.handle, {raw: true}).getContext(),
  94. {bin_size: true},
  95. this.dashboard_context,
  96. )
  97. },
  98. _modifyContext: function (event) {
  99. var ctx = this._getContext();
  100. this.dashboard_context = _.extend(
  101. this.dashboard_context,
  102. py.eval(event.data.context, {context: _.extend(
  103. ctx,
  104. {__getattr__: function() {return false}}
  105. // We need to add this in order to allow to use undefined
  106. // context items
  107. )}),
  108. );
  109. this._refreshOnFly(event);
  110. }
  111. });
  112. return DashboardController;
  113. });