From 69c9500e355be8ae605294c9f0453e91e4b73a2e Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Mon, 26 Mar 2018 13:28:43 +0200 Subject: [PATCH] [IMP] ActionManager: keepSearchView option This rev. introduces an option to doAction (keepSearchView). If set to true, the searchView of the current action will be kept and used by the new action (this only works if there is already an action with a searchView, and if the new action is stacked over that one). This is necessary for the new dashboard view. A better solution would have been to export the state of the existing searchView, and import it to the new one, but the current implementation of the searchView doesn't allow to do that easily. This will be done as soon as the searchView will be rewrote. --- .../js/chrome/action_manager_act_window.js | 32 +++++++++++- .../tests/chrome/action_manager_tests.js | 52 ++++++++++++++++--- 2 files changed, 75 insertions(+), 9 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 f505cccadfe..bff130f5b63 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 @@ -121,6 +121,22 @@ ActionManager.include({ * @returns {Deferred} resolved with the search view when it is ready */ _createSearchView: function (action) { + // if requested, keep the searchview of the current action instead of + // creating a new one + if (action._keepSearchView) { + var currentAction = this.getCurrentAction(); + if (currentAction) { + action.searchView = currentAction.searchView; + action.env = currentAction.env; // make those actions share the same env + return $.when(currentAction.searchView); + } else { + // there is not searchview to keep, so reset the flag to false + // to ensure that the one that will be created will be correctly + // destroyed + action._keepSearchView = false; + } + } + // AAB: temporarily create a dataset, until the SearchView is refactored // and stops using it var dataset = new data.DataSetSearch(this, action.res_model, action.context, action.domain); @@ -254,7 +270,7 @@ ActionManager.include({ // its reference is removed controllerDef.reject(); }); - if (action.searchView) { + if (action.searchView && !action._keepSearchView) { action.searchView.destroy(); } }, @@ -483,6 +499,20 @@ ActionManager.include({ } return this.loadViews(action.res_model, action.context, views, options); }, + /** + * Overrides to handle the 'keepSearchView' option. If set to true, the + * search view of the current action will be re-used in the new action, i.e. + * the environment (domain, context, groupby) will be shared between both + * actions. + * + * @override + */ + _preprocessAction: function (action, options) { + this._super.apply(this, arguments); + if (action.type === 'ir.actions.act_window' && options.keepSearchView) { + action._keepSearchView = true; + } + }, /** * Processes the search data sent by the search view. * diff --git a/addons/web/static/tests/chrome/action_manager_tests.js b/addons/web/static/tests/chrome/action_manager_tests.js index 5859f16ca36..ff8fe80a523 100644 --- a/addons/web/static/tests/chrome/action_manager_tests.js +++ b/addons/web/static/tests/chrome/action_manager_tests.js @@ -126,6 +126,9 @@ QUnit.module('ActionManager', { // search views 'partner,false,search': '', + 'partner,1,search': '' + + '' + + '', 'pony,false,search': '', }; }, @@ -2714,13 +2717,6 @@ QUnit.module('ActionManager', { }, }); - _.extend(this.archs, { - 'partner,7,search': - '' + - '' + - '', - }); - this.actions.push({ id: 33, context: { @@ -2728,7 +2724,7 @@ QUnit.module('ActionManager', { }, name: 'Partners', res_model: 'partner', - search_view_id: [7, 'a custom search view'], + search_view_id: [1, 'a custom search view'], type: 'ir.actions.act_window', views: [[false, 'list']], }); @@ -2792,6 +2788,46 @@ QUnit.module('ActionManager', { actionManager.destroy(); }); + QUnit.test("doAction with option 'keepSearchView'", function (assert) { + assert.expect(4); + + this.actions.push({ + id: 33, + name: 'Partners', + res_model: 'partner', + search_view_id: [1, 'a specific search view'], + type: 'ir.actions.act_window', + views: [[false, 'list']], + }); + + var checkRPC = false; + var actionManager = createActionManager({ + actions: this.actions, + archs: this.archs, + data: this.data, + mockRPC: function (route, args) { + if (checkRPC && route === '/web/dataset/search_read') { + assert.deepEqual(args.domain, [['bar', '=', 1]], + "should search with the correct domain"); + } + return this._super.apply(this, arguments); + }, + }); + + actionManager.doAction(33); + + checkRPC = true; + $('.o_control_panel .o_filters_menu a:contains(Bar)').click(); // filter on bar + assert.strictEqual($('.o_control_panel .o_facet_values').text().trim(), 'Bar', + "the filter on Bar should appear in the search view"); + + actionManager.doAction(3, {keepSearchView: true}); + assert.strictEqual($('.o_control_panel .o_facet_values').text().trim(), 'Bar', + "the filter on Bar should still be in the search view"); + + actionManager.destroy(); + }); + QUnit.module('Actions in target="new"'); QUnit.test('can execute act_window actions in target="new"', function (assert) {