diff options
| author | Aaron Bohy <aab@odoo.com> | 2018-03-21 12:43:17 +0100 |
|---|---|---|
| committer | Géry Debongnie <ged@odoo.com> | 2018-04-03 15:19:42 +0200 |
| commit | 5b3cb812be853c5b116a7bbf945f1c878e6b6fe0 (patch) | |
| tree | d8993f21349db71feef12594f43ab04b09132d0e /addons/web/static/src/js/chrome | |
| parent | 3dc48c79c9570b8f6cb608f1fca3e641e321ca44 (diff) | |
[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).
Diffstat (limited to 'addons/web/static/src/js/chrome')
| -rw-r--r-- | addons/web/static/src/js/chrome/action_manager_act_window.js | 17 | ||||
| -rw-r--r-- | addons/web/static/src/js/chrome/search_view.js | 40 |
2 files changed, 57 insertions, 0 deletions
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', }), @@ -774,6 +775,22 @@ ActionManager.include({ 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 @@ -607,6 +607,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 //-------------------------------------------------------------------------- |
