From 0bba2cbb67f312d3e53f11fe257ed64cfba0380c Mon Sep 17 00:00:00 2001 From: "Adrien Minne (adrm)" Date: Tue, 2 May 2023 09:34:52 +0000 Subject: [PATCH] [REF] spreadsheet: move chart dataSources to UI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit, the chart's dataSources were managed by the core plugin `OdooChartCorePlugin`. This didn't make sense since dataSources aren't core data: - they are neither exported nor imported - they can be different for each user (their ids were generated via uuidv4 at import) Moved the dataSources management to the UI plugin `OdooChartUIPlugin` and moved the relevant getters inside this plugin. Changed the ids of the dataSources to be the same as the chart's id + a prefix, to avoid having to maintain a mapping `chartId` <=> `dataSourceId` . closes odoo/odoo#120136 Task: 3293491 Related: odoo/enterprise#40546 Signed-off-by: Lucas Lefèvre (lul) --- .../chart/plugins/odoo_chart_core_plugin.js | 59 +----------- .../src/chart/plugins/odoo_chart_ui_plugin.js | 90 ++++++++++++++++++- 2 files changed, 90 insertions(+), 59 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 61ae8f2d2d9..9417805556f 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 @@ -1,9 +1,6 @@ /** @odoo-module */ import * as spreadsheet from "@odoo/o-spreadsheet"; -import ChartDataSource from "../data_source/chart_data_source"; import { globalFiltersFieldMatchers } from "@spreadsheet/global_filters/plugins/global_filters_core_plugin"; -import { sprintf } from "@web/core/utils/strings"; -import { _t } from "@web/core/l10n/translation"; import { checkFilterFieldMatching } from "@spreadsheet/global_filters/helpers"; import CommandResult from "../../o_spreadsheet/cancelled_reason"; @@ -11,7 +8,6 @@ const { CorePlugin } = spreadsheet; /** * @typedef {Object} Chart - * @property {string} dataSourceId * @property {Object} fieldMatching * * @typedef {import("@spreadsheet/global_filters/plugins/global_filters_core_plugin").FieldMatching} FieldMatching @@ -20,7 +16,6 @@ const { CorePlugin } = spreadsheet; export default class OdooChartCorePlugin extends CorePlugin { constructor(config) { super(config); - this.dataSources = config.custom.dataSources; /** @type {Object.} */ this.charts = {}; @@ -28,16 +23,10 @@ export default class OdooChartCorePlugin extends CorePlugin { globalFiltersFieldMatchers["chart"] = { geIds: () => this.getters.getOdooChartIds(), getDisplayName: (chartId) => this.getters.getOdooChartDisplayName(chartId), - getTag: async (chartId) => { - const model = await this.getChartDataSource(chartId).getModelLabel(); - return sprintf(_t("Chart - %s"), model); - }, getFieldMatching: (chartId, filterId) => this.getOdooChartFieldMatching(chartId, filterId), - waitForReady: () => this.getOdooChartsWaitForReady(), getModel: (chartId) => this.getters.getChart(chartId).getDefinitionForDataSource().metaData.resModel, - getFields: (chartId) => this.getChartDataSource(chartId).getFields(), }; } @@ -69,16 +58,6 @@ export default class OdooChartCorePlugin extends CorePlugin { } break; } - case "UPDATE_CHART": { - switch (cmd.definition.type) { - case "odoo_pie": - case "odoo_bar": - case "odoo_line": - this._setChartDataSource(cmd.id); - break; - } - break; - } case "DELETE_FIGURE": { const charts = { ...this.charts }; delete charts[cmd.id]; @@ -125,15 +104,6 @@ export default class OdooChartCorePlugin extends CorePlugin { return this.charts[chartId].fieldMatching; } - /** - * @param {string} id - * @returns {ChartDataSource|undefined} - */ - getChartDataSource(id) { - const dataSourceId = this.charts[id].dataSourceId; - return this.dataSources.get(dataSourceId); - } - /** * * @param {string} chartId @@ -179,16 +149,6 @@ export default class OdooChartCorePlugin extends CorePlugin { // Private // ------------------------------------------------------------------------- - /** - * - * @return {Promise[]} - */ - getOdooChartsWaitForReady() { - return this.getOdooChartIds().map((chartId) => - this.getChartDataSource(chartId).loadMetadata() - ); - } - /** * Get the current pivotFieldMatching of a chart * @@ -222,35 +182,18 @@ export default class OdooChartCorePlugin extends CorePlugin { /** * @param {string} chartId - * @param {string} dataSourceId + * @param {Object} fieldMatching */ _addOdooChart(chartId, fieldMatching = {}) { - const dataSourceId = this.uuidGenerator.uuidv4(); const charts = { ...this.charts }; charts[chartId] = { - dataSourceId, fieldMatching, }; - const definition = this.getters.getChart(chartId).getDefinitionForDataSource(); - if (!this.dataSources.contains(dataSourceId)) { - this.dataSources.add(dataSourceId, ChartDataSource, definition); - } this.history.update("charts", charts); - this._setChartDataSource(chartId); - } - - /** - * Sets the catasource on the corresponding chart - * @param {string} chartId - */ - _setChartDataSource(chartId) { - const chart = this.getters.getChart(chartId); - chart.setDataSource(this.getters.getChartDataSource(chartId)); } } OdooChartCorePlugin.getters = [ - "getChartDataSource", "getOdooChartIds", "getChartFieldMatch", "getOdooChartDisplayName", 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 7d3d38821c6..9675982eb4d 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 @@ -2,13 +2,36 @@ import * as spreadsheet from "@odoo/o-spreadsheet"; import { Domain } from "@web/core/domain"; +import { globalFiltersFieldMatchers } from "@spreadsheet/global_filters/plugins/global_filters_core_plugin"; +import ChartDataSource from "../data_source/chart_data_source"; +import { sprintf } from "@web/core/utils/strings"; +import { _t } from "@web/core/l10n/translation"; const { UIPlugin } = spreadsheet; export default class OdooChartUIPlugin extends UIPlugin { + constructor(config) { + super(config); + this.dataSources = config.custom.dataSources; + + globalFiltersFieldMatchers["chart"] = { + ...globalFiltersFieldMatchers["chart"], + getTag: async (chartId) => { + const model = await this.getChartDataSource(chartId).getModelLabel(); + return sprintf(_t("Chart - %s"), model); + }, + waitForReady: () => this._getOdooChartsWaitForReady(), + getFields: (chartId) => this.getChartDataSource(chartId).getFields(), + }; + } + beforeHandle(cmd) { switch (cmd.type) { case "START": + for (const chartId of this.getters.getOdooChartIds()) { + this._setupChartDataSource(chartId); + } + // make sure the domains are correctly set before // any evaluation this._addDomains(); @@ -23,6 +46,26 @@ export default class OdooChartUIPlugin extends UIPlugin { */ handle(cmd) { switch (cmd.type) { + case "CREATE_CHART": { + switch (cmd.definition.type) { + case "odoo_pie": + case "odoo_bar": + case "odoo_line": + this._setupChartDataSource(cmd.id); + break; + } + break; + } + case "UPDATE_CHART": { + switch (cmd.definition.type) { + case "odoo_pie": + case "odoo_bar": + case "odoo_line": + this._setChartDataSource(cmd.id); + break; + } + break; + } case "ADD_GLOBAL_FILTER": case "EDIT_GLOBAL_FILTER": case "REMOVE_GLOBAL_FILTER": @@ -47,6 +90,15 @@ export default class OdooChartUIPlugin extends UIPlugin { } } + /** + * @param {string} chartId + * @returns {ChartDataSource|undefined} + */ + getChartDataSource(chartId) { + const dataSourceId = this._getOdooChartDataSourceId(chartId); + return this.dataSources.get(dataSourceId); + } + // ------------------------------------------------------------------------- // Private // ------------------------------------------------------------------------- @@ -80,6 +132,42 @@ export default class OdooChartUIPlugin extends UIPlugin { this._addDomain(chartId); } } + + /** + * @param {string} chartId + * @param {string} dataSourceId + */ + _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._setChartDataSource(chartId); + } + + /** + * Sets the catasource on the corresponding chart + * @param {string} chartId + */ + _setChartDataSource(chartId) { + const chart = this.getters.getChart(chartId); + chart.setDataSource(this.getChartDataSource(chartId)); + } + + /** + * + * @return {Promise[]} + */ + _getOdooChartsWaitForReady() { + return this.getters + .getOdooChartIds() + .map((chartId) => this.getChartDataSource(chartId).loadMetadata()); + } + + _getOdooChartDataSourceId(chartId) { + return `chart-${chartId}`; + } } -OdooChartUIPlugin.getters = []; +OdooChartUIPlugin.getters = ["getChartDataSource"];