2 var PivotTable = openerp.web.Class.extend({
3 init: function (options) {
7 this.row_groupby = options.row_groupby;
9 this.model = options.model;
10 this.measure = options.measure;
11 this.measure_label = options.measure_label;
12 this.domain = options.domain;
16 // Load initial data into the rows, cols and cells array.
17 // This function needs to be called after init and before
18 // drawing the table (otherwise the data returned will be empty...)
22 id: this.generate_id(),
32 id: this.generate_id(),
34 name: this.measure_label,
41 // get total and create first cell
42 var tot = query_groups (this.model, this.measure, this.domain, [])
43 .then(function (total) {
44 var val = total[0].attributes.aggregates[self.measure];
45 self.set_value(self.rows[0].id, self.cols[0].id, val);
48 var grp = query_groups (this.model, this.visible_fields(), this.domain, this.row_groupby)
49 .then(function (groups) {
50 _.each(groups, function (group) {
51 var new_id = self.generate_id(),
54 path: [group.attributes.value[1]],
55 name: group.attributes.value[1],
59 domain: group.model._domain,
62 self.rows[0].children.push(new_row);
63 self.rows.push(new_row);
64 self.set_value(new_id, self.cols[0].id,
65 group.attributes.aggregates[self.measure]);
67 self.rows[0].is_expanded = true;
70 return $.when(tot, grp);
73 generate_id: function () {
78 visible_fields: function () {
79 return this.row_groupby.concat(this.col_groupby, this.measure);
82 set_value: function (row, col, value) {
83 var cell = _.find(this.cells, function (c) {
84 return ((c.row_id == row) && (c.col_id == col));
89 this.cells.push({row_id: row, col_id: col, value: value});
93 get_value: function (row, col) {
94 var cell = _.find(this.cells, function (c) {
95 return ((c.row_id == row) && (c.col_id == col));
97 return (cell) ? cell.value : '';
100 get_col: function (id) {
101 return _.find(this.cols, function (col) { return col.id == id;});
104 get_row: function (id) {
105 return _.find(this.rows, function (row) { return row.id == id;});
108 fold_row: function (row) {
110 function tree_traversal(tree) {
112 _.each(tree.children, tree_traversal);
115 this.rows = _.difference(this.rows, _.rest(list));
116 row.is_expanded = false;
117 var fold_lvls = _.map(this.rows, function(g) {return g.path.length;});
118 var new_groupby_length = _.max(fold_lvls);
119 this.row_groupby.splice(new_groupby_length);
123 fold_col: function (col) {
125 function tree_traversal(tree) {
127 _.each(tree.children, tree_traversal);
130 this.cols = _.difference(this.cols, _.rest(list));
131 col.is_expanded = false;
132 var fold_lvls = _.map(this.cols, function(g) {return g.path.length;});
133 var new_groupby_length = _.max(fold_lvls);
134 this.col_groupby.splice(new_groupby_length);
138 expand_row: function (row_id, field_id) {
140 row = this.get_row(row_id);
142 if (row.path.length == this.row_groupby.length) {
143 this.row_groupby.push(field_id);
145 return query_groups_data(this.model, this.visible_fields(), row.domain, this.col_groupby, field_id)
146 .then(function (groups) {
147 _.each(groups, function (group) {
148 var new_row_id = self.make_row(group, row);
149 _.each(group, function (data) {
150 var col = _.find(self.cols, function (c) {
151 return _.isEqual(_.rest(data.path), c.path);
154 self.set_value(new_row_id, col.id, data.attributes.aggregates[self.measure]);
158 row.is_expanded = true;
162 make_row: function (groups, parent) {
164 id: this.generate_id(),
165 path: parent.path.concat(groups[0].attributes.value[1]),
166 name: groups[0].attributes.value[1],
170 domain: groups[0].model._domain,
172 parent.children.push(new_row);
173 insertAfter(this.rows, parent, new_row);
177 expand_col: function (col_id, field_id) {
179 col = this.get_col(col_id);
181 if (col.path.length == this.col_groupby.length) {
182 this.col_groupby.push(field_id);
185 return query_groups_data(this.model, this.visible_fields(), col.domain, this.row_groupby, field_id)
186 .then(function (groups) {
187 _.each(groups, function (group) {
188 var new_col_id = self.make_col(group, col);
189 _.each(group, function (data) {
190 var row = _.find(self.rows, function (c) {
191 return _.isEqual(data.path.slice(1), c.path);
194 self.set_value(row.id, new_col_id, data.attributes.aggregates[self.measure]);
198 col.is_expanded = true;
202 make_col: function (groups, parent) {
204 id: this.generate_id(),
205 path: parent.path.concat(groups[0].attributes.value[1]),
206 name: groups[0].attributes.value[1],
210 domain: groups[0].model._domain,
212 parent.children.push(new_col);
213 insertAfter(this.cols, parent, new_col);
217 get_chart_data: function () {
219 var values = _.map(this.rows[0].children, function (pt) {
220 var val = self.get_value(pt.id, 2);
221 return {x: pt.name, y: val};
223 return [{key: 'Bar chart', values: values}];