2013-11-22 07:43:27 +00:00
|
|
|
|
|
|
|
var PivotTable = openerp.web.Class.extend({
|
|
|
|
init: function (options) {
|
2013-11-22 15:10:25 +00:00
|
|
|
this.rows = [];
|
|
|
|
this.cols = [];
|
2013-11-22 07:43:27 +00:00
|
|
|
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;
|
2013-11-22 15:10:25 +00:00
|
|
|
this.id_seed = 0;
|
2013-11-22 07:43:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// 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;
|
2013-11-22 15:10:25 +00:00
|
|
|
this.rows.push({
|
2013-11-22 07:43:27 +00:00
|
|
|
id: this.generate_id(),
|
|
|
|
path: [],
|
|
|
|
name: "Total",
|
|
|
|
is_expanded: false,
|
|
|
|
parent: null,
|
|
|
|
children: [],
|
|
|
|
domain: this.domain,
|
2013-11-22 15:10:25 +00:00
|
|
|
});
|
2013-11-22 07:43:27 +00:00
|
|
|
|
2013-11-22 15:10:25 +00:00
|
|
|
this.cols.push({
|
2013-11-22 07:43:27 +00:00
|
|
|
id: this.generate_id(),
|
|
|
|
path: [],
|
|
|
|
name: this.measure_label,
|
|
|
|
is_expanded: false,
|
|
|
|
parent: null,
|
|
|
|
children: [],
|
|
|
|
domain: this.domain,
|
2013-11-22 15:10:25 +00:00
|
|
|
});
|
2013-11-22 07:43:27 +00:00
|
|
|
|
|
|
|
// 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];
|
2013-11-22 15:10:25 +00:00
|
|
|
self.set_value(self.rows[0].id, self.cols[0].id, val);
|
2013-11-22 07:43:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var grp = query_groups (this.model, this.visible_fields(), this.domain, this.row_groupby)
|
|
|
|
.then(function (groups) {
|
|
|
|
_.each(groups, function (group) {
|
2013-11-22 15:10:25 +00:00
|
|
|
var new_id = self.generate_id(),
|
|
|
|
new_row = {
|
2013-11-22 09:43:22 +00:00
|
|
|
id: new_id,
|
2013-11-22 07:43:27 +00:00
|
|
|
path: [group.attributes.value[1]],
|
|
|
|
name: group.attributes.value[1],
|
|
|
|
is_expanded: false,
|
2013-11-22 15:10:25 +00:00
|
|
|
parent: self.rows[0],
|
2013-11-22 07:43:27 +00:00
|
|
|
children: [],
|
|
|
|
domain: group.model._domain,
|
2013-11-22 15:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.rows[0].children.push(new_row);
|
|
|
|
self.rows.push(new_row);
|
|
|
|
self.set_value(new_id, self.cols[0].id,
|
2013-11-22 07:43:27 +00:00
|
|
|
group.attributes.aggregates[self.measure]);
|
|
|
|
});
|
2013-11-22 15:10:25 +00:00
|
|
|
self.rows[0].is_expanded = true;
|
2013-11-22 07:43:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return $.when(tot, grp);
|
|
|
|
},
|
|
|
|
|
|
|
|
generate_id: function () {
|
|
|
|
this.id_seed += 1;
|
|
|
|
return this.id_seed;
|
|
|
|
},
|
|
|
|
|
|
|
|
visible_fields: function () {
|
|
|
|
return this.row_groupby.concat(this.col_groupby, this.measure);
|
|
|
|
},
|
|
|
|
|
|
|
|
set_value: function (row, col, value) {
|
|
|
|
var cell = _.find(this.cells, function (c) {
|
|
|
|
return ((c.row_id == row) && (c.col_id == col));
|
|
|
|
});
|
|
|
|
if (cell) {
|
|
|
|
cell.value = value;
|
|
|
|
} else {
|
|
|
|
this.cells.push({row_id: row, col_id: col, value: value});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get_value: function (row, col) {
|
|
|
|
var cell = _.find(this.cells, function (c) {
|
|
|
|
return ((c.row_id == row) && (c.col_id == col));
|
|
|
|
});
|
|
|
|
return (cell) ? cell.value : '';
|
2013-11-22 09:43:22 +00:00
|
|
|
},
|
|
|
|
|
2013-11-22 13:59:27 +00:00
|
|
|
get_col: function (id) {
|
2013-11-22 15:10:25 +00:00
|
|
|
return _.find(this.cols, function (col) { return col.id == id;});
|
2013-11-22 13:59:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get_row: function (id) {
|
2013-11-22 15:10:25 +00:00
|
|
|
return _.find(this.rows, function (row) { return row.id == id;});
|
2013-11-22 13:59:27 +00:00
|
|
|
},
|
|
|
|
|
2013-11-22 15:10:25 +00:00
|
|
|
fold_row: function (row) {
|
|
|
|
var list = [];
|
|
|
|
function tree_traversal(tree) {
|
|
|
|
list.push(tree);
|
|
|
|
_.each(tree.children, tree_traversal);
|
|
|
|
}
|
|
|
|
tree_traversal(row);
|
|
|
|
this.rows = _.difference(this.rows, _.rest(list));
|
|
|
|
row.is_expanded = false;
|
|
|
|
var fold_lvls = _.map(this.rows, function(g) {return g.path.length;});
|
2013-11-22 13:59:27 +00:00
|
|
|
var new_groupby_length = _.max(fold_lvls);
|
|
|
|
this.row_groupby.splice(new_groupby_length);
|
2013-11-22 15:10:25 +00:00
|
|
|
row.children = [];
|
2013-11-22 13:59:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fold_col: function (col) {
|
2013-11-22 15:10:25 +00:00
|
|
|
var list = [];
|
|
|
|
function tree_traversal(tree) {
|
|
|
|
list.push(tree);
|
|
|
|
_.each(tree.children, tree_traversal);
|
|
|
|
}
|
|
|
|
tree_traversal(col);
|
|
|
|
this.cols = _.difference(this.cols, _.rest(list));
|
|
|
|
col.is_expanded = false;
|
|
|
|
var fold_lvls = _.map(this.cols, function(g) {return g.path.length;});
|
|
|
|
var new_groupby_length = _.max(fold_lvls);
|
|
|
|
this.col_groupby.splice(new_groupby_length);
|
|
|
|
col.children = [];
|
2013-11-22 13:59:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
expand_row: function (row_id, field_id) {
|
|
|
|
var self = this,
|
|
|
|
row = this.get_row(row_id);
|
|
|
|
|
|
|
|
if (row.path.length == this.row_groupby.length) {
|
|
|
|
this.row_groupby.push(field_id);
|
|
|
|
}
|
|
|
|
return query_groups_data(this.model, this.visible_fields(), row.domain, this.col_groupby, field_id)
|
|
|
|
.then(function (groups) {
|
|
|
|
_.each(groups, function (group) {
|
2013-11-22 15:10:25 +00:00
|
|
|
var new_row_id = self.make_row(group, row);
|
2013-11-22 13:59:27 +00:00
|
|
|
_.each(group, function (data) {
|
2013-11-22 15:10:25 +00:00
|
|
|
var col = _.find(self.cols, function (c) {
|
2013-11-22 13:59:27 +00:00
|
|
|
return _.isEqual(_.rest(data.path), c.path);
|
|
|
|
});
|
|
|
|
if (col) {
|
|
|
|
self.set_value(new_row_id, col.id, data.attributes.aggregates[self.measure]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
row.is_expanded = true;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-11-22 15:10:25 +00:00
|
|
|
make_row: function (groups, parent) {
|
|
|
|
var new_row = {
|
|
|
|
id: this.generate_id(),
|
|
|
|
path: parent.path.concat(groups[0].attributes.value[1]),
|
|
|
|
name: groups[0].attributes.value[1],
|
|
|
|
is_expanded: false,
|
|
|
|
parent: parent.id,
|
|
|
|
children: [],
|
|
|
|
domain: groups[0].model._domain,
|
|
|
|
};
|
|
|
|
parent.children.push(new_row);
|
|
|
|
insertAfter(this.rows, parent, new_row);
|
|
|
|
return new_row.id;
|
|
|
|
},
|
|
|
|
|
2013-11-22 13:59:27 +00:00
|
|
|
expand_col: function (col_id, field_id) {
|
|
|
|
var self = this,
|
|
|
|
col = this.get_col(col_id);
|
|
|
|
|
|
|
|
if (col.path.length == this.col_groupby.length) {
|
|
|
|
this.col_groupby.push(field_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return query_groups_data(this.model, this.visible_fields(), col.domain, this.row_groupby, field_id)
|
|
|
|
.then(function (groups) {
|
|
|
|
_.each(groups, function (group) {
|
2013-11-22 15:10:25 +00:00
|
|
|
var new_col_id = self.make_col(group, col);
|
|
|
|
_.each(group, function (data) {
|
|
|
|
var row = _.find(self.rows, function (c) {
|
|
|
|
return _.isEqual(data.path.slice(1), c.path);
|
|
|
|
});
|
|
|
|
if (row) {
|
|
|
|
self.set_value(row.id, new_col_id, data.attributes.aggregates[self.measure]);
|
|
|
|
}
|
|
|
|
});
|
2013-11-22 13:59:27 +00:00
|
|
|
});
|
2013-11-22 15:10:25 +00:00
|
|
|
col.is_expanded = true;
|
2013-11-22 13:59:27 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-11-22 15:10:25 +00:00
|
|
|
make_col: function (groups, parent) {
|
|
|
|
var new_col = {
|
2013-11-22 13:59:27 +00:00
|
|
|
id: this.generate_id(),
|
|
|
|
path: parent.path.concat(groups[0].attributes.value[1]),
|
|
|
|
name: groups[0].attributes.value[1],
|
|
|
|
is_expanded: false,
|
|
|
|
parent: parent.id,
|
|
|
|
children: [],
|
|
|
|
domain: groups[0].model._domain,
|
|
|
|
};
|
2013-11-22 15:10:25 +00:00
|
|
|
parent.children.push(new_col);
|
|
|
|
insertAfter(this.cols, parent, new_col);
|
|
|
|
return new_col.id;
|
2013-11-22 13:59:27 +00:00
|
|
|
},
|
|
|
|
|
2013-11-22 16:04:49 +00:00
|
|
|
get_chart_data: function () {
|
|
|
|
var self = this;
|
|
|
|
var values = _.map(this.rows[0].children, function (pt) {
|
|
|
|
var val = self.get_value(pt.id, 2);
|
|
|
|
return {x: pt.name, y: val};
|
|
|
|
});
|
|
|
|
return [{key: 'Bar chart', values: values}];
|
|
|
|
|
|
|
|
},
|
2013-11-22 07:43:27 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|