[FIX] web: fix expand all button in pivot view
Also took this opportunity to refactor the code of pivot to adapt the parts that were not up to date and remove the useless deferred in sortRows, courtesy of aab. The exportData function still needs reworking.
This commit is contained in:
@@ -81,15 +81,12 @@ var PivotController = AbstractController.extend({
|
||||
* @returns {Object}
|
||||
*/
|
||||
getContext: function () {
|
||||
if (this.handle) {
|
||||
var state = this.model.get(this.handle);
|
||||
return {
|
||||
pivot_measures: state.measures,
|
||||
pivot_column_groupby: state.data.main_col.groupbys,
|
||||
pivot_row_groupby: state.data.main_row.groupbys,
|
||||
};
|
||||
}
|
||||
return {};
|
||||
var state = this.model.get();
|
||||
return {
|
||||
pivot_measures: state.measures,
|
||||
pivot_column_groupby: state.data.main_col.groupbys,
|
||||
pivot_row_groupby: state.data.main_row.groupbys,
|
||||
};
|
||||
},
|
||||
/**
|
||||
* Render the buttons according to the PivotView.buttons template and
|
||||
@@ -217,13 +214,12 @@ var PivotController = AbstractController.extend({
|
||||
_onButtonClick: function (event) {
|
||||
var $target = $(event.target);
|
||||
if ($target.hasClass('o_pivot_flip_button')) {
|
||||
this.model.flip(this.handle);
|
||||
this.update(null, {reload: false});
|
||||
this.model.flip();
|
||||
this.update({}, {reload: false});
|
||||
}
|
||||
if ($target.hasClass('o_pivot_expand_button')) {
|
||||
this.model
|
||||
.expandAll(this.handle)
|
||||
.then(this.update_state.bind(this));
|
||||
this.model.expandAll();
|
||||
this.update({reload: false});
|
||||
}
|
||||
if ($target.parents('.o_pivot_measures_list').length) {
|
||||
var parent = $target.parent();
|
||||
@@ -344,9 +340,8 @@ var PivotController = AbstractController.extend({
|
||||
var col_id = $target.data('id');
|
||||
var measure = $target.data('measure');
|
||||
var isAscending = $target.hasClass('o_pivot_measure_row_sorted_asc');
|
||||
this.model
|
||||
.sortRows(this.handle, col_id, measure, isAscending)
|
||||
.then(this.update.bind(this, {}, {reload: false}));
|
||||
this.model.sortRows(col_id, measure, isAscending);
|
||||
this.update({}, {reload: false});
|
||||
},
|
||||
/**
|
||||
* This method is called when someone clicks on an open header. When that
|
||||
@@ -359,7 +354,7 @@ var PivotController = AbstractController.extend({
|
||||
event.preventDefault();
|
||||
event.stopImmediatePropagation();
|
||||
var headerID = $(event.target).data('id');
|
||||
this.model.closeHeader(headerID)
|
||||
this.model.closeHeader(headerID);
|
||||
this.update({}, {reload: false});
|
||||
},
|
||||
|
||||
|
||||
@@ -52,12 +52,10 @@ var PivotModel = AbstractModel.extend({
|
||||
header.root.groupbys.splice(newGroupbyLength);
|
||||
},
|
||||
/**
|
||||
* @param {string} id
|
||||
* @returns {Deferred}
|
||||
*/
|
||||
expandAll: function (id) {
|
||||
var record = this.localData[id];
|
||||
return this._loadData(record);
|
||||
expandAll: function () {
|
||||
return this._loadData();
|
||||
},
|
||||
/**
|
||||
* Expand (open up) a given header, be it a row or a column.
|
||||
@@ -132,7 +130,7 @@ var PivotModel = AbstractModel.extend({
|
||||
* @returns {Object}
|
||||
*/
|
||||
exportData: function () {
|
||||
var record = this.model.get(this.handle);
|
||||
var record = this.data;
|
||||
var measureNbr = record.measures.length;
|
||||
var headers = this.renderer._computeHeaders();
|
||||
var measureRow = measureNbr > 1 ? _.last(headers) : [];
|
||||
@@ -236,7 +234,7 @@ var PivotModel = AbstractModel.extend({
|
||||
measures: this.initialMeasures,
|
||||
sorted_column: {},
|
||||
};
|
||||
return this._loadData(params);
|
||||
return this._loadData();
|
||||
},
|
||||
/**
|
||||
* @override
|
||||
@@ -255,12 +253,12 @@ var PivotModel = AbstractModel.extend({
|
||||
this.data.groupedBy = params.groupBy;
|
||||
}
|
||||
if (!this.data.has_data) {
|
||||
return this._loadData(params);
|
||||
return this._loadData();
|
||||
}
|
||||
|
||||
var old_row_root = this.data.main_row.root;
|
||||
var old_col_root = this.data.main_col.root;
|
||||
return this._loadData(params).then(function () {
|
||||
return this._loadData().then(function () {
|
||||
var new_groupby_length;
|
||||
if (!('groupBy' in params)) {
|
||||
// we only update the row groupbys according to the old groupbys
|
||||
@@ -280,18 +278,13 @@ var PivotModel = AbstractModel.extend({
|
||||
* Sort the rows, depending on the values of a given column. This is an
|
||||
* in-memory sort.
|
||||
*
|
||||
* Note that it returns a deferred. @todo: makes sure this does not return a
|
||||
* deferred.
|
||||
*
|
||||
* @param {any} id
|
||||
* @param {any} col_id
|
||||
* @param {any} measure
|
||||
* @param {any} descending
|
||||
* @returns {Deferred}
|
||||
*/
|
||||
sortRows: function (id, col_id, measure, descending) {
|
||||
sortRows: function (col_id, measure, descending) {
|
||||
var cells = this.data.cells;
|
||||
this._traverseTree(this.data.main_row.root, function (header) {
|
||||
this._traverseTree(this.data.main_row.root, function (header) {
|
||||
header.children.sort(compare);
|
||||
});
|
||||
this.data.sorted_column = {
|
||||
@@ -313,7 +306,6 @@ var PivotModel = AbstractModel.extend({
|
||||
value2 = values2 ? values2[measure] : 0;
|
||||
return descending ? value1 - value2 : value2 - value1;
|
||||
}
|
||||
return $.when(id);
|
||||
},
|
||||
/**
|
||||
* Toggle the active state for a given measure, then reload the data.
|
||||
|
||||
@@ -621,6 +621,7 @@ var MockServer = Class.extend({
|
||||
|
||||
var records = this._getRecords(model, kwargs.domain);
|
||||
if (groupByField) {
|
||||
var originalGroupByField = groupByField;
|
||||
var groupByFieldDescr = fields[groupByField.split(':')[0]];
|
||||
var groupByFunction, formatValue;
|
||||
if (groupByFieldDescr.type === 'date') {
|
||||
@@ -664,12 +665,12 @@ var MockServer = Class.extend({
|
||||
id: val
|
||||
});
|
||||
if (related_record) {
|
||||
group[groupByField] = [val, related_record.display_name];
|
||||
group[originalGroupByField] = [val, related_record.display_name];
|
||||
} else {
|
||||
group[groupByField] = false;
|
||||
group[originalGroupByField] = false;
|
||||
}
|
||||
} else {
|
||||
group[groupByField] = val;
|
||||
group[originalGroupByField] = val;
|
||||
}
|
||||
|
||||
// compute count key to match dumb server logic...
|
||||
|
||||
@@ -18,6 +18,7 @@ QUnit.module('Views', {
|
||||
date: {string: "Date", type: "date", store: true},
|
||||
product_id: {string: "Product", type: "many2one", relation: 'product', store: true},
|
||||
non_stored_m2o: {string: "Non Stored M2O", type: "many2one", relation: 'product'},
|
||||
customer: {string: "Customer", type: "many2one", relation: 'customer', store: true},
|
||||
},
|
||||
records: [
|
||||
{
|
||||
@@ -26,23 +27,27 @@ QUnit.module('Views', {
|
||||
bar: true,
|
||||
date: '2016-12-14',
|
||||
product_id: 37,
|
||||
customer: 1,
|
||||
}, {
|
||||
id: 2,
|
||||
foo: 1,
|
||||
bar: true,
|
||||
date: '2016-10-26',
|
||||
product_id: 41,
|
||||
customer: 2,
|
||||
}, {
|
||||
id: 3,
|
||||
foo: 17,
|
||||
bar: true,
|
||||
date: '2016-12-15',
|
||||
product_id: 41,
|
||||
customer: 2,
|
||||
}, {id: 4,
|
||||
foo: 2,
|
||||
bar: false,
|
||||
date: '2016-04-11',
|
||||
product_id: 41,
|
||||
customer: 1,
|
||||
},
|
||||
]
|
||||
},
|
||||
@@ -58,6 +63,18 @@ QUnit.module('Views', {
|
||||
display_name: "xpad",
|
||||
}]
|
||||
},
|
||||
customer: {
|
||||
fields: {
|
||||
name: {string: "Customer Name", type: "char"}
|
||||
},
|
||||
records: [{
|
||||
id: 1,
|
||||
display_name: "First",
|
||||
}, {
|
||||
id: 2,
|
||||
display_name: "Second",
|
||||
}]
|
||||
},
|
||||
};
|
||||
}
|
||||
}, function () {
|
||||
@@ -533,4 +550,45 @@ QUnit.module('Views', {
|
||||
pivot.destroy();
|
||||
});
|
||||
|
||||
QUnit.test('can expand all rows', function (assert) {
|
||||
// FIXME: This test depends on an incomplete behavior of the mock server
|
||||
// and will need to be updated when read_group can group on
|
||||
// multiple field levels.
|
||||
assert.expect(4);
|
||||
|
||||
var pivot = createView({
|
||||
View: PivotView,
|
||||
model: "partner",
|
||||
data: this.data,
|
||||
arch: '<pivot>' +
|
||||
'<field name="foo" type="measure"/>' +
|
||||
'<field name="product_id" type="row"/>' +
|
||||
'</pivot>',
|
||||
});
|
||||
|
||||
assert.strictEqual(pivot.$('td.o_pivot_cell_value').text(), "321220",
|
||||
"should have proper values in cells (total, result 1, result 2");
|
||||
|
||||
// expand on date:days, product
|
||||
pivot.update({groupBy: ['date:days', 'product']});
|
||||
|
||||
assert.strictEqual(pivot.$('tbody tr').length, 7,
|
||||
"should have 7 rows (total + 2 for each category)");
|
||||
|
||||
// collapse the last two rows
|
||||
pivot.$('.o_pivot_header_cell_opened').last().click();
|
||||
pivot.$('.o_pivot_header_cell_opened').last().click();
|
||||
|
||||
assert.strictEqual(pivot.$('tbody tr').length, 5,
|
||||
"should have 5 rows now");
|
||||
|
||||
// expand all
|
||||
$('.o_pivot_expand_button').click();
|
||||
|
||||
assert.strictEqual(pivot.$('tbody tr').length, 7,
|
||||
"should have 7 rows again");
|
||||
|
||||
pivot.destroy();
|
||||
});
|
||||
|
||||
});});
|
||||
|
||||
Reference in New Issue
Block a user