From 187c32c2c72fc0668a97c97a99123107aebf3d06 Mon Sep 17 00:00:00 2001 From: "Lucas Perais (lpe)" Date: Tue, 11 Dec 2018 09:23:17 +0000 Subject: [PATCH] [FIX] web: pivot correct context after reload Open a pivot view with default filters automatically activating Disable those filters Change the groupBys on the rows and cols of the pivot Save your result as a favorite filter Before this commit, the filter did not contain the context information for columns groupby Also, the search view did not exploit the context of the new filter as it should be After this commit, the flow works as expected OPW 1913732 closes odoo/odoo#29415 --- .../web/static/src/js/chrome/search_menus.js | 6 +- .../static/src/js/views/pivot/pivot_model.js | 35 +++++---- addons/web/static/tests/views/pivot_tests.js | 74 +++++++++++++++++++ 3 files changed, 100 insertions(+), 15 deletions(-) diff --git a/addons/web/static/src/js/chrome/search_menus.js b/addons/web/static/src/js/chrome/search_menus.js index 99e8bd6e799..c52be2de08b 100644 --- a/addons/web/static/src/js/chrome/search_menus.js +++ b/addons/web/static/src/js/chrome/search_menus.js @@ -191,6 +191,10 @@ return Widget.extend({ icon: 'fa-star', field: { get_context: function () { + var filterContext = filter.context; + if (typeof filter.context === 'string') { + filterContext = pyeval.eval('context', filter.context); + } var sortParsed = JSON.parse(filter.sort || "[]"); var orderedBy = []; _.each(sortParsed, function (sort) { @@ -200,7 +204,7 @@ return Widget.extend({ }); }); - return _.defaults({}, filter.context, {orderedBy : orderedBy}); + return _.defaults({}, filterContext, {orderedBy : orderedBy}); }, get_groupby: function () { return [filter.context]; }, get_domain: function () { return filter.domain; } diff --git a/addons/web/static/src/js/views/pivot/pivot_model.js b/addons/web/static/src/js/views/pivot/pivot_model.js index 6de39c0b535..95815a03e76 100644 --- a/addons/web/static/src/js/views/pivot/pivot_model.js +++ b/addons/web/static/src/js/views/pivot/pivot_model.js @@ -191,7 +191,7 @@ var PivotModel = AbstractModel.extend({ return {has_data: false}; } return { - colGroupBys: this.data.main_col.groupbys, + colGroupBys: this.data.main_col.root.groupbys, context: this.data.context, domain: this.data.domain, fields: this.fields, @@ -200,7 +200,7 @@ var PivotModel = AbstractModel.extend({ mainColWidth: this.data.main_col.width, measures: this.data.measures, rows: !isRaw && this._computeRows(), - rowGroupBys: this.data.main_row.groupbys, + rowGroupBys: this.data.main_row.root.groupbys, sortedColumn: this.data.sorted_column, }; }, @@ -266,22 +266,19 @@ var PivotModel = AbstractModel.extend({ return this._loadData(); } - var old_row_root = this.data.main_row.root; - var old_col_root = this.data.main_col.root; + var old_row = this.data.main_row; + var old_col = this.data.main_col; return this._loadData().then(function () { - var new_groupby_length; if (!('groupBy' in params) && !('pivot_row_groupby' in (params.context || {}))) { // we only update the row groupbys according to the old groupbys // if we don't have the key 'groupBy' in params. In that case, // we want to have the full open state for the groupbys. - self._updateTree(old_row_root, self.data.main_row.root); - new_groupby_length = self._getHeaderDepth(self.data.main_row.root) - 1; - self.data.main_row.groupbys = old_row_root.groupbys.slice(0, new_groupby_length); + self._updateTree(old_row.root, self.data.main_row.root); + self._updateMainGroupBys(old_row, self.data.main_row); } - self._updateTree(old_col_root, self.data.main_col.root); - new_groupby_length = self._getHeaderDepth(self.data.main_col.root) - 1; - self.data.main_row.groupbys = old_row_root.groupbys.slice(0, new_groupby_length); + self._updateTree(old_col.root, self.data.main_col.root); + self._updateMainGroupBys(old_col, self.data.main_col); }); }, /** @@ -644,9 +641,6 @@ var PivotModel = AbstractModel.extend({ } } - this.data.main_row.groupbys = rowGroupBys; - this.data.main_col.groupbys = colGroupBys; - main_row_header.other_root = main_col_header; main_col_header.other_root = main_row_header; @@ -715,6 +709,19 @@ var PivotModel = AbstractModel.extend({ this._traverseTree(root.children[i], f, arg1, arg2, arg3); } }, + /** + * Updates the groupBys that the main[row | col] holds + * + * @private + * @param {Object} old: The main abstract header before reload + * @param {Object} main: The main abstract header after reload + * + */ + _updateMainGroupBys: function (old, main) { + var new_groupby_length = this._getHeaderDepth(main.root) - 1; + var new_groupby_list = old.root.groupbys.slice(0, new_groupby_length); + main.root.groupbys = new_groupby_list; + }, /** * @param {Object} old_tree * @param {Object} new_tree diff --git a/addons/web/static/tests/views/pivot_tests.js b/addons/web/static/tests/views/pivot_tests.js index 67fc1d97bcc..1d4811009ba 100644 --- a/addons/web/static/tests/views/pivot_tests.js +++ b/addons/web/static/tests/views/pivot_tests.js @@ -825,6 +825,80 @@ QUnit.module('Views', { pivot.destroy(); }); + QUnit.test('correctly remove pivot_ keys from the context', function (assert) { + assert.expect(5); + + this.data.partner.fields.amount = {string: "Amount", type: "float"}; + + // Equivalent to loading with default filter + var pivot = createView({ + View: PivotView, + model: "partner", + data: this.data, + arch: '' + + '' + + '' + + '', + viewOptions: { + context: { + pivot_measures: ['foo'], + pivot_column_groupby: ['customer'], + pivot_row_groupby: ['product_id'], + }, + }, + }); + + // Equivalent to unload the filter + var reloadParams = { + context: {}, + }; + pivot.reload(reloadParams); + + assert.deepEqual(pivot.getContext(), { + pivot_column_groupby: ['customer'], + pivot_measures: ['foo'], + pivot_row_groupby: ['product_id'], + }, "context should be correct"); + + // Let's get rid of the rows groupBy + pivot.$('tbody .o_pivot_header_cell_opened').click(); + + assert.deepEqual(pivot.getContext(), { + pivot_column_groupby: ['customer'], + pivot_measures: ['foo'], + pivot_row_groupby: [], + }, "context should be correct"); + + // And now, get rid of the col groupby + pivot.$('thead .o_pivot_header_cell_opened').click(); + + assert.deepEqual(pivot.getContext(), { + pivot_column_groupby: [], + pivot_measures: ['foo'], + pivot_row_groupby: [], + }, "context should be correct"); + + pivot.$('tbody .o_pivot_header_cell_closed').click(); + pivot.$('.o_pivot_field_menu li[data-field=product_id] a').click(); + + assert.deepEqual(pivot.getContext(), { + pivot_column_groupby: [], + pivot_measures: ['foo'], + pivot_row_groupby: ['product_id'], + }, "context should be correct"); + + pivot.$('thead .o_pivot_header_cell_closed').click(); + pivot.$('.o_pivot_field_menu li[data-field=customer] a').click(); + + assert.deepEqual(pivot.getContext(), { + pivot_column_groupby: ['customer'], + pivot_measures: ['foo'], + pivot_row_groupby: ['product_id'], + }, "context should be correct"); + + pivot.destroy(); + }); + QUnit.test('correctly uses pivot_ keys from the context', function (assert) { assert.expect(7);