[MIG] web_tree_duplicate to v16

This commit is contained in:
Holger Brunn
2023-07-05 11:55:32 +02:00
parent c0c2cc6e0d
commit b53521ca67
14 changed files with 154 additions and 194 deletions

View File

@@ -1,101 +0,0 @@
/* Copyright 2019 Onestein
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define('web_tree_duplicate', function (require) {
"use strict";
var core = require('web.core');
var _t = core._t;
var ListController = require('web.ListController');
var ListView = require('web.ListView');
var search_inputs = require('web.search_inputs');
ListView.include({
/**
* @override
*/
init: function () {
this._super.apply(this, arguments);
var sidebarDuplicate = false;
if ('duplicate' in this.arch.attrs) {
sidebarDuplicate = _.str.toBool(this.arch.attrs.duplicate);
}
this.controllerParams.sidebarDuplicate = sidebarDuplicate;
},
});
ListController.include({
/**
* @override
*/
init: function (parent, model, renderer, params) {
this._super.apply(this, arguments);
this.sidebarDuplicate = params.sidebarDuplicate;
},
/**
* Add the Duplicate button to the sidebar.
*
* @override
*/
renderSidebar: function () {
var res = this._super.apply(this, arguments);
if (this.hasSidebar && this.sidebarDuplicate) {
this.sidebar._addItems('other', [{
label: _t('Duplicate'),
callback: this._onDuplicateSelectedRecords.bind(this),
}]);
}
return res;
},
/**
* This function is triggered when the Duplicate button is clicked.
*
* @private
*/
_onDuplicateSelectedRecords: function () {
this._duplicateRecords(this.selectedRecords);
},
/**
* Duplicate records.
*
* @param {Array} ids Ids of records to duplicate
* @private
* @returns {jQuery.Deferred}
*/
_duplicateRecords: function (ids) {
var self = this;
var done = [];
_.each(ids, function (id) {
done.push(self.model.duplicateRecord(id));
});
return $.when.apply($, done).done(function () {
var dataPoints = arguments;
var ids = _.map(dataPoints, function (dataPoint) {
return self.model.localData[dataPoint].res_id;
});
var filter = {
attrs: {
domain: JSON.stringify([['id', 'in', ids]]),
string: _t('Duplicated Records')
}
}
var filterWidget = new search_inputs.Filter(filter);
var filterGroup = new search_inputs.FilterGroup(
[filterWidget],
self.searchView,
self.searchView.intervalMapping,
self.searchView.periodMapping
);
var facet = filterGroup.make_facet([filterGroup.make_value(filter)]);
self.searchView.query.add([facet]);
});
},
});
});

View File

@@ -0,0 +1,40 @@
/** @odoo-module **/
// (c) 2023 Hunki Enterprises BV (<https://hunki-enterprises.com>)
// License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
import {Domain} from "@web/core/domain";
import {ListController} from "@web/views/list/list_controller";
import {patch} from "@web/core/utils/patch";
patch(ListController.prototype, "add duplicate action", {
getActionMenuItems() {
const result = this._super();
if (
this.archInfo.activeActions.create &&
this.archInfo.activeActions.duplicate
) {
result.other.push({
key: "duplicate",
description: this.env._t("Duplicate"),
callback: () => this.duplicateRecords(),
});
}
return result;
},
async duplicateRecords() {
const ids = await Promise.all(
this.model.root.selection.map(async (record) => {
return await record.model.orm.call(record.resModel, "copy", [
record.resId,
]);
})
);
this.env.searchModel.createNewFilters([
{
description: this.env._t("Duplicated Records"),
domain: new Domain([["id", "in", ids]]).toString(),
type: "filter",
},
]);
},
});