48237393a54a8db018a04d864604bb43003622c1
[odoo/odoo.git] / addons / web / static / src / js / view_pivot.js
1 /*---------------------------------------------------------
2  * Odoo Pivot Table view
3  *---------------------------------------------------------*/
4
5 (function () {
6 'use strict';
7
8 var instance = openerp,
9         _lt = instance.web._lt,
10         _t = instance.web._t,
11         QWeb = instance.web.qweb;
12
13 instance.web.views.add('pivot', 'instance.web.PivotView');
14
15 instance.web.PivotView = instance.web.View.extend({
16         template: 'PivotView',
17     display_name: _lt('Pivot'),
18     view_type: 'pivot',
19
20     init: function(parent, dataset, view_id, options) {
21         this._super(parent, dataset, view_id, options);
22         this.model = new instance.web.Model(dataset.model, {group_by_no_leaf: true});
23         this.action_manager = parent.action_manager;
24
25         this.$buttons = options.$buttons;
26         this.fields = {};
27         this.measures = {};
28         this.groupable_fields = {};
29
30         this.row_groupbys = [];
31         this.col_groupbys = [];
32         this.active_measures = [];
33         },
34         start: function () {
35                 var self = this,
36                 load_fields = this.model.call('fields_get', [])
37                         .then(this.prepare_fields.bind(this));
38
39                 return $.when(this._super(), load_fields).then(function () {
40                         var context = {measures: _.pairs(self.measures)};
41                         self.$buttons.html(QWeb.render('PivotView.buttons', self), context);
42                 });
43         },
44         view_loading: function (fvg) {
45                 var self = this;
46                 this.do_push_state({});
47                 fvg.arch.children.forEach(function (field) {
48                         var name = field.attrs.name + (field.attrs.interval || '');
49             //noinspection FallThroughInSwitchStatementJS
50             switch (field.attrs.type) {
51             case 'measure':
52                 self.active_measures.push(name);
53                 break;
54             case 'col':
55                 self.col_groupbys.push(name);
56                 break;
57             default:
58                 if ('operator' in field.attrs) {
59                     self.active_measures.push(name);
60                     break;
61                 }
62             case 'row':
63                 self.row_groupbys.push(name);
64             }
65                 });
66         },
67     prepare_fields: function (fields) {
68         var self = this,
69             groupable_types = ['many2one', 'char', 'boolean', 
70                                            'selection', 'date', 'datetime'];
71         this.fields = fields;
72         _.each(fields, function (field, name) {
73                 if ((name !== 'id') && (field.store === true)) {
74                         if (field.type === 'integer' || field.type === 'float') {
75                                 self.measures[name] = field;
76                         }
77                         if (_.contains(groupable_types, field.type)) {
78                                 self.groupable_fields[name] = field;
79                         }
80                 }
81         });
82     },
83     do_search: function (domain, context, group_by) {
84         console.log('do_search', domain, context, group_by);
85         debugger;
86         context.measure = "lol";
87     },
88 });
89
90
91 })();
92