From 5b29e2cd214e5fec2d1fc93a62d48ab8fd3ea0a4 Mon Sep 17 00:00:00 2001 From: anhe-odoo Date: Tue, 12 Sep 2023 23:34:49 +0200 Subject: [PATCH] [IMP] spreadsheet: Allow Odoo Chart domain edition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Task Description: This PR aims to add the possibility to edit the evaluation domain of an Odoo chart, the same way it can currently be done for other datasources like `List` or `Pivot`. This revision ensures that the chart datasources are properly reloaded their domain update. closes odoo/odoo#135242 Task: 3384872 Related: odoo/enterprise#47336 Signed-off-by: Rémi Rahir (rar) --- .../chart/plugins/odoo_chart_core_plugin.js | 12 ++-- .../src/chart/plugins/odoo_chart_ui_plugin.js | 69 +++++++++++++++++-- .../data_sources/odoo_views_data_source.js | 8 +++ .../src/pivot/plugins/pivot_ui_plugin.js | 5 +- .../charts/model/odoo_chart_plugin_test.js | 25 +++++-- 5 files changed, 101 insertions(+), 18 deletions(-) diff --git a/addons/spreadsheet/static/src/chart/plugins/odoo_chart_core_plugin.js b/addons/spreadsheet/static/src/chart/plugins/odoo_chart_core_plugin.js index 08541721a49..fdc106351fb 100644 --- a/addons/spreadsheet/static/src/chart/plugins/odoo_chart_core_plugin.js +++ b/addons/spreadsheet/static/src/chart/plugins/odoo_chart_core_plugin.js @@ -111,11 +111,13 @@ export default class OdooChartCorePlugin extends CorePlugin { * @returns {string} */ getOdooChartDisplayName(chartId) { - return this.getters.getChart(chartId).title; + return `(#${this.getOdooChartIds().indexOf(chartId) + 1}) ${ + this.getters.getChart(chartId).title + }`; } /** - * Import the pivots + * Import the charts * * @param {Object} data */ @@ -131,7 +133,7 @@ export default class OdooChartCorePlugin extends CorePlugin { } } /** - * Export the pivots + * Export the chart * * @param {Object} data */ @@ -154,7 +156,7 @@ export default class OdooChartCorePlugin extends CorePlugin { // ------------------------------------------------------------------------- /** - * Get the current pivotFieldMatching of a chart + * Get the current odooChartFieldMatching of a chart * * @param {string} chartId * @param {string} filterId @@ -164,7 +166,7 @@ export default class OdooChartCorePlugin extends CorePlugin { } /** - * Sets the current pivotFieldMatching of a chart + * Sets the current odooChartFieldMatching of a chart * * @param {string} filterId * @param {Record} chartFieldMatches diff --git a/addons/spreadsheet/static/src/chart/plugins/odoo_chart_ui_plugin.js b/addons/spreadsheet/static/src/chart/plugins/odoo_chart_ui_plugin.js index 9675982eb4d..1b2f74eaecf 100644 --- a/addons/spreadsheet/static/src/chart/plugins/odoo_chart_ui_plugin.js +++ b/addons/spreadsheet/static/src/chart/plugins/odoo_chart_ui_plugin.js @@ -60,9 +60,17 @@ export default class OdooChartUIPlugin extends UIPlugin { switch (cmd.definition.type) { case "odoo_pie": case "odoo_bar": - case "odoo_line": + case "odoo_line": { + const dataSource = this.getChartDataSource(cmd.id); + if ( + dataSource.getInitialDomainString() !== + new Domain(cmd.definition.searchParams.domain).toString() + ) { + this._resetChartDataSource(cmd.id); + } this._setChartDataSource(cmd.id); break; + } } break; } @@ -74,7 +82,7 @@ export default class OdooChartUIPlugin extends UIPlugin { this._addDomains(); break; case "UNDO": - case "REDO": + case "REDO": { if ( cmd.commands.find((command) => [ @@ -86,6 +94,29 @@ export default class OdooChartUIPlugin extends UIPlugin { ) { this._addDomains(); } + + const domainEditionCommands = cmd.commands.filter( + (cmd) => cmd.type === "UPDATE_CHART" || cmd.type === "CREATE_CHART" + ); + for (const cmd of domainEditionCommands) { + if (!this.getters.getOdooChartIds().includes(cmd.id)) { + continue; + } + const dataSource = this.getChartDataSource(cmd.id); + if ( + dataSource.getInitialDomainString() !== + new Domain(cmd.definition.searchParams.domain).toString() + ) { + this._resetChartDataSource(cmd.id); + } + } + break; + } + case "REFRESH_ODOO_CHART": + this._refreshOdooChart(cmd.chartId); + break; + case "REFRESH_ALL_DATA_SOURCES": + this._refreshOdooCharts(); break; } } @@ -118,7 +149,7 @@ export default class OdooChartUIPlugin extends UIPlugin { domainList.push(this.getters.getGlobalFilterDomain(filterId, fieldMatch)); } const domain = Domain.combine(domainList, "AND").toString(); - this.getters.getChartDataSource(chartId).addDomain(domain); + this.getChartDataSource(chartId).addDomain(domain); } /** @@ -139,15 +170,24 @@ export default class OdooChartUIPlugin extends UIPlugin { */ _setupChartDataSource(chartId) { const dataSourceId = this._getOdooChartDataSourceId(chartId); - const definition = this.getters.getChart(chartId).getDefinitionForDataSource(); if (!this.dataSources.contains(dataSourceId)) { - this.dataSources.add(dataSourceId, ChartDataSource, definition); + this._resetChartDataSource(chartId); } this._setChartDataSource(chartId); } /** - * Sets the catasource on the corresponding chart + * Sets the datasource on the corresponding chart + * @param {string} chartId + */ + _resetChartDataSource(chartId) { + const definition = this.getters.getChart(chartId).getDefinitionForDataSource(); + const dataSourceId = this._getOdooChartDataSourceId(chartId); + this.dataSources.add(dataSourceId, ChartDataSource, definition); + } + + /** + * Sets the datasource on the corresponding chart * @param {string} chartId */ _setChartDataSource(chartId) { @@ -168,6 +208,23 @@ export default class OdooChartUIPlugin extends UIPlugin { _getOdooChartDataSourceId(chartId) { return `chart-${chartId}`; } + + /** + * Refresh the cache of a chart + * @param {string} chartId Id of the chart + */ + _refreshOdooChart(chartId) { + this.getChartDataSource(chartId).load({ reload: true }); + } + + /** + * Refresh the cache of all the charts + */ + _refreshOdooCharts() { + for (const chartId of this.getters.getOdooChartIds()) { + this._refreshOdooChart(chartId); + } + } } OdooChartUIPlugin.getters = ["getChartDataSource"]; diff --git a/addons/spreadsheet/static/src/data_sources/odoo_views_data_source.js b/addons/spreadsheet/static/src/data_sources/odoo_views_data_source.js index 0fe4b5c82c3..3ef358936a6 100644 --- a/addons/spreadsheet/static/src/data_sources/odoo_views_data_source.js +++ b/addons/spreadsheet/static/src/data_sources/odoo_views_data_source.js @@ -97,6 +97,14 @@ export class OdooViewsDataSource extends LoadableDataSource { }); } + /** + * Get the current domain as a string + * @returns { string } + */ + getInitialDomainString() { + return new Domain(this._initialSearchParams.domain).toString(); + } + /** * * @param {string} domain diff --git a/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js b/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js index 88a3ac603d1..4e250174ad4 100644 --- a/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js +++ b/addons/spreadsheet/static/src/pivot/plugins/pivot_ui_plugin.js @@ -410,11 +410,10 @@ export default class PivotUIPlugin extends spreadsheet.UIPlugin { /** * @param {string} pisvotId - * @param {PivotDefinition=} definition */ - _setupPivotDataSource(pivotId, definition) { + _setupPivotDataSource(pivotId) { const dataSourceId = this.getPivotDataSourceId(pivotId); - definition = definition || this.getters.getPivotModelDefinition(pivotId); + const definition = this.getters.getPivotModelDefinition(pivotId); if (!this.dataSources.contains(dataSourceId)) { this.dataSources.add(dataSourceId, PivotDataSource, definition); } diff --git a/addons/spreadsheet/static/tests/charts/model/odoo_chart_plugin_test.js b/addons/spreadsheet/static/tests/charts/model/odoo_chart_plugin_test.js index 8dd77aba16e..461abd78333 100644 --- a/addons/spreadsheet/static/tests/charts/model/odoo_chart_plugin_test.js +++ b/addons/spreadsheet/static/tests/charts/model/odoo_chart_plugin_test.js @@ -148,7 +148,7 @@ QUnit.module("spreadsheet > odoo chart plugin", {}, () => { assert.verifySteps(["web_read_group"], "it should have loaded the data"); }); - QUnit.test("Changing the chart type does not reload the data", async (assert) => { + QUnit.test("Data reloaded strictly upon domain update", async (assert) => { const { model } = await createSpreadsheetWithChart({ type: "odoo_line", mockRPC: async function (route, args) { @@ -164,20 +164,37 @@ QUnit.module("spreadsheet > odoo chart plugin", {}, () => { // force runtime computation model.getters.getChartRuntime(chartId); await nextTick(); - assert.verifySteps(["web_read_group"], "it should have loaded the data"); + model.dispatch("UPDATE_CHART", { definition: { ...definition, + searchParams: { ...definition.searchParams, domain: [["1", "=", "1"]] }, + }, + id: chartId, + sheetId, + }); + // force runtime computation + model.getters.getChartRuntime(chartId); + await nextTick(); + assert.verifySteps(["web_read_group"], "it should have loaded the data with a new domain"); + + const newDefinition = model.getters.getChartDefinition(chartId); + model.dispatch("UPDATE_CHART", { + definition: { + ...newDefinition, type: "odoo_bar", }, id: chartId, sheetId, }); - await nextTick(); // force runtime computation model.getters.getChartRuntime(chartId); - assert.verifySteps([], "it should have not have loaded the data a second time"); + await nextTick(); + assert.verifySteps( + [], + "it should have not have loaded the data since the domain was unchanged" + ); }); QUnit.test("Can import/export an Odoo chart", async (assert) => {