[REF] refactoring: removes the 'interval' attributes in groupbys and improves searchbar integration (addon web_graph)

bzr revid: ged@openerp.com-20140122160707-39el7frwfj4pwd4d
This commit is contained in:
Gery Debongnie 2014-01-22 17:07:07 +01:00
parent a3bb723379
commit 39e0f4736f
3 changed files with 46 additions and 62 deletions

View File

@ -20,10 +20,16 @@ instance.web_graph.GraphView = instance.web.View.extend({
// Init stuff
// ----------------------------------------------------------------------
init: function(parent, dataset, view_id, options) {
var self = this;
this._super(parent, dataset, view_id, options);
this.dataset = dataset;
this.model = new instance.web.Model(dataset.model, {group_by_no_leaf: true});
this.search_view = parent.searchview;
this.col_search_field = {
get_context: function() { return { col_group_by: self.graph_widget.get_col_groupbys()};},
get_domain: function () {},
get_groupby: function () {},
};
},
view_loading: function (fields_view_get) {
@ -68,7 +74,7 @@ instance.web_graph.GraphView = instance.web.View.extend({
do_search: function (domain, context, group_by) {
var self = this,
col_group_by = this.get_groupbys_from_searchview('ColGroupBy', 'col_group_by');
col_group_by = context.col_group_by || this.get_groupbys_from_searchview('ColGroupBy', 'col_group_by');
if (!this.graph_widget) {
if (group_by.length) {
@ -95,10 +101,11 @@ instance.web_graph.GraphView = instance.web.View.extend({
var facet = this.search_view.query.findWhere({category:cat_name}),
groupby_list = facet ? facet.values.models : [];
return _.map(groupby_list, function (g) {
if (cat_name === 'GroupBy') {
return py.eval(g.attributes.value.attrs.context).group_by;
var context = g.attributes.value.attrs.context;
if (_.isString(context)) {
return py.eval(context).group_by;
} else {
return g.attributes.value.attrs.context[cat_field];
return context[cat_field];
}
});
},
@ -156,15 +163,21 @@ instance.web_graph.GraphView = instance.web.View.extend({
category:'ColGroupBy',
values: this.make_groupby_values(groupbys, 'col_group_by'),
icon:'f',
field: this.search_field
field: this.col_search_field
};
},
make_groupby_values: function (groupbys, category) {
return _.map(groupbys, function (groupby) {
var context = {};
context[category] = groupby.interval ? groupby.field + ':' + groupby.interval : groupby.field;
var value = (category === 'group_by') ? groupby.filter : {attrs:{domain: [], context: context}};
context[category] = groupby.field;
var value;
if (category === 'group_by' && groupby.type !== 'date' && groupby.type !== 'datetime') {
value = groupby.filter;
} else {
value = {attrs: {domain: [], context: context}};
}
// var value = (category === 'group_by') ? groupby.filter : {attrs:{domain: [], context: context}};
return {
label: groupby.string,
value: value
@ -172,11 +185,6 @@ instance.web_graph.GraphView = instance.web.View.extend({
});
},
search_field: {
get_context: function() {},
get_domain: function () {},
get_groupby: function () {},
},
});
};

View File

@ -119,8 +119,8 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({
var row_gbs = this.create_field_values(row_groupby),
col_gbs = this.create_field_values(col_groupby),
dom_changed = !_.isEqual(this.pivot.domain, domain),
row_gb_changed = !this.equal_groupby(row_gbs, this.pivot.rows.groupby),
col_gb_changed = !this.equal_groupby(col_gbs, this.pivot.cols.groupby),
row_gb_changed = !_.isEqual(row_gbs, this.pivot.rows.groupby),
col_gb_changed = !_.isEqual(col_gbs, this.pivot.cols.groupby),
row_reduced = is_strict_beginning_of(row_gbs, this.pivot.rows.groupby),
col_reduced = is_strict_beginning_of(col_gbs, this.pivot.cols.groupby);
@ -160,46 +160,28 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({
this.display_data();
},
// compare groupby, ignoring the 'interval' attribute of dates...
// this is necessary to avoid problems with the groupby received by the
// context which does not have the interval attribute. This is ugly
// and need to be changed at some point
equal_groupby: function (groupby1, groupby2) {
if (groupby1.length !== groupby2.length) { return false; }
for (var i = 0; i < groupby1.length; i++) {
if (!this.equal_value(groupby1[i], groupby2[i])) { return false; }
}
return true;
},
equal_value: function (val1, val2) {
return ((val1.field === val2.field) && (val1.string === val2.string) && (val1.type === val2.type));
},
create_field_value: function (f) {
var field = f.field || ((_.contains(f, ':')) ? f.split(':')[0] : f),
important_field = _.findWhere(this.important_fields, {field:field}),
string = important_field ? important_field.string : this.fields[field].string,
result = {field: field, string: string, type: this.fields[field].type };
var field = (_.contains(f, ':')) ? f.split(':')[0] : f,
groupby_field = _.findWhere(this.important_fields, {field:field}),
string = groupby_field ? groupby_field.string : this.fields[field].string,
result = {field: f, string: string, type: this.fields[field].type };
if (important_field) {
result.filter = important_field.filter;
}
if (f.interval) {
result.interval = f.interval;
}
if (_.contains(f, ':')) {
result.interval = f.split(':')[1];
if (groupby_field) {
result.filter = groupby_field.filter;
}
return result;
},
create_field_values: function (field_ids) {
var self = this;
return _.map(field_ids, function (f) { return self.create_field_value(f); });
return _.map(field_ids, this.proxy('create_field_value'));
},
get_col_groupbys: function () {
return _.pluck(this.pivot.cols.groupby, 'field');
},
// ----------------------------------------------------------------------
// UI code
// ----------------------------------------------------------------------
@ -314,25 +296,24 @@ openerp.web_graph.Graph = openerp.web.Widget.extend({
field_selection: function (event) {
var id = event.target.attributes['data-id'].nodeValue,
field_id = event.target.attributes['data-field-id'].nodeValue,
interval;
interval,
groupby = this.create_field_value(field_id);
event.preventDefault();
if (this.fields[field_id].type === 'date' || this.fields[field_id].type === 'datetime') {
interval = event.target.attributes['data-interval'].nodeValue;
this.expand(id, {field: field_id, interval: interval});
} else {
this.expand(id, field_id);
groupby.field = groupby.field + ':' + interval;
}
this.expand(id, groupby);
},
// ----------------------------------------------------------------------
// Pivot Table integration
// ----------------------------------------------------------------------
expand: function (header_id, field_id) {
expand: function (header_id, groupby) {
var self = this,
header = this.pivot.get_header(header_id),
update_groupby = !!field_id,
groupby = (update_groupby) ? this.create_field_value(field_id)
: header.root.groupby[header.path.length];
update_groupby = !!groupby,
groupby = groupby || header.root.groupby[header.path.length];
this.pivot.expand(header_id, groupby).then(function () {
if (update_groupby && self.graph_view) {

View File

@ -25,8 +25,9 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
// ----------------------------------------------------------------------
// this.measures: list of measure [measure], measure = {field: _, string: _, type: _}
// this.rows.groupby, this.cols.groupby : list of groupbys used for describing rows (...),
// a groupby is also {field:_, string:_, type:_} but it also has a interval
// attribute if its type is date/datetime.
// a groupby is also {field:_, string:_, type:_}
// If its type is date/datetime, field can have the corresponding interval in its description,
// for example 'create_date:week'.
set_measures: function (measures) {
this.measures = measures;
return this.update_data();
@ -221,7 +222,6 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
// make cells
_.each(self.get_ancestors_and_self(group), function (data) {
var values = _.map(self.measures, function (m) {
console.log('m.field', m.field);
return data.attributes.aggregates[m.field];
});
var other = _.find(otherRoot.headers, function (h) {
@ -231,8 +231,6 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
return _.isEqual(_.rest(data.path), h.path);
}
});
console.log('dbg', child.id, other.id, values);
if (_.isEqual(values, [46125])) debugger;
if (other) {
self.add_cell(child.id, other.id, values);
}
@ -381,14 +379,11 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
_query_db: function (groupby, fields, domain, path) {
var self = this,
field_ids = _.without(_.pluck(fields, 'field'), '__count'),
formatted_groupby = (groupby.interval) ? groupby.field + ':' + groupby.interval : groupby.field;
fields = _.map(field_ids, function(f) { return self.raw_field(f); });
fields = _.map(field_ids, function (field) {
return (_.contains(field, ':')) ? field.split(':')[0] : field;
});
return this.model.query(field_ids)
.filter(domain)
.group_by(formatted_groupby)
.group_by(groupby.field)
.then(function (results) {
var groups = _.filter(results, function (group) {
return group.attributes.length > 0;