1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
5 /* jshint undef: false */
7 openerp.web_graph = function (instance) {
10 var _lt = instance.web._lt;
11 var _t = instance.web._t;
13 instance.web.views.add('graph', 'instance.web_graph.GraphView');
15 instance.web_graph.GraphView = instance.web.View.extend({
16 display_name: _lt('Graph'),
19 // ----------------------------------------------------------------------
21 // ----------------------------------------------------------------------
22 init: function(parent, dataset, view_id, options) {
23 this._super(parent, dataset, view_id, options);
24 this.dataset = dataset;
25 this.model = new instance.web.Model(dataset.model, {group_by_no_leaf: true});
26 this.search_view = parent.searchview;
29 view_loading: function (fields_view_get) {
31 arch = fields_view_get.arch;
33 this.widget_config = {
34 title: arch.attrs.string,
35 stacked : (arch.attrs.stacked === 'True'),
36 mode: (arch.attrs.type) ? arch.attrs.type : 'bar',
42 _.each(arch.children, function (field) {
43 if (_.has(field.attrs, 'type')) {
44 switch (field.attrs.type) {
46 self.widget_config.row_groupby.push(field.attrs.name);
49 self.widget_config.col_groupby.push(field.attrs.name);
52 self.widget_config.measures.push(field.attrs.name);
55 } else { // old style, kept for backward compatibility
56 if ('operator' in field.attrs) {
57 self.widget_config.measures.push(field.attrs.name);
59 self.widget_config.row_groupby.push(field.attrs.name);
63 if (self.widget_config.measures.length === 0) {
64 self.widget_config.measures.push('__count');
68 do_search: function (domain, context, group_by) {
69 var col_group_by = this.get_col_groupbys_from_searchview();
71 if (!this.graph_widget) {
72 if (group_by.length) {
73 this.widget_config.row_groupby = group_by;
75 if (col_group_by.length) {
76 this.widget_config.col_groupby = group_by;
78 this.graph_widget = new openerp.web_graph.Graph(this, this.model, domain, this.widget_config);
79 this.graph_widget.appendTo(this.$el);
80 this.graph_widget.on('groupby_changed', this, this.proxy('register_groupby'));
81 this.graph_widget.on('groupby_swapped', this, this.proxy('swap_groupby'));
82 this.ViewManager.on('switch_mode', this, function (e) { if (e === 'graph') this.graph_widget.reload(); });
91 this.graph_widget.set(domain, group_by, col_group_by);
94 get_col_groupbys_from_searchview: function () {
95 var facet = this.search_view.query.findWhere({category:'ColGroupBy'}),
96 groupby_list = facet ? facet.values.models : [];
97 return _.map(groupby_list, function (g) { return g.attributes.value.attrs.context.col_group_by; });
100 do_show: function () {
101 this.do_push_state({});
102 return this._super();
105 // ----------------------------------------------------------------------
106 // Search view integration
107 // ----------------------------------------------------------------------
109 // add groupby to the search view
110 register_groupby: function() {
111 var query = this.search_view.query;
113 if (!_.has(this.search_view, '_s_groupby')) { return; }
116 var row_groupby = this.graph_widget.get_row_groupby(),
117 row_facet = this.make_row_groupby_facets(row_groupby),
118 row_search_facet = query.findWhere({category:'GroupBy'});
120 if (row_search_facet) {
121 row_search_facet.values.reset(row_facet.values);
123 if (row_groupby.length) {
124 query.add(row_facet);
128 var col_groupby = this.graph_widget.get_col_groupby(),
129 col_facet = this.make_col_groupby_facets(col_groupby),
130 col_search_facet = query.findWhere({category:'ColGroupBy'});
132 if (col_search_facet) {
133 col_search_facet.values.reset(col_facet.values);
135 if (col_groupby.length) {
136 query.add(col_facet);
141 swap_groupby: function () {
143 this.register_groupby();
146 make_row_groupby_facets: function(groupbys) {
149 values: this.make_groupby_values(groupbys, 'group_by'),
151 field: this.search_view._s_groupby
155 make_col_groupby_facets: function(groupbys) {
157 category:'ColGroupBy',
158 values: this.make_groupby_values(groupbys, 'col_group_by'),
160 field: this.search_field
164 make_groupby_values: function (groupbys, category) {
165 return _.map(groupbys, function (groupby) {
167 context[category] = groupby.field;
169 label: groupby.string,
170 value: {attrs:{domain: [], context: context}}
176 get_context: function() {},
177 get_domain: function () {},
178 get_groupby: function () {},