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.

101 lines
3.1 KiB

  1. /* Copyright 2019 Onestein
  2. * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
  3. odoo.define('web_tree_duplicate', function (require) {
  4. "use strict";
  5. var core = require('web.core');
  6. var _t = core._t;
  7. var ListController = require('web.ListController');
  8. var ListView = require('web.ListView');
  9. var search_inputs = require('web.search_inputs');
  10. ListView.include({
  11. /**
  12. * @override
  13. */
  14. init: function () {
  15. this._super.apply(this, arguments);
  16. var sidebarDuplicate = false;
  17. if ('duplicate' in this.arch.attrs) {
  18. sidebarDuplicate = _.str.toBool(this.arch.attrs.duplicate);
  19. }
  20. this.controllerParams.sidebarDuplicate = sidebarDuplicate;
  21. },
  22. });
  23. ListController.include({
  24. /**
  25. * @override
  26. */
  27. init: function (parent, model, renderer, params) {
  28. this._super.apply(this, arguments);
  29. this.sidebarDuplicate = params.sidebarDuplicate;
  30. },
  31. /**
  32. * Add the Duplicate button to the sidebar.
  33. *
  34. * @override
  35. */
  36. renderSidebar: function () {
  37. var res = this._super.apply(this, arguments);
  38. if (this.hasSidebar && this.sidebarDuplicate) {
  39. this.sidebar._addItems('other', [{
  40. label: _t('Duplicate'),
  41. callback: this._onDuplicateSelectedRecords.bind(this),
  42. }]);
  43. }
  44. return res;
  45. },
  46. /**
  47. * This function is triggered when the Duplicate button is clicked.
  48. *
  49. * @private
  50. */
  51. _onDuplicateSelectedRecords: function () {
  52. this._duplicateRecords(this.selectedRecords);
  53. },
  54. /**
  55. * Duplicate records.
  56. *
  57. * @param {Array} ids Ids of records to duplicate
  58. * @private
  59. * @returns {jQuery.Deferred}
  60. */
  61. _duplicateRecords: function (ids) {
  62. var self = this;
  63. var done = [];
  64. _.each(ids, function (id) {
  65. done.push(self.model.duplicateRecord(id));
  66. });
  67. return $.when.apply($, done).done(function () {
  68. var dataPoints = arguments;
  69. var ids = _.map(dataPoints, function (dataPoint) {
  70. return self.model.localData[dataPoint].res_id;
  71. });
  72. var filter = {
  73. attrs: {
  74. domain: JSON.stringify([['id', 'in', ids]]),
  75. string: _t('Duplicated Records')
  76. }
  77. }
  78. var filterWidget = new search_inputs.Filter(filter);
  79. var filterGroup = new search_inputs.FilterGroup(
  80. [filterWidget],
  81. self.searchView,
  82. self.searchView.intervalMapping,
  83. self.searchView.periodMapping
  84. );
  85. var facet = filterGroup.make_facet([filterGroup.make_value(filter)]);
  86. self.searchView.query.add([facet]);
  87. });
  88. },
  89. });
  90. });