From 932de2fc0f431109fba83e4e6f4fb61d8c8a661e Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Wed, 8 Sep 2021 07:02:30 +0000 Subject: [PATCH] [FIX] web: pivot: can toggle several measures at once Before this commit, if the user quickly clicked on several measures (before the reload is over), only the last click was taken into account. With this commit, all changes are aggregated such that we only display the final result, with the desired measures. X-original-commit: a4bae57e1f3b095a4e2d35b3da424c34df59b8dd Part-of: odoo/odoo#76576 --- .../web/static/src/views/pivot/pivot_model.js | 10 ++++- .../static/tests/views/pivot_view_tests.js | 37 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/views/pivot/pivot_model.js b/addons/web/static/src/views/pivot/pivot_model.js index 1a6723787a0..8af7fc813a5 100644 --- a/addons/web/static/src/views/pivot/pivot_model.js +++ b/addons/web/static/src/views/pivot/pivot_model.js @@ -379,6 +379,8 @@ export class PivotModel extends Model { domains: null, // not the same as searchParams.domains from cp origins: null, // generated from searchParams.domains from cp }; + + this.nextActiveMeasures = null; // allows to toggle several measures consecutively } //-------------------------------------------------------------------------- @@ -741,17 +743,21 @@ export class PivotModel extends Model { */ async toggleMeasure(fieldName) { const meta = this._buildMeta(); + this.nextActiveMeasures = this.nextActiveMeasures || meta.activeMeasures; + meta.activeMeasures = this.nextActiveMeasures; const index = meta.activeMeasures.indexOf(fieldName); if (index !== -1) { - this._cancelPreviousOperation(); // in this case, we already have all data in memory, no need to - // actually reload a lesser amount of information + // actually reload a lesser amount of information (but still, we need + // to wait in case there is a pending load) meta.activeMeasures.splice(index, 1); + await Promise.resolve(this.loadProm); } else { meta.activeMeasures.push(fieldName); const config = { meta, data: this.data, searchParams: this.searchParams }; this.data = await this._loadData(config); } + this.nextActiveMeasures = null; this.meta = meta; this.notify(); } diff --git a/addons/web/static/tests/views/pivot_view_tests.js b/addons/web/static/tests/views/pivot_view_tests.js index 3ddf7760f55..4839504cdf5 100644 --- a/addons/web/static/tests/views/pivot_view_tests.js +++ b/addons/web/static/tests/views/pivot_view_tests.js @@ -4845,6 +4845,43 @@ QUnit.module("Views", (hooks) => { assert.strictEqual(getCurrentValues(webClient), ["32", "12", "12", "20"].join(",")); }); + QUnit.test("consecutively toggle several measures", async function (assert) { + let def; + const pivot = await makeView({ + type: "pivot", + resModel: "partner", + serverData, + arch: ` + + + + `, + additionalMeasures: ["product_id"], + mockRPC(route, args) { + if (args.method === "read_group") { + return Promise.resolve(def); + } + }, + }); + + assert.strictEqual(getCurrentValues(pivot), ["32", "12", "20"].join(",")); + + // Toggle several measures (the reload is blocked, so all measures should be toggled in once) + def = makeDeferred(); + await toggleMenu(pivot, "Measures"); + await toggleMenuItem(pivot, "Product"); // add product + assert.strictEqual(getCurrentValues(pivot), ["32", "12", "20"].join(",")); + await toggleMenuItem(pivot, "Foo"); // remove foo + assert.strictEqual(getCurrentValues(pivot), ["32", "12", "20"].join(",")); + await toggleMenuItem(pivot, "Count"); // add count + assert.strictEqual(getCurrentValues(pivot), ["32", "12", "20"].join(",")); + + def.resolve(); + await nextTick(); + + assert.strictEqual(getCurrentValues(pivot), ["2", "4", "1", "1", "1", "3"].join(",")); + }); + QUnit.test( "if no measure is set in arch, 'Count' is used as measure initially", async function (assert) {