From 5b3cb812be853c5b116a7bbf945f1c878e6b6fe0 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Wed, 21 Mar 2018 12:43:17 +0100 Subject: [PATCH] [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). --- .../js/chrome/action_manager_act_window.js | 17 ++++++++ .../web/static/src/js/chrome/search_view.js | 40 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/addons/web/static/src/js/chrome/action_manager_act_window.js b/addons/web/static/src/js/chrome/action_manager_act_window.js index be6ac5e2230..f505cccadfe 100644 --- a/addons/web/static/src/js/chrome/action_manager_act_window.js +++ b/addons/web/static/src/js/chrome/action_manager_act_window.js @@ -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. diff --git a/addons/web/static/src/js/chrome/search_view.js b/addons/web/static/src/js/chrome/search_view.js index d1b732477e6..302336deeb2 100644 --- a/addons/web/static/src/js/chrome/search_view.js +++ b/addons/web/static/src/js/chrome/search_view.js @@ -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 //--------------------------------------------------------------------------