summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Bohy <aab@odoo.com>2017-06-01 10:26:50 +0200
committerAaron Bohy <aab@odoo.com>2017-06-01 12:02:17 +0200
commit2f4d3d0e667092f70f44db03437473acf4ad5b32 (patch)
treec2391824366cbbcbf75d4de7fbed27ba9bfb88d3
parent033aec082669d6fb8126d6740b8aa7d09a2831d3 (diff)
[FIX] web: Graph: don't ignore graph keys in context
Keys 'graph_mode', 'graph_measure' and 'graph_groupbys' can be specified in the context and must be used by the graph view to determine the default mode, meadure and groupbys. Before this rev., those keys were simply ignored.
-rw-r--r--addons/web/static/src/js/views/graph/graph_model.js24
-rw-r--r--addons/web/static/src/js/views/graph/graph_view.js16
-rw-r--r--addons/web/static/tests/views/graph_tests.js40
3 files changed, 57 insertions, 23 deletions
diff --git a/addons/web/static/src/js/views/graph/graph_model.js b/addons/web/static/src/js/views/graph/graph_model.js
index 877472a7bc2..5948e07a54f 100644
--- a/addons/web/static/src/js/views/graph/graph_model.js
+++ b/addons/web/static/src/js/views/graph/graph_model.js
@@ -46,30 +46,23 @@ return AbstractModel.extend({
* should be done by the graphView I think.
*
* @param {Object} params
- * @param {string[]} params.groupedBy
- * @param {string} params.initialMode one of 'pie', 'bar', 'line
- * @param {string} params.initialMeasure a valid field name
- * @param {string[]} params.initialGroupBys a list of valid field names
+ * @param {string} params.mode one of 'pie', 'bar', 'line
+ * @param {string} params.measure a valid field name
+ * @param {string[]} params.groupBys a list of valid field names
* @param {Object} params.context
* @param {string[]} params.domain
* @returns {Deferred} The deferred does not return a handle, we don't need
* to keep track of various entities.
*/
load: function (params) {
- this.initialGroupBys = params.initialGroupBys;
+ this.initialGroupBys = params.groupBys;
this.fields = params.fields;
this.modelName = params.modelName;
- if (!params.groupedBy.length) {
- var groupedBy = params.context.graph_groupbys ||
- (params.groupedBy.length && params.groupedBy) ||
- this.initialGroupBys;
- params.groupedBy = groupedBy.slice(0);
- }
this.chart = {
data: [],
- groupedBy: params.groupedBy,
- measure: params.initialMeasure,
- mode: params.initialMode,
+ groupedBy: params.groupBys,
+ measure: params.measure,
+ mode: params.mode,
domain: params.domain,
context: params.context,
};
@@ -130,13 +123,14 @@ return AbstractModel.extend({
if (this.chart.measure !== '__count__') {
fields = fields.concat(this.chart.measure);
}
+ var groupedBy = this.chart.groupedBy.length ? this.chart.groupedBy : this.initialGroupBys;
return this._rpc({
model: this.modelName,
method: 'read_group',
context: this.chart.context,
domain: this.chart.domain,
fields: fields,
- groupBy: this.chart.groupedBy,
+ groupBy: groupedBy,
lazy: false,
})
.then(this._processData.bind(this));
diff --git a/addons/web/static/src/js/views/graph/graph_view.js b/addons/web/static/src/js/views/graph/graph_view.js
index 1d4ee29ef63..08b5ce1968f 100644
--- a/addons/web/static/src/js/views/graph/graph_view.js
+++ b/addons/web/static/src/js/views/graph/graph_view.js
@@ -27,11 +27,11 @@ var GraphView = AbstractView.extend({
/**
* @override
*/
- init: function (viewInfo) {
+ init: function (viewInfo, params) {
this._super.apply(this, arguments);
- var initialMeasure = '__count__';
- var initialGroupBys = [];
+ var measure;
+ var groupBys = [];
viewInfo.fields = _.defaults({__count__: {string: _t("Count"), type: "integer"}}, viewInfo.fields);
viewInfo.arch.children.forEach(function (field) {
var name = field.attrs.name;
@@ -39,9 +39,9 @@ var GraphView = AbstractView.extend({
name += ':' + field.attrs.interval;
}
if (field.attrs.type === 'measure') {
- initialMeasure = name;
+ measure = name;
} else {
- initialGroupBys.push(name);
+ groupBys.push(name);
}
});
@@ -57,9 +57,9 @@ var GraphView = AbstractView.extend({
this.controllerParams.measures = measures;
this.rendererParams.stacked = viewInfo.arch.attrs.stacked !== "False";
- this.loadParams.initialMode = viewInfo.arch.attrs.type || 'bar';
- this.loadParams.initialMeasure = initialMeasure;
- this.loadParams.initialGroupBys = initialGroupBys;
+ this.loadParams.mode = params.context.graph_mode || viewInfo.arch.attrs.type || 'bar';
+ this.loadParams.measure = params.context.graph_measure || measure || '__count__';
+ this.loadParams.groupBys = params.context.graph_groupbys || groupBys || [];
this.loadParams.fields = viewInfo.fields;
},
});
diff --git a/addons/web/static/tests/views/graph_tests.js b/addons/web/static/tests/views/graph_tests.js
index df6e500d181..7a7d22d96d6 100644
--- a/addons/web/static/tests/views/graph_tests.js
+++ b/addons/web/static/tests/views/graph_tests.js
@@ -304,6 +304,46 @@ QUnit.module('Views', {
done();
});
});
+
+ QUnit.test('correctly uses graph_ keys from the context', function (assert) {
+ var done = assert.async();
+ assert.expect(6);
+
+ var graph = createView({
+ View: GraphView,
+ model: "foo",
+ data: this.data,
+ arch: '<graph><field name="product_id"/></graph>',
+ viewOptions: {
+ context: {
+ graph_measure: 'foo',
+ graph_mode: 'line',
+ graph_groupbys: ['color_id'],
+ },
+ },
+ });
+ return concurrency.delay(0).then(function () {
+ // check measure
+ assert.strictEqual(graph.$('text.nv-legend-text:contains(Foo)').length, 1,
+ "should now use the 'foo' measure");
+
+ // check mode
+ assert.strictEqual(graph.renderer.state.mode, "line", "should be in line chart mode");
+ assert.notOk(graph.$buttons.find('button[data-mode="bar"]').hasClass('active'),
+ 'bar chart button should not be active');
+ assert.ok(graph.$buttons.find('button[data-mode="line"]').hasClass('active'),
+ 'line chart button should be active');
+
+ // check groupbys
+ assert.strictEqual(graph.$('text:contains(xphone)').length, 0,
+ "should not contain a text element with product in legend");
+ assert.strictEqual(graph.$('text:contains(red)').length, 1,
+ "should contain a text element with color in legend");
+
+ graph.destroy();
+ done();
+ });
+ });
});
});