[FIX] web_editor: reduce M2M widget rpc

Every options UI is updated when one is updated.
The Many2manyUserValueWidget is built in a way that every UI update will
trigger 2 rpc to get the records to display to the user, even if it is
not the option containing the widget that has been updated.

These rpc are now cached at the widget level.

This commit also fixes a bad computation of the Many2manyUserValueWidget
search domain.

task-2629245

X-original-commit: 847aecb4f9f4647e55e454d8c6560aa267f17c64
Part-of: odoo/odoo#77873
This commit is contained in:
Younn Olivier
2021-10-05 15:41:27 +00:00
parent 67ee284918
commit 8563d989f3
@@ -1918,7 +1918,13 @@ const ListUserValueWidget = UserValueWidget.extend({
if (this.createWidget) {
const selectedIds = currentValues.map(({ id }) => id)
.filter(id => typeof id === 'number');
this.createWidget.options.domain = [...this.createWidget.options.domain, ['id', 'not in', selectedIds]];
const selectedIdsDomain = ['id', 'not in', selectedIds];
const selectedIdsDomainIndex = this.createWidget.options.domain.findIndex(domain => domain[0] === 'id' && domain[1] === 'not in');
if (selectedIdsDomainIndex > -1) {
this.createWidget.options.domain[selectedIdsDomainIndex] = selectedIdsDomain;
} else {
this.createWidget.options.domain = [...this.createWidget.options.domain, selectedIdsDomain];
}
this.createWidget.setValue('');
this.createWidget.inputEl.value = '';
$(this.createWidget.inputEl).trigger('input');
@@ -2384,6 +2390,7 @@ const Many2oneUserValueWidget = SelectUserValueWidget.extend({
init(parent, title, options, $target) {
this.afterSearch = [];
this.displayNameCache = {};
this._rpcCache = {};
const {dataAttributes} = options;
Object.assign(options, {
limit: '5',
@@ -2502,6 +2509,18 @@ const Many2oneUserValueWidget = SelectUserValueWidget.extend({
// Private
//--------------------------------------------------------------------------
/**
* Caches the rpc.
*
* @override
*/
async _rpc() {
const cacheId = JSON.stringify(...arguments);
if (!this._rpcCache[cacheId]) {
this._rpcCache[cacheId] = this._super(...arguments);
}
return this._rpcCache[cacheId];
},
/**
* Searches the database for corresponding records and updates the dropdown
*