<commit_before>var _ = require('underscore');
var BaseAnalysisFormModel = require('./base-analysis-form-model');
var template = require('./merge-form.tpl');
var ColumnOptions = require('../column-options');

var MERGE_FIELDS = 'join_operator,left_source_column,right_source_column,source_geometry_selector,left_source_columns,right_source_columns';

/**
 * Form model for the merge analysis
 */
module.exports = BaseAnalysisFormModel.extend({
  defaults: {
    join_operator: 'inner',
    source_geometry: 'left_source',
    right_source_columns: []
  },

  parse: function (attrs) {
    if (!attrs.source_geometry) {
      attrs.source_geometry_selector = attrs.left_source;
    } else {
      attrs.source_geometry_selector = attrs.right_source;
    }

    return attrs;
  },

  initialize: function () {
    BaseAnalysisFormModel.prototype.initialize.apply(this, arguments);

    this.on('change', this._onChange, this);

    this._leftColumnOptions = new ColumnOptions({}, {
      configModel: this._configModel
    });

    this._rightColumnOptions = new ColumnOptions({}, {
      configModel: this._configModel
    });

    this.listenTo(this._analysisSourceOptionsModel, 'change:fetching', this._setSchema);

    this.listenTo(this._leftColumnOptions, 'columnsFetched', this._setSchema);
    this.listenTo(this._rightColumnOptions, 'columnsFetched', this._setSchema);

    this.on('change:type', this._setSchema, this);
    this.on('change:right_source', this._onChangeRightSource, this);
    this.on('change:join_operator, change:right_source, change:left_source_column, change:right_source_column', this._setSchema, this);

    this._setSchema();
    this._fetchLeftColumns();
    this._fetchRightColumns();
  },

  _getFormFieldNames: function () {
    return MERGE_FIELDS;
  },

  _getFormatFieldNames: function () {
    return MERGE_FIELDS;
  },

  _formatAttrs: function (formAttrs) {
    var customFormattedFormAttrs = _.pick(formAttrs, ['id', 'type', 'left_source', 'right_source'].concat(this._getFormatFieldNames().split(',')));

    if (this.get('left_source') === this.get('source_geometry_selector')) {
      formAttrs.source_geometry = 'left_source';
    } else {
      formAttrs.source_geometry = 'right_source';
    }

    return BaseAnalysisFormModel.prototype._formatAttrs.call(this, customFormattedFormAttrs);
  },

  getTemplate: function () {
    return template;
  },

  getTemplateData: function () {
    return {
      right_source: this.get('right_source'),
      hasLeftAndRightSourceColumns: this.get('left_source_column') && this.get('right_source_column')
    };
  },

  _filterSchemaFieldsByType: function (schema) {
    // Always include the source and target fields in addition to the type-specific fields
    return _.pick(schema, ['left_source', 'right_source'].concat(this._getFormFieldNames().split(',')));
  },

  _onChangeRightSource: function () {
    this._fetchRightColumns();
    this.unset('right_source_column');<commit_msg>Closes #8572<commit_after>var _ = require('underscore');
var BaseAnalysisFormModel = require('./base-analysis-form-model');
var template = require('./merge-form.tpl');
var ColumnOptions = require('../column-options');

var MERGE_FIELDS = 'join_operator,left_source_column,right_source_column,source_geometry_selector,left_source_columns,right_source_columns';

/**
 * Form model for the merge analysis
 */
module.exports = BaseAnalysisFormModel.extend({
  defaults: {
    join_operator: 'inner',
    source_geometry: 'left_source',
    right_source_columns: []
  },

  parse: function (attrs) {
    if (!attrs.source_geometry) {
      attrs.source_geometry_selector = attrs.left_source;
    } else {
      attrs.source_geometry_selector = attrs.right_source;
    }

    return attrs;
  },

  initialize: function () {
    BaseAnalysisFormModel.prototype.initialize.apply(this, arguments);

    this.on('change', this._onChange, this);

    this._leftColumnOptions = new ColumnOptions({}, {
      configModel: this._configModel
    });

    this._rightColumnOptions = new ColumnOptions({}, {
      configModel: this._configModel
    });

    this.listenTo(this._analysisSourceOptionsModel, 'change:fetching', this._setSchema);

    this.listenTo(this._leftColumnOptions, 'columnsFetched', this._setSchema);
    this.listenTo(this._rightColumnOptions, 'columnsFetched', this._setSchema);

    this.on('change:type', this._setSchema, this);
    this.on('change:right_source', this._onChangeRightSource, this);
    this.on('change:join_operator, change:right_source, change:left_source_column, change:right_source_column', this._setSchema, this);

    this._setSchema();
    this._fetchLeftColumns();
    this._fetchRightColumns();
  },

  _getFormFieldNames: function () {
    return MERGE_FIELDS;
  },

  _getFormatFieldNames: function () {
    return MERGE_FIELDS;
  },

  _formatAttrs: function (formAttrs) {
    var customFormattedFormAttrs = _.pick(formAttrs, ['id', 'type', 'left_source', 'right_source'].concat(this._getFormatFieldNames().split(',')));

    if (this.get('left_source') === this.get('source_geometry_selector')) {
      formAttrs.source_geometry = 'left_source';
    } else {
      formAttrs.source_geometry = 'right_source';
    }

    if (!formAttrs.left_source_columns) {
      formAttrs.left_source_columns = [];
    }

    if (!formAttrs.right_source_columns) {
      formAttrs.right_source_columns = [];
    }

    return BaseAnalysisFormModel.prototype._formatAttrs.call(this, customFormattedFormAttrs);
  },

  getTemplate: function () {
    return template;
  },

  getTemplateData: function () {
    return {
      right_source: this.get('right_source'),
      hasLeftAndRightSourceColumns: this.get('left_source_column') && this.get('right_source_column')
    };
  },

  _filterSchemaFieldsByType: function (schema) {
    // Always include the source and target fields in addition to the type-specific fields
    return _.pick(schema, ['left_source', 'right_source'].concat(this._getFormFieldNames().split(',')));
  },

  _onChangeRightSource: function () {
    this._fetchRightColumns();
    this.unset('right_source_column');
{'fraction_non_alphanumeric': 0.1319093428990706, 'fraction_numerical': 0.0006522093592043046, 'mean_word_length': 4.526126126126126, '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': '265749', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1826, 'n_words': 298}