diff --git a/addons/web/static/src/js/views/graph/graph_controller.js b/addons/web/static/src/js/views/graph/graph_controller.js index 388d390701c..d68808bac1d 100644 --- a/addons/web/static/src/js/views/graph/graph_controller.js +++ b/addons/web/static/src/js/views/graph/graph_controller.js @@ -39,6 +39,22 @@ var GraphController = AbstractController.extend({ // Public //-------------------------------------------------------------------------- + /** + * Returns the current mode, measure and groupbys, so we can restore the + * view when we save the current state in the search view, or when we add it + * to the dashboard. + * + * @override + * @returns {Object} + */ + getContext: function () { + var state = this.model.get(); + return { + graph_measure: state.measure, + graph_mode: state.mode, + graph_groupbys: state.groupedBy, + }; + }, /** * Render the buttons according to the GraphView.buttons and * add listeners on it. diff --git a/addons/web/static/tests/views/graph_tests.js b/addons/web/static/tests/views/graph_tests.js index faab8420fbb..df6e500d181 100644 --- a/addons/web/static/tests/views/graph_tests.js +++ b/addons/web/static/tests/views/graph_tests.js @@ -251,6 +251,59 @@ QUnit.module('Views', { }); }); + QUnit.test('getContext correctly returns mode, measure and groupbys', function (assert) { + var done = assert.async(); + assert.expect(4); + + var graph = createView({ + View: GraphView, + model: "foo", + data: this.data, + arch: '' + + '' + + '', + }); + return concurrency.delay(0).then(function () { + assert.deepEqual(graph.getContext(), { + graph_mode: 'bar', + graph_measure: '__count__', + graph_groupbys: ['product_id'], + }, "context should be correct"); + + graph.$buttons.find('li[data-field="foo"] a').click(); // change measure + + return concurrency.delay(0); + }).then(function () { + assert.deepEqual(graph.getContext(), { + graph_mode: 'bar', + graph_measure: 'foo', + graph_groupbys: ['product_id'], + }, "context should be correct"); + + graph.$buttons.find('button[data-mode="line"]').click(); // change mode + + return concurrency.delay(0); + }).then(function () { + assert.deepEqual(graph.getContext(), { + graph_mode: 'line', + graph_measure: 'foo', + graph_groupbys: ['product_id'], + }, "context should be correct"); + + graph.update({groupBy: ['product_id', 'color_id']}); // change groupbys + + return concurrency.delay(0); + }).then(function () { + assert.deepEqual(graph.getContext(), { + graph_mode: 'line', + graph_measure: 'foo', + graph_groupbys: ['product_id', 'color_id'], + }, "context should be correct"); + + graph.destroy(); + done(); + }); + }); }); });