[FIX] web: manage correctly deletion of facets

This commit fixes many synchronization problems between search view bar
and the filters and groupby menus.
This commit is contained in:
Mathieu Duckerts-Antoine
2018-07-06 13:28:17 +02:00
committed by Adrien Dieudonne
parent 4094f75527
commit 94c027efdd
3 changed files with 89 additions and 42 deletions
@@ -126,7 +126,7 @@ var GroupByMenu = DropdownMenu.extend({
itemId: groupbyName,
description: field.string,
fieldName: fieldName,
groupId: this.groupId,
groupId: _.uniqueId('__group__'),
isActive: true,
};
var eventData = _.clone(groupby);
@@ -153,11 +153,6 @@ var GroupByMenu = DropdownMenu.extend({
item.options = this.intervalOptions;
item.defaultOptionId = item.defaultOptionId || this.defaultOptionId;
}
// the idea is that we have only one group in the groupby menu
// like in the search view.
if (!this.groupId) {
this.groupId = item.groupId;
}
// super has to be called here because we need to add options to groupby
// before to call it since some keys in a groupby are initialized using
// the keys 'options' and 'defaultOptionId'
@@ -578,32 +578,35 @@ var SearchView = Widget.extend({
this.query.each(function (facet) {
var values = facet.get('values');
if (facet.attributes.cat === "groupByCategory") {
selectedGroupIds.groupByCategory = selectedGroupIds.groupByCategory.concat(
_.compact(
_.uniq(
_.map(facet.attributes.values, function (value) {
var groupby = value.value;
var groupbyDescription = _.findWhere(self.groupbysMapping, {groupby: groupby});
if (groupbyDescription) {
return groupbyDescription.groupId;
}
})
)
selectedGroupIds.groupByCategory = _.uniq(
values.reduce(
function (acc, value) {
var groupby = value.value;
var description = _.findWhere(self.groupbysMapping, {groupby: groupby});
if (description) {
acc.push(description.groupId);
}
return acc;
},
[]
)
);
}
if (facet.attributes.cat === "filterCategory") {
selectedGroupIds.filterCategory = selectedGroupIds.filterCategory.concat(
_.uniq(
_.compact(
_.map(facet.attributes.values, function (value) {
values.reduce(
function (acc, value) {
var filter = value.value;
var filterDescription = _.findWhere(self.filtersMapping, {filter: filter});
if (filterDescription) {
return filterDescription.groupId;
var description = _.findWhere(self.filtersMapping, {filter: filter});
if (description) {
acc.push(description.groupId);
}
})
return acc;
},
[]
)
)
);
@@ -869,39 +872,45 @@ var SearchView = Widget.extend({
return fv;
},
/**
* @private
* This function ask the groupby menu to deactive all groupbys if no
* groupby is used.
*
* @param {string[]]} selectedGroupIds
*/
_unsetUnusedGroupbys: function (selectedGroupIds) {
var groupIds = this.selectedGroupIds.groupByCategory.reduce(
function (acc, id) {
if (!_.contains(selectedGroupIds, id)) {
acc.push(id);
var groupIds = this.groupsMapping.reduce(
function (acc, triple) {
if (triple.category === 'Group By') {
acc.push(triple.groupId);
}
return acc;
},
[]
);
this.selectedGroupIds.groupByCategory = selectedGroupIds;
this.groupby_menu.unsetGroups(groupIds);
if (selectedGroupIds.length === 0) {
this.groupby_menu.unsetGroups(groupIds);
}
},
/**
* @private
* @param {string[]]} selectedGroupIds
*/
_unsetUnusedFilters: function (selectedGroupIds) {
var groupIds = this.selectedGroupIds.filterCategory.reduce(
function (acc, id) {
if (!_.contains(selectedGroupIds, id)) {
acc.push(id);
}
return acc;
},
[]
);
this.selectedGroupIds.filterCategory = selectedGroupIds;
this.filters_menu.unsetGroups(groupIds);
},
_unsetUnusedFilters: function (selectedGroupIds) {
var groupIds = this.groupsMapping.reduce(
function (acc, triple) {
if (triple.category === 'Filters') {
if (!_.contains(selectedGroupIds, triple.groupId)) {
acc.push(triple.groupId);
}
}
return acc;
},
[]
);
this.selectedGroupIds.filterCategory = selectedGroupIds;
this.filters_menu.unsetGroups(groupIds);
},
//--------------------------------------------------------------------------
// Handlers
@@ -76,6 +76,13 @@ QUnit.module('Search View', {
type: 'ir.actions.act_window',
views: [[2, 'list']],
search_view_id: [5, 'search'],
}, {
id: 7,
name: 'Partners Action 7',
res_model: 'partner',
type: 'ir.actions.act_window',
views: [[2, 'list']],
search_view_id: [6, 'search'],
},
];
@@ -135,6 +142,11 @@ QUnit.module('Search View', {
'<separator/>' +
'<filter string="11" name="coolName11" domain="[]"/>' +
'</search>',
'partner,6,search': '<search>'+
'<filter string="Date" name="coolName" context="{\'group_by\': \'date_field:day\'}"/>' +
'<separator/>' +
'<filter string="Bar" name="superName" context="{\'group_by\': \'bar\'}"/>' +
'</search>',
};
},
}, function () {
@@ -286,6 +298,37 @@ QUnit.module('Search View', {
assert.strictEqual($('.o_content tr.o_group_header').length, 4);
actionManager.destroy();
});
QUnit.test('a separator in groupbys does not cause problems', function (assert) {
assert.expect(6);
var actionManager = createActionManager({
actions: this.actions,
archs: this.archs,
data: this.data,
});
actionManager.doAction(7);
// open menu 'Group By'
$('span.fa-bars').click();
// open options menu
$('.o_group_by_menu .o_menu_item a:first').click();
// activate groupby with 'day' option
$('.o_group_by_menu .o_menu_item .o_item_option[data-option_id="day"]').click();
// activate the second groupby
$('.o_group_by_menu .o_menu_item > a').eq(1).click();
assert.strictEqual($('.o_group_by_menu .o_menu_item').length, 2);
assert.ok($('.o_group_by_menu .o_menu_item').hasClass('selected'));
// deactivate second groupby
$('.o_group_by_menu .o_menu_item > a').eq(1).click();
assert.ok($('.o_group_by_menu .o_menu_item').eq(0).hasClass('selected'));
assert.ok(!$('.o_group_by_menu .o_menu_item').eq(1).hasClass('selected'));
// remove facet
$('.o_facet_remove').click();
assert.ok(!$('.o_group_by_menu .o_menu_item').eq(0).hasClass('selected'));
assert.ok(!$('.o_group_by_menu .o_menu_item').eq(1).hasClass('selected'));
actionManager.destroy();
});
QUnit.module('Filters Menu');