[FIX] fixes a typo 'widet_config' => 'widget_config' (it previously crashed the graph...
[odoo/odoo.git] / addons / web_graph / static / src / js / graph_view.js
1 /*---------------------------------------------------------
2  * OpenERP web_graph
3  *---------------------------------------------------------*/
4
5 /* jshint undef: false  */
6
7 openerp.web_graph = function (instance) {
8 'use strict';
9
10 var _lt = instance.web._lt;
11 var _t = instance.web._t;
12
13 instance.web.views.add('graph', 'instance.web_graph.GraphView');
14
15 instance.web_graph.GraphView = instance.web.View.extend({
16     display_name: _lt('Graph'),
17     view_type: 'graph',
18
19     // ----------------------------------------------------------------------
20     // Init stuff
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;
27     },
28
29     view_loading: function (fields_view_get) {
30         var self = this,
31             arch = fields_view_get.arch;
32
33         this.widget_config = { 
34             title: arch.attrs.string,
35             stacked : (arch.attrs.stacked === 'True'),
36             mode: (arch.attrs.type) ? arch.attrs.type : 'bar',
37             measures: [],
38             row_groupby: [],
39             col_groupby: [],
40         };
41
42         _.each(arch.children, function (field) {
43             if (_.has(field.attrs, 'type')) {
44                 switch (field.attrs.type) {
45                     case 'row':
46                         self.widget_config.row_groupby.push(field.attrs.name);
47                         break;
48                     case 'col':
49                         self.widget_config.col_groupby.push(field.attrs.name);
50                         break;
51                     case 'measure':
52                         self.widget_config.measures.push(field.attrs.name);
53                         break;
54                 }
55             } else {  // old style, kept for backward compatibility
56                 if ('operator' in field.attrs) {
57                     self.widget_config.measures.push(field.attrs.name);
58                 } else {
59                     self.widget_config.row_groupby.push(field.attrs.name);
60                 }
61             }
62         });
63         if (self.widget_config.measures.length === 0) {
64             self.widget_config.measures.push('__count');
65         }
66     },
67
68     do_search: function (domain, context, group_by) {
69         var col_group_by = this.get_col_groupbys_from_searchview();
70
71         if (!this.graph_widget) {
72             if (group_by.length) {
73                 this.widget_config.row_groupby = group_by;
74             }
75             if (col_group_by.length) {
76                 this.widget_config.col_groupby = group_by;
77             }
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(); });
83             return;
84         }
85
86         if (this.swapped) {
87             this.swapped = false;
88             return;
89         }
90
91         this.graph_widget.set(domain, group_by, col_group_by);
92     },
93
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; });
98     },
99
100     do_show: function () {
101         this.do_push_state({});
102         return this._super();
103     },
104
105     // ----------------------------------------------------------------------
106     // Search view integration
107     // ----------------------------------------------------------------------
108
109     // add groupby to the search view
110     register_groupby: function() {
111         var query = this.search_view.query;
112
113         if (!_.has(this.search_view, '_s_groupby')) { return; }
114
115         // add row groupbys
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'});
119
120         if (row_search_facet) {
121             row_search_facet.values.reset(row_facet.values);
122         } else {
123             if (row_groupby.length) {
124                 query.add(row_facet);
125             }
126         }
127          // add col groupbys
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'});
131
132         if (col_search_facet) {
133             col_search_facet.values.reset(col_facet.values);
134         } else {
135             if (col_groupby.length) {
136                 query.add(col_facet);
137             }
138         }
139     },
140
141     swap_groupby: function () {
142         this.swap = true;
143         this.register_groupby();
144     },
145
146     make_row_groupby_facets: function(groupbys) {
147         return {
148             category:'GroupBy',
149             values: this.make_groupby_values(groupbys, 'group_by'),
150             icon:'w',
151             field: this.search_view._s_groupby
152         };
153     },
154
155     make_col_groupby_facets: function(groupbys) {
156         return {
157             category:'ColGroupBy',
158             values: this.make_groupby_values(groupbys, 'col_group_by'),
159             icon:'f',
160             field: this.search_field
161         };
162     },
163
164     make_groupby_values: function (groupbys, category) {
165         return _.map(groupbys, function (groupby) {
166             var context = {};
167             context[category] = groupby.field;
168             return {
169                 label: groupby.string, 
170                 value: {attrs:{domain: [], context: context}}
171             };
172         });
173     },
174
175     search_field: {
176         get_context: function() {},
177         get_domain: function () {},
178         get_groupby: function () {},
179     },
180
181 });
182
183 };
184
185
186
187
188
189
190
191