<commit_before>/*---------------------------------------------------------
 * Odoo Pivot Table view
 *---------------------------------------------------------*/

(function () {
'use strict';

var instance = openerp,
    _lt = instance.web._lt,
    _t = instance.web._t,
    QWeb = instance.web.qweb,
    format_value = instance.web.format_value,
    total = _t("Total");

instance.web.views.add('pivot', 'instance.web.PivotView');

instance.web.PivotView = instance.web.View.extend({
    template: 'PivotView',
    display_name: _lt('Pivot'),
    view_type: 'pivot',
    events: {
        'click .oe-opened': 'on_open_header_click',
        'click .oe-closed': 'on_closed_header_click',
        'click .o-field-menu': 'on_field_menu_selection',
        'click td': 'on_cell_click',
        'click .measure-row': 'on_measure_row_click',
    },

    init: function(parent, dataset, view_id, options) {
        this._super(parent, dataset, view_id, options);
        this.model = new instance.web.Model(dataset.model, {group_by_no_leaf: true});

        this.$sidebar = options.$sidebar;
        this.fields = {};
        this.measures = {};
        this.groupable_fields = {};
        this.ready = false; // will be ready after the first do_search
        this.data_loaded = $.Deferred();
        this.title = options.title;

        this.main_row = {
            root: undefined,
            groupbys: [],
        };
        this.main_col = {
            root: undefined,
            groupbys: [],
        };
        this.initial_col_groupby = [];
        this.initial_row_groupby = [];

        this.active_measures = [];
        this.headers = {};
        this.cells = {};
        this.has_data = false;

        this.last_header_selected = null;
        this.sorted_column = {};
    },
    start: function () {
        var self = this;
        this.$table_container = this.$('.o-pivot-table');

        var load_fields = this.model.call('fields_get', [])
                .then(this.prepare_fields.bind(this));

        if (this.$sidebar) {
            openerp.session.rpc('/web/pivot/check_xlwt').then(function (result) {
                if (result) {
                    self.sidebar = new instance.web.Sidebar(self);
                    self.sidebar.appendTo(self.$sidebar);
                    self.sidebar.add_items('other', [{
                        label: _t("Download xls"),
                        callback: self.download_table.bind(self)
                    }]);
                }
            });
        }
        return $.when(this._super(), load_fields).then(this.render_buttons.bind(this));
    },
    render_buttons: function () {
        var self = this;
        var context = {measures: _.pairs(_.omit(this.measures, '__count__'))};
        this.$buttons.html(QWeb.render('PivotView.buttons', context));
        this.$buttons.click(this.on_button_click.bind(this));
        this.active_measures.forEach(function (measure) {
            self.$buttons.find('li[data-field="' + measure + '"]').addClass('selected');
        });

        var another_ctx = {fields: _.chain(this.groupable_fields).pairs().sortBy(function(f){return f[1].string;}).value()};
        this.$field_selection = this.$('.o-field-selection');
        this.$field_selection.html(QWeb.render('PivotView.FieldSelection', another_ctx));
        openerp.web.bus.on('click', self, function () {
            self.$field_selection.find('ul').first().hide();
        });
        this.$buttons.find('button').tooltip();
    },
    view_loading: function (fvg) {
        var self = this;
        this.title = this.title || fvg.arch.attrs.string;
        this.enable_linking = !fvg.arch.attrs.disable_linking;
        this.$el.toggleClass('oe-enable-linking', this.enable_linking);
        fvg.arch.children.forEach(function (field) {
            var name = field.attrs.name;
            if (field.attrs.interval) {
                name += ':' + field.attrs.interval;
            }
            //noinspection FallThroughInSwitchStatementJS
            switch (field.attrs.type) {
            case 'measure':
                self.active_measures.push(name);
                break;
            case 'col':
                self.initial_col_groupby.push(name);
                break;
            default:
                if ('operator' in field.attrs) {
                    self.active_measures.push(name);
                    break;
                }
            case 'row':
                self.initial_row_groupby.push(name);
            }
        });
        if ((!self.active_measures.length) || fvg.arch.attrs.display_quantity){
            self.active_measures.push('__count__');
        }
    },
    prepare_fields: function (fields) {
        var self = this,
            groupable_types = ['many2one', 'char', 'boolean', 
                               'selection', 'date', 'datetime'];
        this.fields = fields;
        _.each(fields, function (field, name) {
            if ((name !== 'id') && (field.store === true)) {
                if (field.type === 'integer' || field.type === 'float') {
                    self.measures[name] = field;
                }
                if (_.contains(groupable_types, field.type)) {
                    self.groupable_fields[name] = field;
                }
            }
        });
        this.measures.__count__ = {string: "Quantity", type: "integer"};<commit_msg>[IMP] web: pivot view should allow explicitely defined measures<commit_after>/*---------------------------------------------------------
 * Odoo Pivot Table view
 *---------------------------------------------------------*/

(function () {
'use strict';

var instance = openerp,
    _lt = instance.web._lt,
    _t = instance.web._t,
    QWeb = instance.web.qweb,
    format_value = instance.web.format_value,
    total = _t("Total");

instance.web.views.add('pivot', 'instance.web.PivotView');

instance.web.PivotView = instance.web.View.extend({
    template: 'PivotView',
    display_name: _lt('Pivot'),
    view_type: 'pivot',
    events: {
        'click .oe-opened': 'on_open_header_click',
        'click .oe-closed': 'on_closed_header_click',
        'click .o-field-menu': 'on_field_menu_selection',
        'click td': 'on_cell_click',
        'click .measure-row': 'on_measure_row_click',
    },

    init: function(parent, dataset, view_id, options) {
        this._super(parent, dataset, view_id, options);
        this.model = new instance.web.Model(dataset.model, {group_by_no_leaf: true});

        this.$sidebar = options.$sidebar;
        this.fields = {};
        this.measures = {};
        this.groupable_fields = {};
        this.ready = false; // will be ready after the first do_search
        this.data_loaded = $.Deferred();
        this.title = options.title;

        this.main_row = {
            root: undefined,
            groupbys: [],
        };
        this.main_col = {
            root: undefined,
            groupbys: [],
        };
        this.initial_col_groupby = [];
        this.initial_row_groupby = [];

        this.active_measures = [];
        this.headers = {};
        this.cells = {};
        this.has_data = false;

        this.last_header_selected = null;
        this.sorted_column = {};
    },
    start: function () {
        var self = this;
        this.$table_container = this.$('.o-pivot-table');

        var load_fields = this.model.call('fields_get', [])
                .then(this.prepare_fields.bind(this));

        if (this.$sidebar) {
            openerp.session.rpc('/web/pivot/check_xlwt').then(function (result) {
                if (result) {
                    self.sidebar = new instance.web.Sidebar(self);
                    self.sidebar.appendTo(self.$sidebar);
                    self.sidebar.add_items('other', [{
                        label: _t("Download xls"),
                        callback: self.download_table.bind(self)
                    }]);
                }
            });
        }
        return $.when(this._super(), load_fields).then(this.render_buttons.bind(this));
    },
    render_buttons: function () {
        var self = this;
        var context = {measures: _.pairs(_.omit(this.measures, '__count__'))};
        this.$buttons.html(QWeb.render('PivotView.buttons', context));
        this.$buttons.click(this.on_button_click.bind(this));
        this.active_measures.forEach(function (measure) {
            self.$buttons.find('li[data-field="' + measure + '"]').addClass('selected');
        });

        var another_ctx = {fields: _.chain(this.groupable_fields).pairs().sortBy(function(f){return f[1].string;}).value()};
        this.$field_selection = this.$('.o-field-selection');
        this.$field_selection.html(QWeb.render('PivotView.FieldSelection', another_ctx));
        openerp.web.bus.on('click', self, function () {
            self.$field_selection.find('ul').first().hide();
        });
        this.$buttons.find('button').tooltip();
    },
    view_loading: function (fvg) {
        var self = this;
        this.title = this.title || fvg.arch.attrs.string;
        this.enable_linking = !fvg.arch.attrs.disable_linking;
        this.$el.toggleClass('oe-enable-linking', this.enable_linking);
        fvg.arch.children.forEach(function (field) {
            var name = field.attrs.name;
            if (field.attrs.interval) {
                name += ':' + field.attrs.interval;
            }
            //noinspection FallThroughInSwitchStatementJS
            switch (field.attrs.type) {
            case 'measure':
                self.active_measures.push(name);
                break;
            case 'col':
                self.initial_col_groupby.push(name);
                break;
            default:
                if ('operator' in field.attrs) {
                    self.active_measures.push(name);
                    break;
                }
            case 'row':
                self.initial_row_groupby.push(name);
            }
        });
        if ((!self.active_measures.length) || fvg.arch.attrs.display_quantity){
            self.active_measures.push('__count__');
        }
        // add active measures to the measure list.  This is very rarely necessary, but it
        // can be useful if one is working with a functional field non stored, but in a
        // model with an overrided read_group method.  In this case, the pivot view could
        // work, and the measure should be allowed.  However, be careful if you define a
        // measure in your pivot view: non stored functional fields will probably not work
        // (their aggregate will always be 0).
        _.each(this.active_measures, function (m) {
            if (!(m in self.measures)) {
                self.measures[m] = self.fields[m];
            }
        });
    },
    prepare_fields: function (fields) {
        var self = this,
            groupable_types = ['many2one', 'char', 'boolean', 
                               'selection', 'date', 'datetime'];
        this.fields = fields;
        _.each(fields, function (field, name) {
            if ((name !== 'id') && (field.store === true)) {
                if (field.type === 'integer' || field.type === 'float') {
                    self.measures[name] = field;
                }
                if (_.contains(groupable_types, field.type)) {
                    self.groupable_fields[name] = field;
                }
            }
        });
        this.measures.__count__ = {string: "Quantity", type: "integer"};
{'fraction_non_alphanumeric': 0.1585026923745006, 'fraction_numerical': 0.0004342539517109606, 'mean_word_length': 1.7793869176924935, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1869988', 'n_tokens_mistral': 3314, 'n_tokens_neox': 3172, 'n_words': 588}