diff --git a/addons/web_graph/static/src/js/graph.js b/addons/web_graph/static/src/js/graph.js index 8cdc9a3eb0f..d92d09f3a8c 100644 --- a/addons/web_graph/static/src/js/graph.js +++ b/addons/web_graph/static/src/js/graph.js @@ -35,37 +35,14 @@ instance.web_graph.GraphView = instance.web.View.extend({ view_loading: function (fields_view_get) { var self = this, - arch = fields_view_get.arch, - measures = [], - stacked = false; + arch = fields_view_get.arch; - this.widget_config = { title: arch.attrs.string }; - - if (!_.has(arch.attrs, 'type')) { - this.widget_config.mode = 'bar_chart'; - } else { - switch (arch.attrs.type) { - case 'bar': - this.widget_config.mode = 'bar_chart'; - break; - case 'pie': - this.widget_config.mode = 'pie_chart'; - break; - case 'line': - this.widget_config.mode = 'line_chart'; - break; - case 'pivot': - case 'heatmap': - case 'row_heatmap': - case 'col_heatmap': - this.widget_config.mode = arch.attrs.type; - break; - } - } - - if (arch.attrs.stacked === 'True') { - this.widget_config.stacked = true; - } + this.widget_config = { + title: arch.attrs.string, + stacked : (arch.attrs.stacked === 'True'), + mode: (arch.attrs.type) ? arch.attrs.type : 'bar', + measures: [], + }; _.each(arch.children, function (field) { if (_.has(field.attrs, 'type')) { @@ -77,28 +54,22 @@ instance.web_graph.GraphView = instance.web.View.extend({ self.default_col_groupby.push(field.attrs.name); break; case 'measure': - measures.push(field.attrs.name); + self.widget_config.measures.push(field.attrs.name); break; } } else { // old style, kept for backward compatibility if ('operator' in field.attrs) { - measures.push(field.attrs.name); + self.widget_config.measures.push(field.attrs.name); } else { self.default_row_groupby.push(field.attrs.name); } } }); - if (measures.length === 0) { - measures.push('__count'); + if (self.widget_config.measures.length === 0) { + self.widget_config.measures.push('__count'); } this.widget_config.row_groupby = self.default_row_groupby; this.widget_config.col_groupby = self.default_col_groupby; - this.widget_config.measures = measures; - // measures:measures, - // update:false, - // title: title, - // bar_ui: (stacked) ? 'stack' : 'group' - // }); }, get_context: function (facet) { @@ -109,46 +80,42 @@ instance.web_graph.GraphView = instance.web.View.extend({ }, do_search: function (domain, context, group_by) { - var col_groupby = context.col_group_by || [], - options = {domain:domain}; + // var col_groupby = context.col_group_by || [], + // options = {domain:domain}; if (!this.graph_widget) { this.graph_widget = new openerp.web_graph.Graph(this, this.model, domain, this.widget_config); this.graph_widget.appendTo(this.$el); this.graph_widget.on('groupby_changed', this, this.proxy('register_groupby')); + this.ViewManager.on('switch_mode', this, function (e) { if (e === 'graph') this.graph_widget.reload(); }); } - this.search_view_groupby = group_by; + // 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) { - options.update = false; - options.silent = true; - } + // 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) { + // options.update = false; + // options.silent = true; + // } - if (this.groupby_mode === 'manual') { - options.row_groupby = group_by; - options.col_groupby = col_groupby; - } else { - options.row_groupby = _.toArray(this.default_row_groupby); - options.col_groupby = _.toArray(this.default_col_groupby); - } - this.graph_widget.set_domain(domain); - this.graph_widget.set_col_groupby(options.col_groupby); - this.graph_widget.set_row_groupby(options.row_groupby); + // if (this.groupby_mode === 'manual') { + // options.row_groupby = group_by; + // options.col_groupby = col_groupby; + // } else { + // options.row_groupby = _.toArray(this.default_row_groupby); + // options.col_groupby = _.toArray(this.default_col_groupby); + // } + // this.graph_widget.set(domain, options.row_groupby, options.col_groupby); + // this.graph_widget.set_domain(domain); + // this.graph_widget.set_col_groupby(options.col_groupby); + // this.graph_widget.set_row_groupby(options.row_groupby); - // this.graph_widget.pivot.config(options); - - if (!this.graph_widget.enabled) { - this.graph_widget.activate_display(); - this.ViewManager.on('switch_mode', this, function () {this.graph_widget.pivot.update_data(); }); - } }, do_show: function () { @@ -199,7 +166,6 @@ instance.web_graph.GraphView = instance.web.View.extend({ }, }); -// important_fields = [field], field = {field: _,type: _, string: _} instance.web_graph.Graph = instance.web.Widget.extend(openerp.EventDispatcherMixin, { template: 'GraphWidget', @@ -212,8 +178,6 @@ instance.web_graph.Graph = instance.web.Widget.extend(openerp.EventDispatcherMix }, init: function(parent, model, domain, options) { - var self = this; - this._super(parent); this.model = model; this.domain = domain; @@ -231,7 +195,7 @@ instance.web_graph.Graph = instance.web.Widget.extend(openerp.EventDispatcherMix self.important_fields = f; }); - var def2 = this.get_model_fields().then(function (f) { + var def2 = this.model.call('fields_get', []).then(function (f) { self.fields = f; self.measure_list = self.get_measures(); self.add_measures_to_options(); @@ -278,12 +242,6 @@ instance.web_graph.Graph = instance.web.Widget.extend(openerp.EventDispatcherMix }); }, - get_model_fields: function () { - return this.model.call('fields_get', []).then(function (fs) { - return fs; - }); - }, - get_measures: function(fields) { var measures = []; _.each(this.fields, function (f, id) { @@ -294,27 +252,14 @@ instance.web_graph.Graph = instance.web.Widget.extend(openerp.EventDispatcherMix return measures; }, - set_domain: function (domain) { + + set: function (domain, row_groupby, col_groupby) { if (this.pivot) { - this.pivot.set_domain(domain); + this.pivot.set(domain, row_groupby, col_groupby); } else { this.pivot_options.domain = domain; - } - }, - - set_row_groupby: function (groupby) { - if (this.pivot) { - this.pivot.set_row_groupby(groupby); - } else { - this.pivot_options.row_groupby = groupby; - } - }, - - set_col_groupby: function (groupby) { - if (this.pivot) { - this.pivot.set_col_groupby(groupby); - } else { - this.pivot_options.col_groupby = groupby; + this.pivot_options.row_groupby = row_groupby; + this.pivot_options.col_groupby = col_groupby; } }, @@ -329,10 +274,12 @@ instance.web_graph.Graph = instance.web.Widget.extend(openerp.EventDispatcherMix }, set_mode: function (mode) { - + this.mode = mode; + this.display_data(); }, - activate_display: function () { + reload: function () { + this.pivot.update_data(); }, display_data: function () { @@ -364,8 +311,7 @@ instance.web_graph.Graph = instance.web.Widget.extend(openerp.EventDispatcherMix mode_selection: function (event) { event.preventDefault(); var mode = event.target.attributes['data-mode'].nodeValue; - this.mode = mode; - this.display_data(); + this.set_mode(mode); }, measure_selection: function (event) { @@ -623,7 +569,7 @@ instance.web_graph.Graph = instance.web.Widget.extend(openerp.EventDispatcherMix /****************************************************************************** * Drawing charts methods... ******************************************************************************/ - bar_chart: function () { + bar: function () { var self = this, dim_x = this.pivot.rows.groupby.length, dim_y = this.pivot.cols.groupby.length, @@ -707,7 +653,7 @@ instance.web_graph.Graph = instance.web.Widget.extend(openerp.EventDispatcherMix }, - line_chart: function () { + line: function () { var self = this, dim_x = this.pivot.rows.groupby.length, dim_y = this.pivot.cols.groupby.length; @@ -742,7 +688,7 @@ instance.web_graph.Graph = instance.web.Widget.extend(openerp.EventDispatcherMix }); }, - pie_chart: function () { + pie: function () { var self = this, dim_x = this.pivot.rows.groupby.length; var data = _.map(this.pivot.get_rows_leaves(), function (row) { diff --git a/addons/web_graph/static/src/js/pivot.js b/addons/web_graph/static/src/js/pivot.js index dcf89de1144..10a6b3f9052 100644 --- a/addons/web_graph/static/src/js/pivot.js +++ b/addons/web_graph/static/src/js/pivot.js @@ -30,8 +30,6 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend(openerp.EventDispatcherM init: function (model, domain, fields, options) { openerp.EventDispatcherMixin.init.call(this); - this.rows = { groupby: [], headers: null }; - this.cols = { groupby: [], headers: null }; this.cells = []; this.domain = domain; this.no_data = true; @@ -40,9 +38,9 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend(openerp.EventDispatcherM this.fields.__count = {type: 'integer', string:'Quantity'}; this.measures = [{field:'__count', type: this.fields.type, string:this.fields.string}]; this.active = false; + this.rows = { groupby: this.create_field_values(options.row_groupby || []), headers: null }; + this.cols = { groupby: this.create_field_values(options.col_groupby || []), headers: null }; if (options.measures) { this.set_measures(options.measures); } - if (options.col_groupby) { this.set_col_groupby(options.col_groupby); } - if (options.row_groupby) { this.set_row_groupby(options.row_groupby); } }, // ---------------------------------------------------------------------- @@ -53,11 +51,6 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend(openerp.EventDispatcherM this.update_data(); }, - set_domain: function (domain) { - this.domain = domain; - if (this.active) { this.update_data(); } - }, - set_measures: function (measures) { var self = this; if (!_.isEqual(measures, this.measures)) { @@ -93,42 +86,37 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend(openerp.EventDispatcherM if (this.active) { this.update_data(); } }, - set_col_groupby: function (groupbys) { - var self = this; - if (!_.isEqual(groupbys, this.cols.groupby)) { - this.cols.groupby = []; - _.each(groupbys, function (g) { self._add_groupby(self.cols.groupby, g); }); - this.cols.headers = null; - if (this.active) { - this.trigger('groupby_changed'); - this.update_data(); - } - } - }, + set: function (domain, row_groupby, col_groupby) { + var row_gbs = this.create_field_values(row_groupby), + col_gbs = this.create_field_values(col_groupby), + dom_changed = !_.isEqual(this.domain, domain), + gb_changed = !(_.isEqual(row_gbs, this.rows.groupby) && + _.isEqual(col_gbs, this.cols.groupby)); - set_row_groupby: function (groupbys) { - var self = this; - if (!_.isEqual(groupbys, this.rows.groupby)) { - this.rows.groupby = []; - _.each(groupbys, function (g) { self._add_groupby(self.rows.groupby, g); }); - this.rows.headers = null; - if (this.active) { - this.trigger('groupby_changed'); - this.update_data(); - } - } - }, + if (dom_changed) { + this.domain = domain; + } + if (gb_changed) { + this.cols.groupby = col_gbs; + this.rows.groupby = row_gbs; + if (this.active) { this.trigger('groupby_changed'); } + } + if (this.active && (gb_changed || dom_changed)) { + this.update_data(); + } + }, + + create_field_value: function (f) { + return (f.field && f.string && f.type) ? f : {field: f, string: this.fields[f].string, type: this.fields[f].type}; + }, + + create_field_values: function (field_ids) { + var self = this; + return _.map(field_ids, function (f) { return self.create_field_value(f); }); + }, _add_groupby: function(groupby_list, groupby) { - if (groupby.field && groupby.string && groupby.type) { - groupby_list.push(groupby); - } else { - groupby_list.push({ - field:groupby, - string: this.fields[groupby].string, - type: this.fields[groupby].type - }); - } + groupby_list.push(this.create_field_value(groupby)); }, // ---------------------------------------------------------------------- @@ -413,7 +401,7 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend(openerp.EventDispatcherM group.attributes.value = 'undefined'; } else if (value instanceof Array) { group.attributes.value = value[1]; - } + } group.path = value ? (current_path || []).concat(group.attributes.value) : []; group.attributes.aggregates.__count = group.attributes.length; @@ -508,7 +496,6 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend(openerp.EventDispatcherM }, }); - })();