[IMP] web: SearchView: add/remove filter from outside

This rev. allows to add or remove filters to the SearchView from
the outside, and to make it display this new domain. This can be
used by views that trigger domain changes (e.g. this is the case
of the new DashboardView).
This commit is contained in:
Aaron Bohy
2018-04-03 15:19:42 +02:00
committed by Géry Debongnie
parent 3dc48c79c9
commit 5b3cb812be
2 changed files with 57 additions and 0 deletions
@@ -22,6 +22,7 @@ ActionManager.include({
env_updated: '_onEnvUpdated',
execute_action: '_onExecuteAction',
get_controller_context: '_onGetControllerContext',
update_filters: '_onUpdateFilters',
search: '_onSearch',
switch_view: '_onSwitchView',
}),
@@ -773,6 +774,22 @@ ActionManager.include({
var context = currentController && currentController.widget.getContext();
ev.data.callback(context || {});
},
/**
* Handles a request to add/remove search view filters.
*
* @param {OdooEvent} ev
* @param {string} ev.data.controllerID
* @param {Array[Object]} [ev.data.newFilters]
* @param {Array[Object]} [ev.data.filtersToRemove]
* @param {function} ev.data.callback called with the added filters as arg
*/
_onUpdateFilters: function (ev) {
var controller = this.controllers[ev.data.controllerID];
var action = this.actions[controller.actionID];
var data = ev.data;
var addedFilters = action.searchView.updateFilters(data.newFilters, data.filtersToRemove);
data.callback(addedFilters);
},
/**
* Called when there is a change in the search view, so the current action's
* environment needs to be updated with the new domain, context and groupby.
@@ -606,6 +606,46 @@ var SearchView = Widget.extend({
});
},
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* Updates the domain of the search view by adding and/or removing filters.
*
* @todo: the way it is done could be improved, but the actual state of the
* searchview doesn't allow to do much better.
* @param {Array[Object]} list of filters to add, described by objects with
* keys domain (the domain as an Array), and help (the text to display in
* the facet)
* @param {Array[Object]} list of filters to remove (previously added ones)
* @returns {Array[Object]} list of added filters (to pass as filtersToRemove
* for a further call to this function)
*/
updateFilters: function (newFilters, filtersToRemove) {
var self = this;
var addedFilters = _.map(newFilters, function (filter) {
filter = {
attrs: {domain: filter.domain, help: filter.help},
};
var filterWidget = new search_inputs.Filter(filter);
var filterGroup = new search_inputs.FilterGroup([filterWidget], self);
var facet = filterGroup.make_facet([filterGroup.make_value(filter)]);
self.query.add([facet], {silent: true});
return _.last(self.query.models);
})
_.each(filtersToRemove, function (filter) {
self.query.remove(filter, {silent: true});
});
this.query.trigger('reset');
return addedFilters
},
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------