[REF] spreadsheet: move chart dataSources to UI

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) <lul@odoo.com>
This commit is contained in:
Adrien Minne (adrm)
2023-05-05 17:06:45 +02:00
parent bc56084355
commit 0bba2cbb67
2 changed files with 90 additions and 59 deletions
@@ -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.<string, Chart>} */
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",
@@ -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"];