diff options
Diffstat (limited to 'addons/web_graph/static/src/js/graph_view.js')
| -rw-r--r-- | addons/web_graph/static/src/js/graph_view.js | 140 |
1 files changed, 51 insertions, 89 deletions
diff --git a/addons/web_graph/static/src/js/graph_view.js b/addons/web_graph/static/src/js/graph_view.js index 1b940c1703a..eb901c28423 100644 --- a/addons/web_graph/static/src/js/graph_view.js +++ b/addons/web_graph/static/src/js/graph_view.js @@ -16,20 +16,14 @@ instance.web_graph.GraphView = instance.web.View.extend({ display_name: _lt('Graph'), view_type: 'graph', + // ---------------------------------------------------------------------- + // Init stuff + // ---------------------------------------------------------------------- init: function(parent, dataset, view_id, options) { this._super(parent, dataset, view_id, options); 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 = []; - this.search_field = { - get_context: this.proxy('get_context'), - get_domain: function () {}, - get_groupby: function () { }, - }; }, view_loading: function (fields_view_get) { @@ -41,16 +35,18 @@ instance.web_graph.GraphView = instance.web.View.extend({ stacked : (arch.attrs.stacked === 'True'), mode: (arch.attrs.type) ? arch.attrs.type : 'bar', measures: [], + row_groupby: [], + col_groupby: [], }; _.each(arch.children, function (field) { if (_.has(field.attrs, 'type')) { switch (field.attrs.type) { case 'row': - self.default_row_groupby.push(field.attrs.name); + self.widget_config.row_groupby.push(field.attrs.name); break; case 'col': - self.default_col_groupby.push(field.attrs.name); + self.widget_config.col_groupby.push(field.attrs.name); break; case 'measure': self.widget_config.measures.push(field.attrs.name); @@ -60,61 +56,28 @@ instance.web_graph.GraphView = instance.web.View.extend({ if ('operator' in field.attrs) { self.widget_config.measures.push(field.attrs.name); } else { - self.default_row_groupby.push(field.attrs.name); + self.widet_config.row_groupby.push(field.attrs.name); } } }); 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; - }, - - get_context: function (facet) { - var col_group_by = _.map(facet.values.models, function (model) { - return model.attributes.value.attrs.context.col_group_by; - }); - return {col_group_by : col_group_by}; }, do_search: function (domain, context, group_by) { - // var col_groupby = context.col_group_by || [], - // options = {domain:domain}; - if (!this.graph_widget) { + if (group_by.length) { + this.widget_config.row_groupby = group_by; + } 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(); }); + return; } - // 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 (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.set(domain, group_by, this.graph_widget.get_col_groupby()); }, do_show: function () { @@ -122,47 +85,46 @@ instance.web_graph.GraphView = instance.web.View.extend({ return this._super(); }, + // ---------------------------------------------------------------------- + // Search view integration + // ---------------------------------------------------------------------- + + // add groupby to the search view register_groupby: function() { - // var self = this, - // query = this.search_view.query; - - // 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); - // }); - // var cols = _.map(this.graph_widget.pivot.cols.groupby, function (group) { - // return make_facet('ColGroupBy', group); - // }); - - // query.reset(rows.concat(cols)); - - // function make_facet (category, fields) { - // var values, - // icon, - // backbone_field, - // cat_name; - // if (!(fields instanceof Array)) { fields = [fields]; } - // if (category === 'GroupBy') { - // cat_name = 'group_by'; - // icon = 'w'; - // backbone_field = self.search_view._s_groupby; - // } else { - // cat_name = 'col_group_by'; - // icon = 'f'; - // backbone_field = self.search_field; - // } - // values = _.map(fields, function (field) { - // var context = {}; - // context[cat_name] = field; - // return {label: self.graph_widget.fields[field].string, value: {attrs:{domain: [], context: context}}}; - // }); - // return {category:category, values: values, icon:icon, field: backbone_field}; - // } + var query = this.search_view.query; + + if (!_.has(this.search_view, '_s_groupby')) { return; } + + var row_groupby = this.graph_widget.get_row_groupby(); + var gb_facet = this.make_groupby_facets(row_groupby); + var search_facet = query.findWhere({category:'GroupBy'}); + + if (search_facet) { + search_facet.values.reset(gb_facet.values); + } else { + if (row_groupby.length) { + query.add(gb_facet); + } + } }, + + make_groupby_facets: function(fields) { + var self = this, + values = _.map(fields, function (field) { + return { + label: self.graph_widget.fields[field].string, + value: {attrs:{domain: [], context: {group_by: field}}} + }; + }); + return {category:'GroupBy', values: values, icon:'w', field: self.search_view._s_groupby}; + }, + + search_field: { + get_context: function() {}, + get_domain: function () {}, + get_groupby: function () { }, + }, + }); |
