<commit_before>/*---------------------------------------------------------
 * OpenERP web_graph
 *---------------------------------------------------------*/

/* jshint undef: false  */

openerp.web_graph = function (instance) {
'use strict';

var _lt = instance.web._lt;
var _t = instance.web._t;

instance.web.views.add('graph', 'instance.web_graph.GraphView');

instance.web_graph.GraphView = instance.web.View.extend({
    display_name: _lt('Graph'),
    view_type: 'graph',

    // ----------------------------------------------------------------------
    // Init stuff
    // ----------------------------------------------------------------------
    init: function(parent, dataset, view_id, options) {
        var self = this;
        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.col_search_field = {
            get_context: function() { return { col_group_by: self.graph_widget.get_col_groupbys()};},
            get_domain: function () {},
            get_groupby: function () {},
        };
    },

    view_loading: function (fields_view_get) {
        var self = this,
            arch = fields_view_get.arch;

        this.widget_config = {
            title: arch.attrs.string,
            stacked : (arch.attrs.stacked === 'True'),
            mode: arch.attrs.type || 'bar',
            measures: [],
            row_groupby: [],
            col_groupby: [],
            graph_view: this,
        };

        _.each(arch.children, function (field) {
            var field_name = field.attrs.name;
            if (_.has(field.attrs, 'interval')) {
                field_name = field.attrs.name + ':' + field.attrs.interval;
            }
            if (_.has(field.attrs, 'type')) {
                switch (field.attrs.type) {
                    case 'row':
                        self.widget_config.row_groupby.push(field_name);
                        break;
                    case 'col':
                        self.widget_config.col_groupby.push(field_name);
                        break;
                    case 'measure':
                        self.widget_config.measures.push(field_name);
                        break;
                }
            } else {  // old style, kept for backward compatibility
                if ('operator' in field.attrs) {
                    self.widget_config.measures.push(field_name);
                } else {
                    self.widget_config.row_groupby.push(field_name);
                }
            }
        });
        if (self.widget_config.measures.length === 0) {
            self.widget_config.measures.push('__count');
        }
    },

    do_search: function (domain, context, group_by) {
        var self = this,
            col_group_by = this.extract_groupby('col_group_by', context) || [];

        if (!this.graph_widget) {
            if (group_by.length) {
                this.widget_config.row_groupby = group_by;
            }
            if (col_group_by.length) {
                this.widget_config.col_groupby = col_group_by;
            }
            this.graph_widget = new openerp.web_graph.Graph(this, this.model, domain, this.widget_config);
            this.graph_widget.appendTo(this.$el);
            this.ViewManager.on('switch_mode', this, function (e) {
                var col_gb = self.get_groupbys_from_searchview('ColGroupBy', 'col_group_by'),
                    row_gb = self.get_groupbys_from_searchview('GroupBy', 'group_by');

                if (e === 'graph') this.graph_widget.set(domain, row_gb, col_gb);
            });
            return;
        }

        this.graph_widget.set(domain, group_by, col_group_by);
    },

    extract_groupby: function (cat_field, context) {
        context = (_.isString(context)) ? py.eval(context) : context;
        return context[cat_field];
    },

    get_groupbys_from_searchview: function (cat_name, cat_field) {
        var self=this,
            facet = this.search_view.query.findWhere({category:cat_name}),
            groupby_list = facet ? facet.values.models : [];
        return _.map(groupby_list, function (g) { 
            var context = g.attributes.value.attrs.context;
            return self.extract_groupby(cat_field, context);
        });
    },

    do_show: function () {
        this.do_push_state({});
        return this._super();
    },

    // ----------------------------------------------------------------------
    // Search view integration
    // ----------------------------------------------------------------------

    // add groupby to the search view
    register_groupby: function(row_groupby, col_groupby) {
        var query = this.search_view.query;

        if (!_.has(this.search_view, '_s_groupby')) { return; }

        // add row groupbys
        var row_facet = this.make_row_groupby_facets(row_groupby),
            row_search_facet = query.findWhere({category:'GroupBy'});

        if (row_search_facet) {
            row_search_facet.values.reset(row_facet.values);
        } else {
            if (row_groupby.length) {
                query.add(row_facet);
            }
        }
         // add col groupbys
        var col_facet = this.make_col_groupby_facets(col_groupby),
            col_search_facet = query.findWhere({category:'ColGroupBy'});

        if (col_search_facet) {
            col_search_facet.values.reset(col_facet.values);
        } else {
            if (col_groupby.length) {
                query.add(col_facet);<commit_msg>[FIX] fixes a bug caused by two consecutive calls to do_search, with incomplete informations in the first call.  These calls then triggered useless queries and caused an incorrect (but brief) display of partial information in graph view (addon web_graph)<commit_after>/*---------------------------------------------------------
 * OpenERP web_graph
 *---------------------------------------------------------*/

/* jshint undef: false  */

openerp.web_graph = function (instance) {
'use strict';

var _lt = instance.web._lt;
var _t = instance.web._t;

instance.web.views.add('graph', 'instance.web_graph.GraphView');

instance.web_graph.GraphView = instance.web.View.extend({
    display_name: _lt('Graph'),
    view_type: 'graph',

    // ----------------------------------------------------------------------
    // Init stuff
    // ----------------------------------------------------------------------
    init: function(parent, dataset, view_id, options) {
        var self = this;
        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.col_search_field = {
            get_context: function() { return { col_group_by: self.graph_widget.get_col_groupbys()};},
            get_domain: function () {},
            get_groupby: function () {},
        };
    },

    view_loading: function (fields_view_get) {
        var self = this,
            arch = fields_view_get.arch;

        this.widget_config = {
            title: arch.attrs.string,
            stacked : (arch.attrs.stacked === 'True'),
            mode: arch.attrs.type || 'bar',
            measures: [],
            row_groupby: [],
            col_groupby: [],
            graph_view: this,
        };

        _.each(arch.children, function (field) {
            var field_name = field.attrs.name;
            if (_.has(field.attrs, 'interval')) {
                field_name = field.attrs.name + ':' + field.attrs.interval;
            }
            if (_.has(field.attrs, 'type')) {
                switch (field.attrs.type) {
                    case 'row':
                        self.widget_config.row_groupby.push(field_name);
                        break;
                    case 'col':
                        self.widget_config.col_groupby.push(field_name);
                        break;
                    case 'measure':
                        self.widget_config.measures.push(field_name);
                        break;
                }
            } else {  // old style, kept for backward compatibility
                if ('operator' in field.attrs) {
                    self.widget_config.measures.push(field_name);
                } else {
                    self.widget_config.row_groupby.push(field_name);
                }
            }
        });
        if (self.widget_config.measures.length === 0) {
            self.widget_config.measures.push('__count');
        }
    },

    do_search: function (domain, context, group_by) {
        if (this.ignore_do_search) {
            this.ignore_do_search = false;
            return;
        }

        var self = this,
            col_group_by = this.extract_groupby('col_group_by', context) || [];

        if (!this.graph_widget) {
            if (group_by.length) {
                this.widget_config.row_groupby = group_by;
            }
            if (col_group_by.length) {
                this.widget_config.col_groupby = col_group_by;
            }
            this.graph_widget = new openerp.web_graph.Graph(this, this.model, domain, this.widget_config);
            this.graph_widget.appendTo(this.$el);
            this.ViewManager.on('switch_mode', this, function (e) {
                var col_gb = self.get_groupbys_from_searchview('ColGroupBy', 'col_group_by'),
                    row_gb = self.get_groupbys_from_searchview('GroupBy', 'group_by');

                if (e === 'graph') this.graph_widget.set(domain, row_gb, col_gb);
            });
            return;
        }

        this.graph_widget.set(domain, group_by, col_group_by);
    },

    extract_groupby: function (cat_field, context) {
        context = (_.isString(context)) ? py.eval(context) : context;
        return context[cat_field];
    },

    get_groupbys_from_searchview: function (cat_name, cat_field) {
        var self=this,
            facet = this.search_view.query.findWhere({category:cat_name}),
            groupby_list = facet ? facet.values.models : [];
        return _.map(groupby_list, function (g) { 
            var context = g.attributes.value.attrs.context;
            return self.extract_groupby(cat_field, context);
        });
    },

    do_show: function () {
        this.do_push_state({});
        return this._super();
    },

    // ----------------------------------------------------------------------
    // Search view integration
    // ----------------------------------------------------------------------

    // add groupby to the search view
    register_groupby: function(row_groupby, col_groupby) {
        var query = this.search_view.query;

        if (!_.has(this.search_view, '_s_groupby')) { return; }

        if (row_groupby.length && col_groupby.length) {
            // when two changes to the search view will be done, the method do_search
            // will be called twice, once with the correct groupby and incorrect col_groupby,
            // and once with correct informations. This flag is necessary to prevent the 
            // incorrect informations to propagate and trigger useless queries
            this.ignore_do_search = true;
        }

        // add row groupbys
        var row_facet = this.make_row_groupby_facets(row_groupby),
            row_search_facet = query.findWhere({category:'GroupBy'});

        if (row_search_facet) {
            row_search_facet.values.reset(row_facet.values);
        } else {
            if (row_groupby.length) {
                query.add(row_facet);
            }
        }
         // add col groupbys
        var col_facet = this.make_col_groupby_facets(col_groupby),
            col_search_facet = query.findWhere({category:'ColGroupBy'});

        if (col_search_facet) {
            col_search_facet.values.reset(col_facet.values);
        } else {
            if (col_groupby.length) {
                query.add(col_facet);
{'fraction_non_alphanumeric': 0.17148521394349908, 'fraction_numerical': 0.00016520733520568312, 'mean_word_length': 1.5413518052057096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2129984', 'n_tokens_mistral': 3379, 'n_tokens_neox': 3256, 'n_words': 614}