[FIX] web: always use filter_domain for search panel counters

The counters for categories were not updated when filter values were
selected. This commit fixes that situation.
This might impact the global performances of the search panel but
there is still room for improvement. Actually, it could be
possible to reload categories and filters less often by carefully
track the internal changes and the categories/filters attributes
(enable counters, expand,...).

closes odoo/odoo#49307

Related: odoo/enterprise#9795
Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
This commit is contained in:
Mathieu Duckerts-Antoine
2020-06-12 13:34:04 +00:00
committed by Julien Mougenot
parent 8a2a25e7fc
commit 7002bdb239
4 changed files with 75 additions and 6 deletions
+5 -1
View File
@@ -456,6 +456,7 @@ class Base(models.AbstractModel):
:param expand: whether to return the full range of field values in comodel_domain
or only the field image values (possibly filtered and/or completed
with parents if hierarchize is set). Default is False.
:param filter_domain: domain generated by filters. Default is [].
:param hierarchize: determines if the categories must be displayed hierarchically
(if possible). If set to true and _parent_name is set on the
comodel field, the information necessary for the hierarchization will
@@ -479,7 +480,10 @@ class Base(models.AbstractModel):
'supported_types': supported_types, 'field_type': field.type}))
model_domain = kwargs.get('search_domain', [])
extra_domain = kwargs.get('category_domain', [])
extra_domain = AND([
kwargs.get('category_domain', []),
kwargs.get('filter_domain', []),
])
if field.type == 'selection':
return {
@@ -406,6 +406,7 @@ const SearchPanel = Widget.extend({
category_domain: this._getCategoryDomain(category.id),
enable_counters: enableCounters,
expand,
filter_domain: this._getFilterDomain(),
hierarchize,
limit,
search_domain: this.searchDomain,
@@ -643,10 +644,9 @@ const SearchPanel = Widget.extend({
* the searchPanel and the reloading of the data.
*
* @private
* @param {Object} [reloadOptions={}]
*/
_notifyDomainUpdated(reloadOptions = {}) {
this.reloadOptions = reloadOptions;
_notifyDomainUpdated() {
this.reloadOptions = { shouldFetchCategories: true };
this.trigger_up('search_panel_domain_updated', {
domain: this.getDomain(),
});
@@ -744,7 +744,7 @@ const SearchPanel = Widget.extend({
if (hasChanged) {
const storageKey = this._getLocalStorageKey(category);
this.call('local_storage', 'setItem', storageKey, valueId);
this._notifyDomainUpdated({ shouldFetchCategories: true });
this._notifyDomainUpdated();
} else {
this._render();
}
@@ -744,6 +744,7 @@ var MockServer = Class.extend({
* @param {Array[]} [kwargs.comodel_domain] domain of field values (if relational)
* (this parameter is used in _search_panel_range)
* @param {boolean} [kwargs.enable_counters] whether to count records by value
* @param {Array[]} [kwargs.filter_domain] domain generated by filters
* @param {integer} [kwargs.limit] maximal number of values to fetch
* @param {Array[]} [kwargs.search_domain] base domain of search (this parameter
* is used in _search_panel_range)
@@ -757,7 +758,10 @@ var MockServer = Class.extend({
}
const modelDomain = kwargs.search_domain || [];
const extraDomain = kwargs.category_domain || [];
const extraDomain = Domain.prototype.normalizeArray([
...(kwargs.category_domain || []),
...(kwargs.filter_domain || []),
]);
if (field.type === 'selection') {
const newKwargs = Object.assign({}, kwargs, {
@@ -2619,6 +2619,67 @@ QUnit.module('Views', {
form.destroy();
});
QUnit.test("Reload categories with counters when filter values are selected", async function (assert) {
assert.expect(10);
const kanban = await createView({
View: KanbanView,
model: 'partner',
data: this.data,
mockRPC: function (route, args) {
assert.step(args.method || route);
return this._super.apply(this, arguments);
},
services: this.services,
arch: `
<kanban>
<templates>
<t t-name="kanban-box">
<div>
<field name="foo"/>
</div>
</t>
</templates>
</kanban>`,
archs: {
'partner,false,search': `
<search>
<searchpanel>
<field name="category_id" enable_counters="1"/>
<field name="state" select="multi" enable_counters="1"/>
</searchpanel>
</search>`,
},
});
assert.verifySteps([
'search_panel_select_range',
"search_panel_select_multi_range",
'/web/dataset/search_read'
]);
assert.deepEqual(getCounters(kanban), [
1, 3, // category counts (in order)
1, 1, 2 // filter counts
]);
await testUtils.dom.click(kanban.el.querySelector('.o_search_panel_filter_value input'));
assert.deepEqual(getCounters(kanban), [
1, // category counts (for silver: 0 is not displayed)
1, 1, 2 // filter counts
]);
assert.verifySteps([
'search_panel_select_range',
'/web/dataset/search_read',
"search_panel_select_multi_range"
]);
kanban.destroy();
});
QUnit.test("many2one: select one, expand, hierarchize, counters", async function (assert) {
assert.expect(5);