[IMP] adds search view integration in graph view (addon web_graph)

bzr revid: ged@openerp.com-20140115093749-t2s0306fchm2tnir
This commit is contained in:
Gery Debongnie 2014-01-15 10:37:49 +01:00
parent b681cff77f
commit cf81efda43
3 changed files with 132 additions and 144 deletions

View File

@ -16,20 +16,14 @@ instance.web_graph.GraphView = instance.web.View.extend({
display_name: _lt('Graph'),
view_type: 'graph',
// ----------------------------------------------------------------------
// Init stuff
// ----------------------------------------------------------------------
init: function(parent, dataset, view_id, options) {
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.search_view_groupby = [];
this.groupby_mode = 'default'; // 'default' or 'manual'
this.default_row_groupby = [];
this.default_col_groupby = [];
this.search_field = {
get_context: this.proxy('get_context'),
get_domain: function () {},
get_groupby: function () { },
};
},
view_loading: function (fields_view_get) {
@ -41,16 +35,18 @@ instance.web_graph.GraphView = instance.web.View.extend({
stacked : (arch.attrs.stacked === 'True'),
mode: (arch.attrs.type) ? arch.attrs.type : 'bar',
measures: [],
row_groupby: [],
col_groupby: [],
};
_.each(arch.children, function (field) {
if (_.has(field.attrs, 'type')) {
switch (field.attrs.type) {
case 'row':
self.default_row_groupby.push(field.attrs.name);
self.widget_config.row_groupby.push(field.attrs.name);
break;
case 'col':
self.default_col_groupby.push(field.attrs.name);
self.widget_config.col_groupby.push(field.attrs.name);
break;
case 'measure':
self.widget_config.measures.push(field.attrs.name);
@ -60,61 +56,28 @@ instance.web_graph.GraphView = instance.web.View.extend({
if ('operator' in field.attrs) {
self.widget_config.measures.push(field.attrs.name);
} else {
self.default_row_groupby.push(field.attrs.name);
self.widet_config.row_groupby.push(field.attrs.name);
}
}
});
if (self.widget_config.measures.length === 0) {
self.widget_config.measures.push('__count');
}
this.widget_config.row_groupby = self.default_row_groupby;
this.widget_config.col_groupby = self.default_col_groupby;
},
get_context: function (facet) {
var col_group_by = _.map(facet.values.models, function (model) {
return model.attributes.value.attrs.context.col_group_by;
});
return {col_group_by : col_group_by};
},
do_search: function (domain, context, group_by) {
// var col_groupby = context.col_group_by || [],
// options = {domain:domain};
if (!this.graph_widget) {
if (group_by.length) {
this.widget_config.row_groupby = group_by;
}
this.graph_widget = new openerp.web_graph.Graph(this, this.model, domain, this.widget_config);
this.graph_widget.appendTo(this.$el);
this.graph_widget.on('groupby_changed', this, this.proxy('register_groupby'));
this.ViewManager.on('switch_mode', this, function (e) { if (e === 'graph') this.graph_widget.reload(); });
return;
}
// this.search_view_groupby = group_by;
// if (group_by.length && this.groupby_mode !== 'manual') {
// if (_.isEqual(col_groupby, [])) {
// col_groupby = this.default_col_groupby;
// }
// }
// if (group_by.length || col_groupby.length) {
// this.groupby_mode = 'manual';
// }
// if (!this.graph_widget.enabled) {
// options.update = false;
// options.silent = true;
// }
// if (this.groupby_mode === 'manual') {
// options.row_groupby = group_by;
// options.col_groupby = col_groupby;
// } else {
// options.row_groupby = _.toArray(this.default_row_groupby);
// options.col_groupby = _.toArray(this.default_col_groupby);
// }
// this.graph_widget.set(domain, options.row_groupby, options.col_groupby);
// this.graph_widget.set_domain(domain);
// this.graph_widget.set_col_groupby(options.col_groupby);
// this.graph_widget.set_row_groupby(options.row_groupby);
this.graph_widget.set(domain, group_by, this.graph_widget.get_col_groupby());
},
do_show: function () {
@ -122,47 +85,46 @@ instance.web_graph.GraphView = instance.web.View.extend({
return this._super();
},
// ----------------------------------------------------------------------
// Search view integration
// ----------------------------------------------------------------------
// add groupby to the search view
register_groupby: function() {
// var self = this,
// query = this.search_view.query;
var query = this.search_view.query;
// this.groupby_mode = 'manual';
// if (_.isEqual(this.search_view_groupby, this.graph_widget.pivot.rows.groupby) ||
// (!_.has(this.search_view, '_s_groupby'))) {
// return;
// }
// var rows = _.map(this.graph_widget.pivot.rows.groupby, function (group) {
// return make_facet('GroupBy', group);
// });
// var cols = _.map(this.graph_widget.pivot.cols.groupby, function (group) {
// return make_facet('ColGroupBy', group);
// });
if (!_.has(this.search_view, '_s_groupby')) { return; }
// query.reset(rows.concat(cols));
var row_groupby = this.graph_widget.get_row_groupby();
var gb_facet = this.make_groupby_facets(row_groupby);
var search_facet = query.findWhere({category:'GroupBy'});
// function make_facet (category, fields) {
// var values,
// icon,
// backbone_field,
// cat_name;
// if (!(fields instanceof Array)) { fields = [fields]; }
// if (category === 'GroupBy') {
// cat_name = 'group_by';
// icon = 'w';
// backbone_field = self.search_view._s_groupby;
// } else {
// cat_name = 'col_group_by';
// icon = 'f';
// backbone_field = self.search_field;
// }
// values = _.map(fields, function (field) {
// var context = {};
// context[cat_name] = field;
// return {label: self.graph_widget.fields[field].string, value: {attrs:{domain: [], context: context}}};
// });
// return {category:category, values: values, icon:icon, field: backbone_field};
// }
if (search_facet) {
search_facet.values.reset(gb_facet.values);
} else {
if (row_groupby.length) {
query.add(gb_facet);
}
}
},
make_groupby_facets: function(fields) {
var self = this,
values = _.map(fields, function (field) {
return {
label: self.graph_widget.fields[field].string,
value: {attrs:{domain: [], context: {group_by: field}}}
};
});
return {category:'GroupBy', values: values, icon:'w', field: self.search_view._s_groupby};
},
search_field: {
get_context: function() {},
get_domain: function () {},
get_groupby: function () { },
},
});

View File

@ -9,14 +9,9 @@ nv.dev = false; // sets nvd3 library in production mode
openerp.web_graph.Graph = openerp.web.Widget.extend(openerp.EventDispatcherMixin, {
template: 'GraphWidget',
events: {
'click .graph_mode_selection li' : 'mode_selection',
'click .graph_measure_selection li' : 'measure_selection',
'click .graph_options_selection li' : 'option_selection',
'click .web_graph_click' : 'header_cell_clicked',
'click a.field-selection' : 'field_selection',
},
// ----------------------------------------------------------------------
// Init stuff
// ----------------------------------------------------------------------
init: function(parent, model, domain, options) {
this._super(parent);
this.model = model;
@ -56,6 +51,8 @@ openerp.web_graph.Graph = openerp.web.Widget.extend(openerp.EventDispatcherMixin
});
},
// this method gets the fields that appear in the search view, under the
// 'Groupby' heading
get_search_fields: function () {
var self = this,
options = {model:this.model, view_type: 'search'},
@ -83,6 +80,7 @@ openerp.web_graph.Graph = openerp.web.Widget.extend(openerp.EventDispatcherMixin
});
},
// Extracts the integer/float fields which are not 'id'
get_measures: function(fields) {
var measures = [];
_.each(this.fields, function (f, id) {
@ -93,17 +91,6 @@ openerp.web_graph.Graph = openerp.web.Widget.extend(openerp.EventDispatcherMixin
return measures;
},
set: function (domain, row_groupby, col_groupby) {
if (this.pivot) {
this.pivot.set(domain, row_groupby, col_groupby);
} else {
this.pivot_options.domain = domain;
this.pivot_options.row_groupby = row_groupby;
this.pivot_options.col_groupby = col_groupby;
}
},
add_measures_to_options: function() {
var measure_selection = this.$('.graph_measure_selection');
_.each(this.measure_list, function (measure) {
@ -114,39 +101,47 @@ openerp.web_graph.Graph = openerp.web.Widget.extend(openerp.EventDispatcherMixin
});
},
// ----------------------------------------------------------------------
// Configuration methods
// ----------------------------------------------------------------------
set: function (domain, row_groupby, col_groupby) {
if (this.pivot) {
this.pivot.set(domain, row_groupby, col_groupby);
} else {
this.pivot_options.domain = domain;
this.pivot_options.row_groupby = row_groupby;
this.pivot_options.col_groupby = col_groupby;
}
},
set_mode: function (mode) {
this.mode = mode;
this.display_data();
},
// returns the row groupbys as a list of fields (not the representation used
// internally by pivot table)
get_row_groupby: function () {
return _.pluck(this.pivot.rows.groupby, 'field');
},
get_col_groupby: function () {
return _.pluck(this.pivot.cols.groupby, 'field');
},
reload: function () {
this.pivot.update_data();
},
display_data: function () {
this.$('.graph_main_content svg').remove();
this.$('.graph_main_content div').remove();
this.table.empty();
if (this.visible_ui) {
this.$('.graph_header').css('display', 'block');
} else {
this.$('.graph_header').css('display', 'none');
}
if (this.pivot.no_data) {
this.$('.graph_main_content').append($(QWeb.render('graph_no_data')));
} else {
var table_modes = ['pivot', 'heatmap', 'row_heatmap', 'col_heatmap'];
if (_.contains(table_modes, this.mode)) {
this.draw_table();
} else {
this.$('.graph_main_content').append($('<div><svg></svg></div>'));
this.svg = this.$('.graph_main_content svg')[0];
this.width = this.$el.width();
this.height = Math.min(Math.max(document.documentElement.clientHeight - 116 - 60, 250), Math.round(0.8*this.$el.width()));
this[this.mode]();
}
}
// ----------------------------------------------------------------------
// UI code
// ----------------------------------------------------------------------
events: {
'click .graph_mode_selection li' : 'mode_selection',
'click .graph_measure_selection li' : 'measure_selection',
'click .graph_options_selection li' : 'option_selection',
'click .web_graph_click' : 'header_cell_clicked',
'click a.field-selection' : 'field_selection',
},
mode_selection: function (event) {
@ -233,9 +228,38 @@ openerp.web_graph.Graph = openerp.web.Widget.extend(openerp.EventDispatcherMixin
}
},
/******************************************************************************
* Drawing pivot table methods...
******************************************************************************/
// ----------------------------------------------------------------------
// Main display method
// ----------------------------------------------------------------------
display_data: function () {
this.$('.graph_main_content svg').remove();
this.$('.graph_main_content div').remove();
this.table.empty();
if (this.visible_ui) {
this.$('.graph_header').css('display', 'block');
} else {
this.$('.graph_header').css('display', 'none');
}
if (this.pivot.no_data) {
this.$('.graph_main_content').append($(QWeb.render('graph_no_data')));
} else {
var table_modes = ['pivot', 'heatmap', 'row_heatmap', 'col_heatmap'];
if (_.contains(table_modes, this.mode)) {
this.draw_table();
} else {
this.$('.graph_main_content').append($('<div><svg></svg></div>'));
this.svg = this.$('.graph_main_content svg')[0];
this.width = this.$el.width();
this.height = Math.min(Math.max(document.documentElement.clientHeight - 116 - 60, 250), Math.round(0.8*this.$el.width()));
this[this.mode]();
}
}
},
// ----------------------------------------------------------------------
// Drawing the table
// ----------------------------------------------------------------------
draw_table: function () {
this.pivot.main_row().title = 'Total';
if (this.pivot.measures.length == 1) {
@ -411,22 +435,22 @@ openerp.web_graph.Graph = openerp.web.Widget.extend(openerp.EventDispatcherMixin
}
},
/******************************************************************************
* Drawing charts methods...
******************************************************************************/
// ----------------------------------------------------------------------
// Drawing charts code
// ----------------------------------------------------------------------
bar: function () {
var self = this,
dim_x = this.pivot.rows.groupby.length,
dim_y = this.pivot.cols.groupby.length,
data;
// No groupby **************************************************************
// No groupby
if ((dim_x === 0) && (dim_y === 0)) {
data = [{key: 'Total', values:[{
x: 'Total',
y: this.pivot.get_total(),
}]}];
// Only column groupbys ****************************************************
// Only column groupbys
} else if ((dim_x === 0) && (dim_y >= 1)){
data = _.map(this.pivot.get_columns_with_depth(1), function (header) {
return {
@ -434,7 +458,7 @@ openerp.web_graph.Graph = openerp.web.Widget.extend(openerp.EventDispatcherMixin
values: [{x:header.root[0].title, y: self.pivot.get_total(header)[0]}]
};
});
// Just 1 row groupby ******************************************************
// Just 1 row groupby
} else if ((dim_x === 1) && (dim_y === 0)) {
data = _.map(this.pivot.main_row().children, function (pt) {
var value = self.pivot.get_total(pt),
@ -442,7 +466,7 @@ openerp.web_graph.Graph = openerp.web.Widget.extend(openerp.EventDispatcherMixin
return {x: title, y: value};
});
data = [{key: self.pivot.measures[0].string, values:data}];
// 1 row groupby and some col groupbys**************************************
// 1 row groupby and some col groupbys
} else if ((dim_x === 1) && (dim_y >= 1)) {
data = _.map(this.pivot.get_cols_with_depth(1), function (colhdr) {
var values = _.map(self.pivot.get_rows_with_depth(1), function (header) {
@ -453,7 +477,7 @@ openerp.web_graph.Graph = openerp.web.Widget.extend(openerp.EventDispatcherMixin
});
return {key: colhdr.title || 'Undefined', values: values};
});
// At least two row groupby*************************************************
// At least two row groupby
} else {
var keys = _.uniq(_.map(this.pivot.get_rows_with_depth(2), function (hdr) {
return hdr.title || 'Undefined';

View File

@ -77,18 +77,20 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend(openerp.EventDispatcherM
var row_gbs = this.create_field_values(row_groupby),
col_gbs = this.create_field_values(col_groupby),
dom_changed = !_.isEqual(this.domain, domain),
gb_changed = !(_.isEqual(row_gbs, this.rows.groupby) &&
_.isEqual(col_gbs, this.cols.groupby));
row_gb_changed = !_.isEqual(row_gbs, this.rows.groupby),
col_gb_changed = !_.isEqual(col_gbs, this.cols.groupby);
if (dom_changed) {
this.domain = domain;
}
if (gb_changed) {
if (row_gb_changed || col_gb_changed) {
this.cols.groupby = col_gbs;
this.rows.groupby = row_gbs;
if (row_gb_changed) {this.rows.headers = null; }
if (col_gb_changed) {this.cols.headers = null; }
if (this.active) { this.trigger('groupby_changed'); }
}
if (this.active && (gb_changed || dom_changed)) {
if (this.active && (row_gb_changed || col_gb_changed || dom_changed)) {
this.update_data();
}
},