[REF] slight modifications to the underlying data structure of pivot table in addon web_graph

bzr revid: ged@openerp.com-20131123114201-g6v6q9r15n9gdzg2
This commit is contained in:
Gery Debongnie 2013-11-23 12:42:01 +01:00
parent 0f995a25d5
commit 42e38292b8
3 changed files with 70 additions and 69 deletions

View File

@ -1,6 +1,6 @@
function format_chart_data (pivot) {
var values = _.map(pivot.rows[0].children.reverse(), function (pt) {
var values = _.map(pivot.rows.headers[0].children.reverse(), function (pt) {
var val = pivot.get_value(pt.id, 2);
return {x: pt.name, y: val};
});

View File

@ -186,9 +186,9 @@ instance.web_graph.GraphView = instance.web.View.extend({
pivot['fold_' + options.type](header);
this.draw_table();
} else {
if (header.path.length < pivot[options.type + '_groupby'].length) {
if (header.path.length < pivot[options.type + 's'].groupby.length) {
// expand the corresponding header
var field = pivot[options.type + '_groupby'][header.path.length];
var field = pivot[options.type + 's'].groupby[header.path.length];
pivot['expand_' + options.type](id, field)
.then(this.proxy('draw_table'));
} else {
@ -205,7 +205,7 @@ instance.web_graph.GraphView = instance.web.View.extend({
display_dropdown: function (options) {
var self = this,
pivot = this.pivot_table,
already_grouped = pivot.row_groupby.concat(pivot.col_groupby),
already_grouped = pivot.rows.groupby.concat(pivot.cols.groupby),
possible_groups = _.difference(self.data.important_fields, already_grouped),
dropdown_options = {
fields: _.map(possible_groups, function (field) {
@ -224,7 +224,7 @@ instance.web_graph.GraphView = instance.web.View.extend({
draw_table: function () {
this.table.empty();
this.draw_top_headers();
_.each(this.pivot_table.rows, this.proxy('draw_row'));
_.each(this.pivot_table.rows.headers, this.proxy('draw_row'));
},
make_border_cell: function (colspan, rowspan) {
@ -244,7 +244,7 @@ instance.web_graph.GraphView = instance.web.View.extend({
draw_top_headers: function () {
var self = this,
pivot = this.pivot_table,
height = _.max(_.map(pivot.cols, function(g) {return g.path.length;})),
height = _.max(_.map(pivot.cols.headers, function(g) {return g.path.length;})),
header_cells = [[this.make_border_cell(1, height)]];
function set_dim (cols) {
@ -277,11 +277,11 @@ instance.web_graph.GraphView = instance.web.View.extend({
}
}
set_dim(pivot.cols[0]); // add width and height info to columns headers
if (pivot.cols[0].children.length === 0) {
make_cells(pivot.cols, 0);
set_dim(pivot.cols.main); // add width and height info to columns headers
if (pivot.cols.main.children.length === 0) {
make_cells(pivot.cols.headers, 0);
} else {
make_cells(pivot.cols[0].children, 1);
make_cells(pivot.cols.main.children, 1);
}
_.each(header_cells, function (cells) {
@ -302,7 +302,7 @@ instance.web_graph.GraphView = instance.web.View.extend({
html_row.append(row_header);
_.each(pivot.cols, function (col) {
_.each(pivot.cols.headers, function (col) {
if (col.children.length === 0) {
var cell = $('<td></td>').append(pivot.get_value(row.id, col.id));
html_row.append(cell);

View File

@ -1,53 +1,58 @@
var PivotTable = openerp.web.Class.extend({
init: function (options) {
this.rows = [];
this.cols = [];
this.cells = [];
this.row_groupby = options.row_groupby;
this.col_groupby = [];
this.model = options.model;
this.measure = options.measure;
this.measure_label = options.measure_label;
this.domain = options.domain;
this.id_seed = 0;
},
id_seed: 0,
// Load initial data into the rows, cols and cells array.
// This function needs to be called after init and before
// drawing the table (otherwise the data returned will be empty...)
start: function () {
var self = this;
this.rows.push({
init: function (options) {
main_row = {
id: this.generate_id(),
path: [],
name: "Total",
is_expanded: false,
parent: null,
children: [],
domain: this.domain,
});
this.cols.push({
domain: options.domain,
};
main_col = {
id: this.generate_id(),
path: [],
name: this.measure_label,
name: options.measure_label,
is_expanded: false,
parent: null,
children: [],
domain: this.domain,
});
domain: options.domain,
};
// get total and create first cell
var tot = query_groups (this.model, this.measure, this.domain, [])
.then(function (total) {
var val = total[0].attributes.aggregates[self.measure];
self.set_value(self.rows[0].id, self.cols[0].id, val);
});
this.rows = {
groupby: options.row_groupby,
main: main_row,
headers: [main_row],
};
this.cols = {
groupby: [],
main: main_col,
headers: [main_col],
};
var initial_group = this.expand_row(this.rows[0].id, this.row_groupby[0]);
this.cells = [];
this.model = options.model;
this.domain = options.domain;
this.measure = options.measure;
this.measure_label = options.measure_label;
},
return $.when(tot, initial_group);
// Load initial data into the rows, cols and cells array.
// This function needs to be called after init and before
// drawing the table (otherwise the data returned will be empty...)
start: function () {
var self = this,
initial_group = this.expand_row(this.rows.main.id, this.rows.groupby[0]),
total = query_groups(this.model, this.measure, this.domain, [])
.then(function (total) {
var val = total[0].attributes.aggregates[self.measure];
self.set_value(self.rows.main.id, self.cols.main.id, val);
});
return $.when(total, initial_group);
},
generate_id: function () {
@ -56,7 +61,7 @@ var PivotTable = openerp.web.Class.extend({
},
visible_fields: function () {
return this.row_groupby.concat(this.col_groupby, this.measure);
return this.rows.groupby.concat(this.cols.groupby, this.measure);
},
set_value: function (row, col, value) {
@ -78,11 +83,11 @@ var PivotTable = openerp.web.Class.extend({
},
get_col: function (id) {
return _.find(this.cols, function (col) { return col.id == id;});
return _.find(this.cols.headers, function (col) { return col.id == id;});
},
get_row: function (id) {
return _.find(this.rows, function (row) { return row.id == id;});
return _.find(this.rows.headers, function (row) { return row.id == id;});
},
fold_row: function (row) {
@ -92,11 +97,11 @@ var PivotTable = openerp.web.Class.extend({
_.each(tree.children, tree_traversal);
}
tree_traversal(row);
this.rows = _.difference(this.rows, _.rest(list));
this.rows.headers = _.difference(this.rows.headers, _.rest(list));
row.is_expanded = false;
var fold_lvls = _.map(this.rows, function(g) {return g.path.length;});
var fold_lvls = _.map(this.rows.headers, function(g) {return g.path.length;});
var new_groupby_length = _.max(fold_lvls);
this.row_groupby.splice(new_groupby_length);
this.rows.groupby.splice(new_groupby_length);
row.children = [];
},
@ -107,11 +112,11 @@ var PivotTable = openerp.web.Class.extend({
_.each(tree.children, tree_traversal);
}
tree_traversal(col);
this.cols = _.difference(this.cols, _.rest(list));
this.cols.headers = _.difference(this.cols.headers, _.rest(list));
col.is_expanded = false;
var fold_lvls = _.map(this.cols, function(g) {return g.path.length;});
var fold_lvls = _.map(this.cols.headers, function(g) {return g.path.length;});
var new_groupby_length = _.max(fold_lvls);
this.col_groupby.splice(new_groupby_length);
this.cols.groupby.splice(new_groupby_length);
col.children = [];
},
@ -119,15 +124,15 @@ var PivotTable = openerp.web.Class.extend({
var self = this,
row = this.get_row(row_id);
if (row.path.length == this.row_groupby.length) {
this.row_groupby.push(field_id);
if (row.path.length == this.rows.groupby.length) {
this.rows.groupby.push(field_id);
}
return query_groups_data(this.model, this.visible_fields(), row.domain, this.col_groupby, field_id)
return query_groups_data(this.model, this.visible_fields(), row.domain, this.cols.groupby, field_id)
.then(function (groups) {
_.each(groups.reverse(), function (group) {
var new_row_id = self.make_header(group, row, self.rows);
var new_row_id = self.make_header(group, row, self.rows.headers);
_.each(group, function (data) {
var col = _.find(self.cols, function (c) {
var col = _.find(self.cols.headers, function (c) {
return _.isEqual(_.rest(data.path), c.path);
});
if (col) {
@ -158,16 +163,16 @@ var PivotTable = openerp.web.Class.extend({
var self = this,
col = this.get_col(col_id);
if (col.path.length == this.col_groupby.length) {
this.col_groupby.push(field_id);
if (col.path.length == this.cols.groupby.length) {
this.cols.groupby.push(field_id);
}
return query_groups_data(this.model, this.visible_fields(), col.domain, this.row_groupby, field_id)
return query_groups_data(this.model, this.visible_fields(), col.domain, this.rows.groupby, field_id)
.then(function (groups) {
_.each(groups, function (group) {
var new_col_id = self.make_header(group, col, self.cols);
var new_col_id = self.make_header(group, col, self.cols.headers);
_.each(group, function (data) {
var row = _.find(self.rows, function (c) {
var row = _.find(self.rows.headers, function (c) {
return _.isEqual(data.path.slice(1), c.path);
});
if (row) {
@ -184,10 +189,6 @@ var PivotTable = openerp.web.Class.extend({
this.rows = this.cols;
this.cols = temp;
temp = this.row_groupby;
this.row_groupby = this.col_groupby;
this.col_groupby = temp;
_.each(this.cells, function (cell) {
temp = cell.row_id;
cell.row_id = cell.col_id;
@ -196,8 +197,8 @@ var PivotTable = openerp.web.Class.extend({
},
clear_all: function () {
this.fold_row(this.rows[0]);
this.fold_col(this.cols[0]);
this.fold_row(this.rows.main);
this.fold_col(this.cols.main);
}
});