[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.
This commit is contained in:
Aaron Bohy
2017-06-01 12:02:17 +02:00
parent 033aec0826
commit 2f4d3d0e66
3 changed files with 57 additions and 23 deletions
@@ -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));
@@ -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;
},
});
@@ -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();
});
});
});
});