<commit_before>'use strict';

$(function() {

    var TableModel = Backbone.Model.extend({

        defaults: {
            row_labels: [
                'Individual Income Tax Liability',
                'Payroll Tax Liability',
                'Combined Payroll and Individual Income Tax Liability'
            ],
            row_labels_change: [
                'Individual Income Tax Liability Change',
                'Payroll Tax Liability Change',
                'Combined Payroll and Individual Income Tax Liability Change'
            ],
            col_indices: [0,1,2,3,4,5,6,7,11,16,17,18]
        },

        initialize: function() {
            this.buildExtraColumnKeys();
            this.divideCellValues();
            this.columnVisibility();
        },

        buildExtraColumnKeys: function() {
            _.each(this.get('cols'), function(col) {
                col.divisor_label = '';
                if (col.divisor === 1000) {
                    col.divisor_label += 'Thousands';
                } else if (col.divisor === 1000000) {
                    col.divisor_label += 'Millions';
                } else if (col.divisor === 1000000000) {
                    col.divisor_label += 'Billions';
                }
                // col.divisor_label += col.units ? col.divisor_label ? ' of ' + col.units : col.units : ''; // this adds "of dollars", etc
                col.label_val = col.label.toString().toLowerCase().split(" ").join("-");
            });
        },

        divideCellValues: function() {
            var that = this;
            var i = 0;
            _.each(this.get('rows'), function(row) {
                var j = 0;
                _.each(row.cells, function(cell) {
                    if (_.isEmpty(cell.year_values)) {
                        that.get('rows')[i]['cells'][j]['tot_value'] = that.numberWithCommas((parseFloat(cell.value / cell.format.divisor)).toFixed(cell.format.decimals));
                    } else {
                        _.each(_.keys(cell.year_values), function(year) {
                            that.get('rows')[i]['cells'][j]['year_values'][year] = that.numberWithCommas((parseFloat(cell.year_values[year] / cell.format.divisor)).toFixed(cell.format.decimals));
                        });
                    }
                    j++;
                });
                i++;
            });
        },

        numberWithCommas: function(x) {
            var parts = x.toString().split(".");
            parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            return parts.join(".") === "NaN" ? 0 : parts.join(".");
        },

        columnVisibility: function() {
          var _self = this;
          _.map(this.get('col_indices'), function(col_idx) {
              var col = _self.get('cols')[col_idx];
              if (col) {
                col.show = true;
              }
              return col;
          });

        }
    });


    var TablesCollection = Backbone.Collection.extend({
        model: TableModel
    });


    var SelectedTableModel = Backbone.Model.extend({
        defaults: {
            difference: true,
            plan:'X',
            payroll_tax: true,
            income_tax: true,
            income: 'expanded',
            grouping: 'bin'
        },

        reference: function() {
            var grouping = '_' + this.get('grouping');
            if (this.get('difference') && !(this.get('payroll_tax') || this.get('income_tax'))) {
                return false;
            }
            var tax = (this.get('payroll_tax')) ? ((this.get('income_tax')) ? 'c' : 'p') : '';
            var table = (this.get('difference') ? tax + 'df' : 'm' + this.get('plan')) + '_' + this.get('grouping')
            return table;
        },

        rowLabels: function() {
            if (this.get('grouping') == 'bin') {
                return [
                    'Less than 10',
                    '10-20',
                    '20-30',
                    '30-40',
                    '40-50',
                    '50-75',
                    '75-100',
                    '100-200',
                    '200-500',
                    '500-1000',
                    '1000+',
                    'All'
                ];
            } else if (this.get('grouping') == 'dec') {
                return [
                    '0-10%',
                    '10-20%',
                    '20-30%',
                    '30-40%',
                    '40-50%',
                    '50-60%',
                    '60-70%',
                    '70-80%',
                    '80-90%',
                    '90-100%',
                    'All'
                ];
            }
        }
    });

    var TableView = Backbone.View.extend({
        template: _.template('\
            <div class="panel panel-default">\
                <div class="panel-body" style="overflow: scroll">\
                    <table class="table table-striped" style="width:100%">\
                        <thead>\
                            <tr>\<commit_msg>Update row names in taxbrain-tablebuilder.js<commit_after>'use strict';

$(function() {

    var TableModel = Backbone.Model.extend({

        defaults: {
            row_labels: [
                'Individual Income Tax Liability',
                'Payroll Tax Liability',
                'Combined Payroll and Individual Income Tax Liability'
            ],
            row_labels_change: [
                'Individual Income Tax Liability Change',
                'Payroll Tax Liability Change',
                'Combined Payroll and Individual Income Tax Liability Change'
            ],
            col_indices: [0,1,2,3,4,5,6,7,11,16,17,18]
        },

        initialize: function() {
            this.buildExtraColumnKeys();
            this.divideCellValues();
            this.columnVisibility();
        },

        buildExtraColumnKeys: function() {
            _.each(this.get('cols'), function(col) {
                col.divisor_label = '';
                if (col.divisor === 1000) {
                    col.divisor_label += 'Thousands';
                } else if (col.divisor === 1000000) {
                    col.divisor_label += 'Millions';
                } else if (col.divisor === 1000000000) {
                    col.divisor_label += 'Billions';
                }
                // col.divisor_label += col.units ? col.divisor_label ? ' of ' + col.units : col.units : ''; // this adds "of dollars", etc
                col.label_val = col.label.toString().toLowerCase().split(" ").join("-");
            });
        },

        divideCellValues: function() {
            var that = this;
            var i = 0;
            _.each(this.get('rows'), function(row) {
                var j = 0;
                _.each(row.cells, function(cell) {
                    if (_.isEmpty(cell.year_values)) {
                        that.get('rows')[i]['cells'][j]['tot_value'] = that.numberWithCommas((parseFloat(cell.value / cell.format.divisor)).toFixed(cell.format.decimals));
                    } else {
                        _.each(_.keys(cell.year_values), function(year) {
                            that.get('rows')[i]['cells'][j]['year_values'][year] = that.numberWithCommas((parseFloat(cell.year_values[year] / cell.format.divisor)).toFixed(cell.format.decimals));
                        });
                    }
                    j++;
                });
                i++;
            });
        },

        numberWithCommas: function(x) {
            var parts = x.toString().split(".");
            parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            return parts.join(".") === "NaN" ? 0 : parts.join(".");
        },

        columnVisibility: function() {
          var _self = this;
          _.map(this.get('col_indices'), function(col_idx) {
              var col = _self.get('cols')[col_idx];
              if (col) {
                col.show = true;
              }
              return col;
          });

        }
    });


    var TablesCollection = Backbone.Collection.extend({
        model: TableModel
    });


    var SelectedTableModel = Backbone.Model.extend({
        defaults: {
            difference: true,
            plan:'X',
            payroll_tax: true,
            income_tax: true,
            income: 'expanded',
            grouping: 'bin'
        },

        reference: function() {
            var grouping = '_' + this.get('grouping');
            if (this.get('difference') && !(this.get('payroll_tax') || this.get('income_tax'))) {
                return false;
            }
            var tax = (this.get('payroll_tax')) ? ((this.get('income_tax')) ? 'c' : 'p') : '';
            var table = (this.get('difference') ? tax + 'df' : 'm' + this.get('plan')) + '_' + this.get('grouping')
            return table;
        },

        rowLabels: function() {
            if (this.get('grouping') == 'bin') {
                return [
                    '<$10K',
                    '$10-20K',
                    '$20-30K',
                    '$30-40K',
                    '$40-50K',
                    '$50-75K',
                    '$75-100K',
                    '$100-200K',
                    '$200-500K',
                    '$500-1000K',
                    '>$1000K',
                    'All'
                ];
            } else if (this.get('grouping') == 'dec') {
                return [
                    '0-10',
                    '10-20',
                    '20-30',
                    '30-40',
                    '40-50',
                    '50-60',
                    '60-70',
                    '70-80',
                    '80-90',
                    '90-100',
                    'All'
                ];
            }
        }
    });

    var TableView = Backbone.View.extend({
        template: _.template('\
            <div class="panel panel-default">\
                <div class="panel-body" style="overflow: scroll">\
                    <table class="table table-striped" style="width:100%">\
                        <thead>\
                            <tr>\
{'fraction_non_alphanumeric': 0.14921259842519685, 'fraction_numerical': 0.026771653543307086, 'mean_word_length': 1.18751345532831, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '910456', 'n_tokens_mistral': 3034, 'n_tokens_neox': 2671, 'n_words': 481}