2014-01-14 12:51:42 +00:00
|
|
|
/*---------------------------------------------------------
|
|
|
|
* OpenERP web_graph
|
|
|
|
*---------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* jshint undef: false */
|
|
|
|
|
|
|
|
openerp.web_graph = function (instance) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _lt = instance.web._lt;
|
|
|
|
var _t = instance.web._t;
|
|
|
|
|
|
|
|
instance.web.views.add('graph', 'instance.web_graph.GraphView');
|
|
|
|
|
|
|
|
instance.web_graph.GraphView = instance.web.View.extend({
|
|
|
|
display_name: _lt('Graph'),
|
|
|
|
view_type: 'graph',
|
|
|
|
|
2014-01-15 09:37:49 +00:00
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
// Init stuff
|
|
|
|
// ----------------------------------------------------------------------
|
2014-01-14 12:51:42 +00:00
|
|
|
init: function(parent, dataset, view_id, options) {
|
2014-01-22 16:07:07 +00:00
|
|
|
var self = this;
|
2014-01-14 12:51:42 +00:00
|
|
|
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;
|
2014-01-22 16:07:07 +00:00
|
|
|
this.col_search_field = {
|
2014-08-21 07:03:12 +00:00
|
|
|
get_context: function() { return { col_group_by: self.graph_widget.get_col_groupbys(),
|
|
|
|
measures: self.graph_widget.get_current_measures()};},
|
2014-01-22 16:07:07 +00:00
|
|
|
get_domain: function () {},
|
|
|
|
get_groupby: function () {},
|
|
|
|
};
|
2014-01-14 12:51:42 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
view_loading: function (fields_view_get) {
|
|
|
|
var self = this,
|
|
|
|
arch = fields_view_get.arch;
|
|
|
|
|
2014-01-21 14:30:34 +00:00
|
|
|
this.widget_config = {
|
2014-01-14 12:51:42 +00:00
|
|
|
title: arch.attrs.string,
|
|
|
|
stacked : (arch.attrs.stacked === 'True'),
|
2014-01-24 15:16:35 +00:00
|
|
|
mode: arch.attrs.type || 'bar',
|
2014-01-14 12:51:42 +00:00
|
|
|
measures: [],
|
2014-01-15 09:37:49 +00:00
|
|
|
row_groupby: [],
|
|
|
|
col_groupby: [],
|
2014-01-21 14:30:34 +00:00
|
|
|
graph_view: this,
|
2014-01-14 12:51:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
_.each(arch.children, function (field) {
|
2014-01-24 08:42:22 +00:00
|
|
|
var field_name = field.attrs.name;
|
|
|
|
if (_.has(field.attrs, 'interval')) {
|
|
|
|
field_name = field.attrs.name + ':' + field.attrs.interval;
|
2014-02-07 13:53:39 +00:00
|
|
|
}
|
2014-08-22 15:51:20 +00:00
|
|
|
//noinspection FallThroughInSwitchStatementJS
|
|
|
|
switch (field.attrs.type) {
|
|
|
|
case 'measure':
|
|
|
|
self.widget_config.measures.push(field_name);
|
|
|
|
break;
|
|
|
|
case 'col':
|
|
|
|
self.widget_config.col_groupby.push(field_name);
|
|
|
|
break;
|
|
|
|
default:
|
2014-01-14 12:51:42 +00:00
|
|
|
if ('operator' in field.attrs) {
|
2014-01-24 08:42:22 +00:00
|
|
|
self.widget_config.measures.push(field_name);
|
2014-08-22 15:51:20 +00:00
|
|
|
break;
|
2014-01-14 12:51:42 +00:00
|
|
|
}
|
2014-08-22 15:51:20 +00:00
|
|
|
case 'row':
|
|
|
|
self.widget_config.row_groupby.push(field_name);
|
2014-01-14 12:51:42 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
if (self.widget_config.measures.length === 0) {
|
|
|
|
self.widget_config.measures.push('__count');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
do_search: function (domain, context, group_by) {
|
2014-02-10 11:23:13 +00:00
|
|
|
if (this.ignore_do_search) {
|
|
|
|
this.ignore_do_search = false;
|
|
|
|
return;
|
|
|
|
}
|
2014-01-17 16:19:40 +00:00
|
|
|
var self = this,
|
2014-02-26 11:17:10 +00:00
|
|
|
groupbys = this.get_groupbys_from_searchview(),
|
2014-08-21 07:03:12 +00:00
|
|
|
col_group_by = groupbys.col_group_by,
|
|
|
|
measures = groupbys.measures;
|
2017-01-03 11:06:58 +00:00
|
|
|
// Do not forward-port
|
|
|
|
if(measures.length === 0 && context.measures) {
|
|
|
|
measures = context.measures;
|
|
|
|
}
|
2014-01-15 15:51:37 +00:00
|
|
|
|
2014-01-14 12:51:42 +00:00
|
|
|
if (!this.graph_widget) {
|
2014-09-26 09:38:06 +00:00
|
|
|
this.widget_config.context = _.clone(context);
|
|
|
|
this.widget_config.context.group_by_no_leaf = true;
|
2014-01-15 09:37:49 +00:00
|
|
|
if (group_by.length) {
|
|
|
|
this.widget_config.row_groupby = group_by;
|
|
|
|
}
|
2014-01-15 15:51:37 +00:00
|
|
|
if (col_group_by.length) {
|
2014-02-07 13:53:39 +00:00
|
|
|
this.widget_config.col_groupby = col_group_by;
|
2014-01-15 15:51:37 +00:00
|
|
|
}
|
2014-08-21 07:03:12 +00:00
|
|
|
if (measures.length) {
|
|
|
|
this.widget_config.measures = measures;
|
|
|
|
}
|
2014-01-14 12:51:42 +00:00
|
|
|
this.graph_widget = new openerp.web_graph.Graph(this, this.model, domain, this.widget_config);
|
|
|
|
this.graph_widget.appendTo(this.$el);
|
2014-01-21 14:30:34 +00:00
|
|
|
this.ViewManager.on('switch_mode', this, function (e) {
|
2014-02-26 11:17:10 +00:00
|
|
|
if (e === 'graph') {
|
|
|
|
var group_bys = self.get_groupbys_from_searchview();
|
2014-08-21 07:03:12 +00:00
|
|
|
this.graph_widget.set(domain, group_bys.group_by, group_bys.col_group_by, group_bys.measures);
|
2014-02-26 11:17:10 +00:00
|
|
|
}
|
2014-01-17 16:19:40 +00:00
|
|
|
});
|
2014-01-15 09:37:49 +00:00
|
|
|
return;
|
2014-01-14 12:51:42 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 07:03:12 +00:00
|
|
|
this.graph_widget.set(domain, group_by, col_group_by, measures);
|
2014-01-15 15:51:37 +00:00
|
|
|
},
|
|
|
|
|
2014-02-26 11:17:10 +00:00
|
|
|
get_groupbys_from_searchview: function () {
|
2014-08-21 07:03:12 +00:00
|
|
|
var result = { group_by: [], col_group_by: [], measures: []},
|
2014-02-26 11:17:10 +00:00
|
|
|
searchdata = this.search_view.build_search_data();
|
2014-02-07 13:53:39 +00:00
|
|
|
|
2014-02-26 11:17:10 +00:00
|
|
|
_.each(searchdata.groupbys, function (data) {
|
|
|
|
data = (_.isString(data)) ? py.eval(data) : data;
|
|
|
|
result.group_by = result.group_by.concat(data.group_by);
|
|
|
|
if (data.col_group_by) {
|
|
|
|
result.col_group_by = result.col_group_by.concat(data.col_group_by);
|
|
|
|
}
|
2014-08-21 07:03:12 +00:00
|
|
|
if (data.measures) {
|
|
|
|
result.measures = result.measures.concat(data.measures);
|
|
|
|
}
|
2014-02-26 11:17:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (result.col_group_by.length) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
_.each(searchdata.contexts, function (context) {
|
|
|
|
if (context.col_group_by) {
|
|
|
|
result.col_group_by = result.col_group_by.concat(context.col_group_by);
|
|
|
|
}
|
2014-08-21 07:03:12 +00:00
|
|
|
if (context.measures) {
|
|
|
|
result.measures = result.measures.concat(context.measures);
|
|
|
|
}
|
2014-01-22 13:34:52 +00:00
|
|
|
});
|
2014-02-26 11:17:10 +00:00
|
|
|
return result;
|
2014-01-17 16:19:40 +00:00
|
|
|
},
|
|
|
|
|
2014-01-14 12:51:42 +00:00
|
|
|
do_show: function () {
|
|
|
|
this.do_push_state({});
|
|
|
|
return this._super();
|
|
|
|
},
|
|
|
|
|
2014-01-15 09:37:49 +00:00
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
// Search view integration
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
// add groupby to the search view
|
2014-01-21 14:30:34 +00:00
|
|
|
register_groupby: function(row_groupby, col_groupby) {
|
2014-02-26 11:17:10 +00:00
|
|
|
var query = this.search_view.query,
|
|
|
|
groupbys = this.get_groupbys_from_searchview(),
|
|
|
|
search_row_groupby = groupbys.group_by,
|
|
|
|
search_col_groupby = groupbys.col_group_by,
|
2014-08-21 07:03:12 +00:00
|
|
|
search_measures = groupbys.measures,
|
2014-02-26 11:17:10 +00:00
|
|
|
row_gb_changed = !_.isEqual(_.pluck(row_groupby, 'field'), search_row_groupby),
|
2014-08-21 07:03:12 +00:00
|
|
|
col_gb_changed = !_.isEqual(_.pluck(col_groupby, 'field'), search_col_groupby),
|
|
|
|
measures_gb_changed = !_.isEqual(_.pluck(col_groupby, 'field'), search_col_groupby);
|
2014-01-15 09:37:49 +00:00
|
|
|
if (!_.has(this.search_view, '_s_groupby')) { return; }
|
|
|
|
|
2014-02-26 11:17:10 +00:00
|
|
|
if (!row_gb_changed && !col_gb_changed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
[FIX] web: fixes issues with custom filters and search view
The problem was that when the user manipulates the graph view (in pivot
table mode), the graph view resetted the group by facet in the search
view. It was not a problem unless a custom filter with a groupby was
already there, in which case, the group bys were duplicated.
The search view is now smarter, it only resets the additional groupbys
(and col_groupbys). Also, to prevent usability problems, the graph
view disable the '+/-' groupbys added by a custom filters.
Note that this fix is only temporary: a revamp of custom filters, facets,
search view is coming in the next months. (at least, that's the idea). Right
now, too much 'search logic' is in the graph view.
Another note: this fix is somewhat fragile: it makes some assumptions
about the search query (mainly that the custom filter is the first facet,
also, that no other filters add groupbys/col_groupbys)
2014-09-09 10:26:16 +00:00
|
|
|
var custom_groups = this.get_custom_filter_groupbys();
|
|
|
|
row_groupby = row_groupby.slice(custom_groups.groupby.length);
|
|
|
|
col_groupby = col_groupby.slice(custom_groups.col_groupby.length);
|
|
|
|
|
2014-02-26 11:17:10 +00:00
|
|
|
if (row_gb_changed && col_gb_changed) {
|
2014-02-10 11:23:13 +00:00
|
|
|
// when two changes to the search view will be done, the method do_search
|
|
|
|
// will be called twice, once with the correct groupby and incorrect col_groupby,
|
|
|
|
// and once with correct informations. This flag is necessary to prevent the
|
|
|
|
// incorrect informations to propagate and trigger useless queries
|
|
|
|
this.ignore_do_search = true;
|
|
|
|
}
|
|
|
|
|
2014-02-26 11:17:10 +00:00
|
|
|
if (row_gb_changed) {
|
|
|
|
// add row groupbys
|
|
|
|
var row_facet = this.make_row_groupby_facets(row_groupby),
|
|
|
|
row_search_facet = query.findWhere({category:'GroupBy'});
|
2014-01-15 09:37:49 +00:00
|
|
|
|
2014-02-26 11:17:10 +00:00
|
|
|
if (row_search_facet) {
|
2014-04-22 15:03:16 +00:00
|
|
|
row_search_facet.values.reset(row_facet.values, {focus_input:false});
|
2014-02-26 11:17:10 +00:00
|
|
|
} else {
|
|
|
|
if (row_groupby.length) {
|
|
|
|
query.add(row_facet);
|
|
|
|
}
|
2014-01-15 15:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-26 11:17:10 +00:00
|
|
|
|
|
|
|
if (col_gb_changed) {
|
|
|
|
// add col groupbys
|
|
|
|
var col_facet = this.make_col_groupby_facets(col_groupby),
|
|
|
|
col_search_facet = query.findWhere({category:'ColGroupBy'});
|
|
|
|
|
|
|
|
if (col_search_facet) {
|
2014-04-22 15:03:16 +00:00
|
|
|
col_search_facet.values.reset(col_facet.values, {focus_input:false});
|
2014-02-26 11:17:10 +00:00
|
|
|
} else {
|
|
|
|
if (col_groupby.length) {
|
|
|
|
query.add(col_facet);
|
|
|
|
}
|
2014-01-15 09:37:49 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-14 12:51:42 +00:00
|
|
|
},
|
2014-01-15 09:37:49 +00:00
|
|
|
|
2014-01-15 15:51:37 +00:00
|
|
|
make_row_groupby_facets: function(groupbys) {
|
|
|
|
return {
|
|
|
|
category:'GroupBy',
|
|
|
|
values: this.make_groupby_values(groupbys, 'group_by'),
|
|
|
|
icon:'w',
|
|
|
|
field: this.search_view._s_groupby
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
make_col_groupby_facets: function(groupbys) {
|
|
|
|
return {
|
|
|
|
category:'ColGroupBy',
|
|
|
|
values: this.make_groupby_values(groupbys, 'col_group_by'),
|
|
|
|
icon:'f',
|
2014-01-22 16:07:07 +00:00
|
|
|
field: this.col_search_field
|
2014-01-15 15:51:37 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
make_groupby_values: function (groupbys, category) {
|
|
|
|
return _.map(groupbys, function (groupby) {
|
|
|
|
var context = {};
|
2014-01-22 16:07:07 +00:00
|
|
|
context[category] = groupby.field;
|
|
|
|
var value;
|
|
|
|
if (category === 'group_by' && groupby.type !== 'date' && groupby.type !== 'datetime') {
|
2014-01-23 11:34:50 +00:00
|
|
|
value = groupby.filter || {attrs: {domain: [], context: context}};
|
2014-01-22 16:07:07 +00:00
|
|
|
} else {
|
|
|
|
value = {attrs: {domain: [], context: context}};
|
|
|
|
}
|
2014-01-15 15:51:37 +00:00
|
|
|
return {
|
2014-01-21 14:30:34 +00:00
|
|
|
label: groupby.string,
|
|
|
|
value: value
|
2014-01-15 15:51:37 +00:00
|
|
|
};
|
|
|
|
});
|
2014-01-15 09:37:49 +00:00
|
|
|
},
|
[FIX] web: fixes issues with custom filters and search view
The problem was that when the user manipulates the graph view (in pivot
table mode), the graph view resetted the group by facet in the search
view. It was not a problem unless a custom filter with a groupby was
already there, in which case, the group bys were duplicated.
The search view is now smarter, it only resets the additional groupbys
(and col_groupbys). Also, to prevent usability problems, the graph
view disable the '+/-' groupbys added by a custom filters.
Note that this fix is only temporary: a revamp of custom filters, facets,
search view is coming in the next months. (at least, that's the idea). Right
now, too much 'search logic' is in the graph view.
Another note: this fix is somewhat fragile: it makes some assumptions
about the search query (mainly that the custom filter is the first facet,
also, that no other filters add groupbys/col_groupbys)
2014-09-09 10:26:16 +00:00
|
|
|
|
|
|
|
get_custom_filter_groupbys: function () {
|
|
|
|
var gb = [],
|
|
|
|
col_gb = [];
|
|
|
|
|
|
|
|
var facet = this.search_view.query.at(0);
|
|
|
|
if (facet) {
|
|
|
|
if (facet.get('category') !== 'GroupBy' && facet.get('category') !== 'ColGroupBy') {
|
|
|
|
gb = get_groupby(facet);
|
|
|
|
col_gb = get_col_groupby(facet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
groupby: gb,
|
|
|
|
col_groupby: col_gb,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 12:51:42 +00:00
|
|
|
});
|
[FIX] web: fixes issues with custom filters and search view
The problem was that when the user manipulates the graph view (in pivot
table mode), the graph view resetted the group by facet in the search
view. It was not a problem unless a custom filter with a groupby was
already there, in which case, the group bys were duplicated.
The search view is now smarter, it only resets the additional groupbys
(and col_groupbys). Also, to prevent usability problems, the graph
view disable the '+/-' groupbys added by a custom filters.
Note that this fix is only temporary: a revamp of custom filters, facets,
search view is coming in the next months. (at least, that's the idea). Right
now, too much 'search logic' is in the graph view.
Another note: this fix is somewhat fragile: it makes some assumptions
about the search query (mainly that the custom filter is the first facet,
also, that no other filters add groupbys/col_groupbys)
2014-09-09 10:26:16 +00:00
|
|
|
|
|
|
|
function get_groupby(facet) {
|
|
|
|
var field = facet.get('field'),
|
|
|
|
result = [];
|
|
|
|
if ('get_groupby' in field) {
|
|
|
|
result = instance.web.pyeval.sync_eval_domains_and_contexts({
|
|
|
|
group_by_seq: field.get_groupby(facet)
|
|
|
|
}).group_by;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
function get_col_groupby(facet) {
|
|
|
|
var field = facet.get('field'),
|
|
|
|
result = [];
|
|
|
|
if ('get_context' in field) {
|
|
|
|
result = instance.web.pyeval.sync_eval_domains_and_contexts({
|
|
|
|
contexts: field.get_context(facet)
|
|
|
|
}).context.col_group_by || [];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-14 12:51:42 +00:00
|
|
|
};
|