[REF] removes some useless variables and some other cleaning to please jshint (addon...
[odoo/odoo.git] / addons / web_graph / static / src / js / graph.js
index 145ee07..8a7ac48 100644 (file)
@@ -22,6 +22,7 @@ instance.web_graph.GraphView = instance.web.View.extend({
         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 = [];
@@ -49,37 +50,60 @@ instance.web_graph.GraphView = instance.web.View.extend({
 
     view_loading: function (fields_view_get) {
         var self = this,
-            measure = null;
+            arch = fields_view_get.arch,
+            measure = null,
+            stacked = false;
 
-        if (fields_view_get.arch.attrs.type === 'bar') {
-            this.mode = 'bar_chart';
+        if (arch.attrs.type === 'bar' || !_.has(arch.attrs, 'type')) {
+            this.graph_widget.mode = 'bar_chart';
+        }
+        if (arch.attrs.stacked === 'True') {
+            stacked = true;
         }
 
-        _.each(fields_view_get.arch.children, function (field) {
-            if ('name' in field.attrs) {
+        _.each(arch.children, function (field) {
+            if (_.has(field.attrs, 'type')) {
+                switch (field.attrs.type) {
+                    case 'row':
+                        self.default_row_groupby.push(field.attrs.name);
+                        break;
+                    case 'col':
+                        self.default_col_groupby.push(field.attrs.name);
+                        break;
+                    case 'measure':
+                        measure = field.attrs.name;
+                        break;
+                }
+            } else {  // old style, kept for backward compatibility
                 if ('operator' in field.attrs) {
-                    measure = field.attrs.name;
+                    measure = (measure) ? measure : field.attrs.name;
                 } else {
-                    if (measure) {
-                        self.default_col_groupby.push(field.attrs.name);
-                    } else {
-                        self.default_row_groupby.push(field.attrs.name);
-                    }
+                    self.default_row_groupby.push(field.attrs.name);
                 }
             }
         });
-        this.graph_widget.pivot.config({measure:measure, update:false});
+        this.graph_widget.config({
+            measure:measure,
+            update:false,
+            bar_ui: (stacked) ? 'stack' : 'group'
+        });
     },
 
     do_search: function (domain, context, group_by) {
-        var self = this,
-            col_groupby = context.col_group_by || [],
+        var col_groupby = context.col_group_by || [],
             options = {domain:domain};
 
+        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) { 
+        if (!this.graph_widget.enabled) {
             options.update = false;
             options.silent = true;
         }
@@ -106,8 +130,12 @@ instance.web_graph.GraphView = instance.web.View.extend({
     register_groupby: function() {
         var self = this,
             query = this.search_view.query;
-        this.groupby_mode = 'manual';
 
+        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);
         });
@@ -143,7 +171,7 @@ instance.web_graph.GraphView = instance.web.View.extend({
 });
 
 instance.web_graph.Graph = instance.web.Widget.extend({
-    template: "GraphWidget",
+    template: 'GraphWidget',
 
     events: {
         'click .graph_mode_selection li' : 'mode_selection',
@@ -220,7 +248,6 @@ instance.web_graph.Graph = instance.web.Widget.extend({
                                          .attr('href', '#')
                                          .append(self.fields[measure].string);
                 measure_selection.append($('<li></li>').append(choice));
-
             });
         });
 
@@ -235,7 +262,7 @@ instance.web_graph.Graph = instance.web.Widget.extend({
         this.pivot.on('redraw_required', this, this.proxy('display_data'));
         this.pivot.update_data();
         this.enabled = true;
-        instance.web.bus.on('click', this, function (ev) {
+        instance.web.bus.on('click', this, function () {
             if (this.dropdown) {
                 this.dropdown.remove();
                 this.dropdown = null;
@@ -244,7 +271,6 @@ instance.web_graph.Graph = instance.web.Widget.extend({
     },
 
     display_data: function () {
-        var pivot = this.pivot;
         this.$('.graph_main_content svg').remove();
         this.table.empty();
 
@@ -253,7 +279,7 @@ instance.web_graph.Graph = instance.web.Widget.extend({
         } else {
             this.$('.graph_header').css('display', 'none');
         }
-        if (pivot.no_data) {
+        if (this.pivot.no_data) {
             var msg = 'No data available. Try to remove any filter or add some data.';
             this.table.append($('<tr><td>' + msg + '</td></tr>'));
         } else {
@@ -265,15 +291,7 @@ instance.web_graph.Graph = instance.web.Widget.extend({
                 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()));
-                // var options = {
-                //     svg: 
-                //     mode: this.mode,
-                //     width: this.$el.width(),
-                //     height: Math.min(Math.max(document.documentElement.clientHeight - 116 - 60, 250), Math.round(0.8*this.$el.width())),
-                //     measure_label: this.measure_label()
-                // };
                 this[this.mode]();
-                // openerp.web_graph.draw_chart(options);
             }
         }
     },
@@ -317,8 +335,7 @@ instance.web_graph.Graph = instance.web.Widget.extend({
         event.stopPropagation();
         var id = event.target.attributes['data-id'].nodeValue,
             header = this.pivot.get_header(id),
-            self = this,
-            dim = header.root.groupby.length;
+            self = this;
 
         if (header.is_expanded) {
             this.pivot.fold(header);
@@ -327,6 +344,9 @@ instance.web_graph.Graph = instance.web.Widget.extend({
                 var field = header.root.groupby[header.path.length];
                 this.pivot.expand(id, field);
             } else {
+                if (!this.important_fields.length) {
+                    return;
+                }
                 var fields = _.map(this.important_fields, function (field) {
                         return {id: field, value: self.fields[field].string};
                 });
@@ -341,8 +361,7 @@ instance.web_graph.Graph = instance.web.Widget.extend({
     },
 
     field_selection: function (event) {
-        var self = this,
-            id = event.target.attributes['data-id'].nodeValue,
+        var id = event.target.attributes['data-id'].nodeValue,
             field_id = event.target.attributes['data-field-id'].nodeValue;
         event.preventDefault();
         this.pivot.expand(id, field_id);
@@ -499,33 +518,14 @@ instance.web_graph.Graph = instance.web.Widget.extend({
         var self = this,
             dim_x = this.pivot.rows.groupby.length,
             dim_y = this.pivot.cols.groupby.length,
-            data = [];
+            data;
 
         // No groupby **************************************************************
         if ((dim_x === 0) && (dim_y === 0)) {
             data = [{key: 'Total', values:[{
-                title: 'Total',
-                value: this.pivot.get_value(this.pivot.rows.main.id, this.pivot.cols.main.id),
+                x: 'Total',
+                y: this.pivot.get_value(this.pivot.rows.main.id, this.pivot.cols.main.id),
             }]}];
-            nv.addGraph(function () {
-              var chart = nv.models.discreteBarChart()
-                    .x(function(d) { return d.title;})
-                    .y(function(d) { return d.value;})
-                    .tooltips(false)
-                    .showValues(true)
-                    .width(self.width)
-                    .height(self.height)
-                    .staggerLabels(true);
-
-                d3.select(self.svg)
-                    .datum(data)
-                    .attr('width', self.width)
-                    .attr('height', self.height)
-                    .call(chart);
-
-                nv.utils.windowResize(chart.update);
-                return chart;
-            });
         // Only column groupbys ****************************************************
         } else if ((dim_x === 0) && (dim_y >= 1)){
             data =  _.map(this.pivot.get_columns_depth(1), function (header) {
@@ -534,53 +534,14 @@ instance.web_graph.Graph = instance.web.Widget.extend({
                     values: [{x:header.root.main.title, y: self.pivot.get_total(header)}]
                 };
             });
-            nv.addGraph(function() {
-                var chart = nv.models.multiBarChart()
-                        .stacked(self.bar_ui === 'stacked')
-                        .tooltips(false)
-                        .width(self.width)
-                        .height(self.height)
-                        .showControls(false);
-
-                d3.select(self.svg)
-                    .datum(data)
-                    .attr('width', self.width)
-                    .attr('height', self.height)
-                    .transition()
-                    .duration(500)
-                    .call(chart);
-
-                nv.utils.windowResize(chart.update);
-
-                return chart;
-            });
         // Just 1 row groupby ******************************************************
         } else if ((dim_x === 1) && (dim_y === 0))  {
             data = _.map(this.pivot.rows.main.children, function (pt) {
                 var value = self.pivot.get_value(pt.id, self.pivot.cols.main.id),
                     title = (pt.title !== undefined) ? pt.title : 'Undefined';
-                return {title: title, value: value};
+                return {x: title, y: value};
             });
             data = [{key: this.measure_label(), values:data}];
-            nv.addGraph(function () {
-              var chart = nv.models.discreteBarChart()
-                    .x(function(d) { return d.title;})
-                    .y(function(d) { return d.value;})
-                    .tooltips(false)
-                    .showValues(true)
-                    .width(self.width)
-                    .height(self.height)
-                    .staggerLabels(true);
-
-                d3.select(self.svg)
-                    .datum(data)
-                    .attr('width', self.width)
-                    .attr('height', self.height)
-                    .call(chart);
-
-                nv.utils.windowResize(chart.update);
-                return chart;
-            });
         // 1 row groupby and some col groupbys**************************************
         } else if ((dim_x === 1) && (dim_y >= 1))  {
             data = _.map(this.pivot.get_columns_depth(1), function (colhdr) {
@@ -592,24 +553,6 @@ instance.web_graph.Graph = instance.web.Widget.extend({
                 });
                 return {key: colhdr.title || 'Undefined', values: values};
             });
-
-            nv.addGraph(function () {
-              var chart = nv.models.multiBarChart()
-                    .stacked(self.bar_ui === 'stacked')
-                    .staggerLabels(true)
-                    .width(self.width)
-                    .height(self.height)
-                    .tooltips(false);
-
-                d3.select(self.svg)
-                    .datum(data)
-                    .attr('width', self.width)
-                    .attr('height', self.height)
-                    .call(chart);
-
-                nv.utils.windowResize(chart.update);
-                return chart;
-            });
         // At least two row groupby*************************************************
         } else {
             var keys = _.uniq(_.map(this.pivot.get_rows_depth(2), function (hdr) {
@@ -627,25 +570,25 @@ instance.web_graph.Graph = instance.web.Widget.extend({
                 });
                 return {key:key, values: values};
             });
-
-            nv.addGraph(function () {
-              var chart = nv.models.multiBarChart()
-                    .stacked(self.bar_ui === 'stacked')
-                    .staggerLabels(true)
-                    .width(self.width)
-                    .height(self.height)
-                    .tooltips(false);
-
-                d3.select(self.svg)
-                    .datum(data)
-                    .attr('width', self.width)
-                    .attr('height', self.height)
-                    .call(chart);
-
-                nv.utils.windowResize(chart.update);
-                return chart;
-            });
         }
+
+        nv.addGraph(function () {
+          var chart = nv.models.multiBarChart()
+                .width(self.width)
+                .height(self.height)
+                .stacked(self.bar_ui === 'stack')
+                .staggerLabels(true);
+
+            d3.select(self.svg)
+                .datum(data)
+                .attr('width', self.width)
+                .attr('height', self.height)
+                .call(chart);
+
+            nv.utils.windowResize(chart.update);
+            return chart;
+        });
+
     },
 
     line_chart: function () {
@@ -685,8 +628,7 @@ instance.web_graph.Graph = instance.web.Widget.extend({
 
     pie_chart: function () {
         var self = this,
-            dim_x = this.pivot.rows.groupby.length,
-            dim_y = this.pivot.cols.groupby.length;
+            dim_x = this.pivot.rows.groupby.length;
 
         var data = _.map(this.pivot.get_rows_leaves(), function (row) {
             var title = _.map(row.path, function (p) {