[REF] large refactoring in progress. goal is to separate the data handling code (pivot) from the ui code (drawing, buttons, events (addon web_graph)

bzr revid: ged@openerp.com-20131122151025-8npxh5g5y2ema4tq
This commit is contained in:
Gery Debongnie 2013-11-22 16:10:25 +01:00
parent 2c5fb3b2a2
commit 200762944a
2 changed files with 100 additions and 233 deletions

View File

@ -197,9 +197,10 @@ instance.web_graph.GraphView = instance.web.View.extend({
},
draw_table: function () {
console.log("cols",this.pivot_table.cols);
this.table.empty();
this.draw_top_headers();
_.each(this.pivot_table.rows_array(), this.proxy('draw_row'));
_.each(this.pivot_table.rows, this.proxy('draw_row'));
},
make_border_cell: function (colspan, rowspan) {
@ -219,7 +220,7 @@ instance.web_graph.GraphView = instance.web.View.extend({
draw_top_headers: function () {
var self = this,
pivot = this.pivot_table,
height = pivot.get_max_path_length(pivot.cols),
height = _.max(_.map(pivot.cols, function(g) {return g.path.length;})),
header_cells = [[this.make_border_cell(1, height)]];
function set_dim (cols) {
@ -239,8 +240,8 @@ instance.web_graph.GraphView = instance.web.View.extend({
}
function make_cells (queue, level) {
var col = queue.shift();
queue = queue.concat(col.children);
var col = queue[0];
queue = _.rest(queue).concat(col.children);
if (col.path.length == level) {
_.last(header_cells).push(make_col_header(col));
} else {
@ -252,12 +253,11 @@ instance.web_graph.GraphView = instance.web.View.extend({
}
}
set_dim(pivot.cols); // add width and height info to columns headers
if (pivot.cols.children.length === 0) {
make_cells([pivot.cols], 0);
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);
} else {
make_cells(pivot.cols.children, 1);
make_cells(pivot.cols[0].children, 1);
}
_.each(header_cells, function (cells) {
@ -278,9 +278,11 @@ instance.web_graph.GraphView = instance.web.View.extend({
html_row.append(row_header);
_.each(pivot.cols_array(), function (col) {
var cell = $('<td></td>').append(pivot.get_value(row.id, col.id));
html_row.append(cell)
_.each(pivot.cols, function (col) {
if (col.children.length === 0) {
var cell = $('<td></td>').append(pivot.get_value(row.id, col.id));
html_row.append(cell);
}
});
this.table.append(html_row);
}

View File

@ -2,8 +2,8 @@
var PivotTable = openerp.web.Class.extend({
init: function (options) {
this.rows = {};
this.cols = {};
this.rows = [];
this.cols = [];
this.cells = [];
this.row_groupby = options.row_groupby;
this.col_groupby = [];
@ -11,8 +11,7 @@ var PivotTable = openerp.web.Class.extend({
this.measure = options.measure;
this.measure_label = options.measure_label;
this.domain = options.domain;
this.id_seed = -1;
this.id_seed = 0;
},
// Load initial data into the rows, cols and cells array.
@ -20,7 +19,7 @@ var PivotTable = openerp.web.Class.extend({
// drawing the table (otherwise the data returned will be empty...)
start: function () {
var self = this;
this.rows = {
this.rows.push({
id: this.generate_id(),
path: [],
name: "Total",
@ -28,9 +27,9 @@ var PivotTable = openerp.web.Class.extend({
parent: null,
children: [],
domain: this.domain,
};
});
this.cols = {
this.cols.push({
id: this.generate_id(),
path: [],
name: this.measure_label,
@ -38,32 +37,35 @@ var PivotTable = openerp.web.Class.extend({
parent: null,
children: [],
domain: this.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.id, self.cols.id, val);
self.set_value(self.rows[0].id, self.cols[0].id, val);
});
var grp = query_groups (this.model, this.visible_fields(), this.domain, this.row_groupby)
.then(function (groups) {
_.each(groups, function (group) {
var new_id = self.generate_id();
self.rows.children.push({
var new_id = self.generate_id(),
new_row = {
id: new_id,
path: [group.attributes.value[1]],
name: group.attributes.value[1],
is_expanded: false,
parent: self.rows,
parent: self.rows[0],
children: [],
domain: group.model._domain,
});
self.set_value(new_id, self.cols.id,
};
self.rows[0].children.push(new_row);
self.rows.push(new_row);
self.set_value(new_id, self.cols[0].id,
group.attributes.aggregates[self.measure]);
});
self.rows.is_expanded = true;
self.rows[0].is_expanded = true;
});
return $.when(tot, grp);
@ -96,67 +98,42 @@ var PivotTable = openerp.web.Class.extend({
return (cell) ? cell.value : '';
},
iterate: function (header, iterator) {
var self = this;
iterator(header);
_.each(header.children, function (child) {
self.iterate(child, iterator);
});
},
iterate_rows: function (iterator) {
this.iterate(this.rows, iterator);
},
iterate_cols: function (iterator) {
this.iterate(this.cols, iterator);
},
get_max_path_length: function (header) {
var height = 0;
this.iterate_cols(function (col) {
height = Math.max(height, col.path.length);
});
return height;
},
rows_array: function () {
var result = [];
this.iterate_rows(function (row) { result.push(row); });
return result;
},
cols_array: function () {
var result = [];
this.iterate_cols(function (col) { result.push(col); });
return result;
},
get_col: function (id) {
return _.find(this.cols_array(), function (col) { return col.id == id;});
return _.find(this.cols, function (col) { return col.id == id;});
},
get_row: function (id) {
return _.find(this.rows_array(), function (row) { return row.id == id;});
},
fold: function (header) {
header.children = [];
header.is_expanded = false;
var fold_lvls = _.map(this.rows_array(), function(g) {return g.path.length;});
var new_groupby_length = _.max(fold_lvls);
this.row_groupby.splice(new_groupby_length);
// to do : remove every corresponding cells
return _.find(this.rows, function (row) { return row.id == id;});
},
fold_row: function (row) {
this.fold(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;});
var new_groupby_length = _.max(fold_lvls);
this.row_groupby.splice(new_groupby_length);
row.children = [];
},
fold_col: function (col) {
this.fold(col);
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 = [];
},
expand_row: function (row_id, field_id) {
@ -169,10 +146,9 @@ var PivotTable = openerp.web.Class.extend({
return query_groups_data(this.model, this.visible_fields(), row.domain, this.col_groupby, field_id)
.then(function (groups) {
_.each(groups, function (group) {
var new_row_id = self.make_header(group, row);
var cols_array = self.cols_array();
var new_row_id = self.make_row(group, row);
_.each(group, function (data) {
var col = _.find(cols_array, function (c) {
var col = _.find(self.cols, function (c) {
return _.isEqual(_.rest(data.path), c.path);
});
if (col) {
@ -184,42 +160,8 @@ var PivotTable = openerp.web.Class.extend({
});
},
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);
}
console.log("cols",this.cols_array());
console.log("dbg",this.col_groupby);
return query_groups_data(this.model, this.visible_fields(), col.domain, this.row_groupby, field_id)
.then(function (groups) {
console.log("groups",groups);
_.each(groups, function (group) {
// console.log("adding group",group);
var new_col_id = self.make_header(group, col);
// // var rows_array = self.rows_array();
// // _.each(group, function (data) {
// // var row = _.find(rows_array, 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]);
// // }
// // });
});
// col.is_expanded = true;
// // console.log("col is now",col);
// // console.log("dbg",col.children.length);
});
},
make_header: function (groups, parent) {
var new_header = {
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],
@ -228,129 +170,52 @@ var PivotTable = openerp.web.Class.extend({
children: [],
domain: groups[0].model._domain,
};
parent.children.push(new_header);
return new_header.id;
parent.children.push(new_row);
insertAfter(this.rows, parent, new_row);
return new_row.id;
},
// make_row: function (groups, parent_id) {
// var self = this,
// path,
// value,
// expanded,
// domain,
// parent,
// has_parent = (parent_id !== undefined),
// row_id = this.generate_id();
expand_col: function (col_id, field_id) {
var self = this,
col = this.get_col(col_id);
// if (has_parent) {
// parent = this.get_row(parent_id);
// path = parent.path.concat(groups[0].attributes.value[1]);
// value = groups[0].attributes.value[1];
// expanded = false;
// parent.children.push(row_id);
// domain = groups[0].model._domain;
// } else {
// parent = null;
// path = [];
// value = 'Total';
// expanded = true;
// domain = this.data.domain;
// }
if (col.path.length == this.col_groupby.length) {
this.col_groupby.push(field_id);
}
// var jquery_row = $('<tr></tr>');
return query_groups_data(this.model, this.visible_fields(), col.domain, this.row_groupby, field_id)
.then(function (groups) {
_.each(groups, function (group) {
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]);
}
});
});
col.is_expanded = true;
});
// var header = self.make_cell(value, {is_border:true, indent: path.length, foldable:true, row_id: row_id});
// jquery_row.append(header);
},
// var cell;
make_col: function (groups, parent) {
var new_col = {
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_col);
insertAfter(this.cols, parent, new_col);
return new_col.id;
},
// _.each(this.cols, function (col) {
// var element = _.find(groups, function (group) {
// return _.isEqual(_.rest(group.path), col.path);
// });
// if (element === undefined) {
// cell = self.make_cell('');
// } else {
// cell = self.make_cell(element.attributes.aggregates[self.data.measure]);
// }
// if (col.expanded) {
// cell.css('display', 'none');
// }
// col.cells.push({td:cell, row_id:row_id});
// jquery_row.append(cell);
// });
// if (!has_parent) {
// header.find('.icon-plus-sign')
// .removeClass('icon-plus-sign')
// .addClass('icon-minus-sign');
// }
// var row = {
// id: row_id,
// path: path,
// value: value,
// expanded: expanded,
// parent: parent_id,
// children: [],
// html: jquery_row,
// domain: domain,
// };
// this.rows.push(row); // to do, insert it properly, after all childs of parent
// return row;
// },
// expand_row: function (row_id, field_id) {
// var self = this;
// var row = this.get_row(row_id);
// if (row.path.length == this.data.row_groupby.length) {
// this.data.row_groupby.push(field_id);
// }
// row.expanded = true;
// row.html.find('.icon-plus-sign')
// .removeClass('icon-plus-sign')
// .addClass('icon-minus-sign');
// var visible_fields = this.data.row_groupby.concat(this.data.col_groupby, this.data.measure);
// query_groups_data(this.data.model, visible_fields, row.domain, this.data.col_groupby, field_id)
// .then(function (groups) {
// _.each(groups.reverse(), function (group) {
// var new_row = self.make_row(group, row_id);
// row.html.after(new_row.html);
// });
// });
// },
// toArray: function (header) {
// return [header].concat(_.map(header.children), this.toArray(hea))
// }
});
// function expand_row (row, field) {
// }
// function expand_col (col, field) {
// }
// function fold_col (col) {
// }
// function update_values () {
// // update all cells (reload data from db)
// // return a deferred
// }
// var id_seed = -1;
// return init();
// }