From dabd7bbe3107b5da9987a2363faed218874f07f0 Mon Sep 17 00:00:00 2001 From: Julien Banken Date: Wed, 4 Jan 2023 15:36:58 +0000 Subject: [PATCH] [IMP] web: graph: restorable order, stacked & cumulative options For the Knowledge module, we would like to allow the user to fully customize a graph view and let the user insert the graph they see in an article. When loading the graph view from an article, the system can not currently restore the following settings: - For the bar chart and the line chart: The system will not able to restore the "sorting order" and the "stack option" set by the user. - For the line chart: The system will not be able to restore the "cumulative option" set by the user. This commit will ensure that these options can be specified from the context of the view. The context configuration will have a higher precedence than the configuration stored in the metadata of the model. When the user will open an article containing a graph view, the graph view will automatically load the options that have been set by the user when inserting the view. The user can thus create their own dashboards by customizing the graphs and inserting them in an article. task-3056268 closes odoo/odoo#112600 X-original-commit: 458bf22c7e275b7893505bb39c16073a9f63b9ca Related: odoo/enterprise#37046 Signed-off-by: Thibault Delavallee (tde) --- addons/web/static/src/views/graph/graph_controller.js | 10 +++++++++- addons/web/static/src/views/graph/graph_model.js | 7 +++++++ addons/web/static/tests/views/graph_view_tests.js | 10 ++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/views/graph/graph_controller.js b/addons/web/static/src/views/graph/graph_controller.js index 9b69133ffb8..4e2000843fb 100644 --- a/addons/web/static/src/views/graph/graph_controller.js +++ b/addons/web/static/src/views/graph/graph_controller.js @@ -31,11 +31,19 @@ export class GraphController extends Component { getContext() { // expand context object? change keys? const { measure, groupBy, mode } = this.model.metaData; - return { + const context = { graph_measure: measure, graph_mode: mode, graph_groupbys: groupBy.map((gb) => gb.spec), }; + if (mode !== "pie") { + context.graph_order = this.model.metaData.order; + context.graph_stacked = this.model.metaData.stacked; + if (mode === "line") { + context.graph_cumulated = this.model.metaData.cumulated; + } + } + return context; } /** diff --git a/addons/web/static/src/views/graph/graph_model.js b/addons/web/static/src/views/graph/graph_model.js index 87df2bac813..5ec338e7692 100644 --- a/addons/web/static/src/views/graph/graph_model.js +++ b/addons/web/static/src/views/graph/graph_model.js @@ -163,6 +163,13 @@ export class GraphModel extends Model { metaData.measure = context.graph_measure || metaData.measure; metaData.mode = context.graph_mode || metaData.mode; metaData.groupBy = groupBy.length ? groupBy : this.initialGroupBy; + if (metaData.mode !== "pie") { + metaData.order = "graph_order" in context ? context.graph_order : metaData.order; + metaData.stacked = "graph_stacked" in context ? context.graph_stacked : metaData.stacked; + if (metaData.mode === "line") { + metaData.cumulated = "graph_cumulated" in context ? context.graph_cumulated : metaData.cumulated; + } + } this._normalize(metaData); diff --git a/addons/web/static/tests/views/graph_view_tests.js b/addons/web/static/tests/views/graph_view_tests.js index 4c7130af549..0367b40b147 100644 --- a/addons/web/static/tests/views/graph_view_tests.js +++ b/addons/web/static/tests/views/graph_view_tests.js @@ -2479,24 +2479,34 @@ QUnit.module("Views", (hooks) => { graph_mode: "bar", graph_measure: "__count", graph_groupbys: ["product_id"], + graph_order: null, + graph_stacked: true, group_by: [], }, { graph_mode: "bar", graph_measure: "foo", graph_groupbys: ["product_id"], + graph_order: null, + graph_stacked: true, group_by: [], }, { graph_mode: "line", graph_measure: "foo", + graph_cumulated: false, graph_groupbys: ["product_id"], + graph_order: null, + graph_stacked: true, group_by: [], }, { graph_mode: "line", graph_measure: "foo", + graph_cumulated: false, graph_groupbys: ["product_id", "color_id"], + graph_order: null, + graph_stacked: true, group_by: ["product_id", "color_id"], }, ];