<commit_before>var _ = require('underscore');

var BaseAnalysisFormModel = require('./base-analysis-form-model');
var template = require('./georeference-form.tpl');
var ColumnOptions = require('../column-options');
var GEOREFERENCE_TYPES = require('./georeference-types');

var TYPE_TO_META_MAP = {};
GEOREFERENCE_TYPES.map(function (d) {
  TYPE_TO_META_MAP[d.type] = d;
});

/**
 * Form model for a georeference analysis
 * It has a rather complicated schema, that depends on several data points and state.
 */
module.exports = BaseAnalysisFormModel.extend({
  parse: function (attrs) {
    return _.defaults(
      _.pick(attrs, 'id', 'source'), // maintain default attrs
      this._typeDef(attrs.type).parse(attrs)
    );
  },

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

    this._columnOptions = new ColumnOptions({}, {
      configModel: this._configModel,
      nodeDefModel: this._layerDefinitionModel.findAnalysisDefinitionNodeModel(this.get('source'))
    });

    this.listenTo(this._columnOptions, 'columnsFetched', this._setSchema);

    this.on('change:type', this._onTypeChanged, this);

    this._setSchema();
  },

  updateNodeDefinition: function (nodeDefModel) {
    var attrs = this._formatAttrs(this.attributes);
    nodeDefModel.clear({silent: true});
    nodeDefModel.set(attrs);
  },

  _formatAttrs: function (formAttrs) {
    var customFormattedFormAttrs = this._typeDef().formatAttrs(formAttrs, this._columnOptions);
    return BaseAnalysisFormModel.prototype._formatAttrs.call(this, customFormattedFormAttrs);
  },

  getTemplate: function () {
    return template;
  },

  getTemplateData: function () {
    return {parametersDataFields: this._typeDef().parametersDataFields};
  },

  _filterSchemaFieldsByType: function (schema) {
    // Always include the source field in addition to the type-specific fields
    var fields = ['source', 'type'].concat(this._typeDef().parametersDataFields.split(','));
    return _.pick(schema, fields);
  },

  /*
  /**
   * @override {BaseAnalysisFormModel._setSchema}
   */
  _setSchema: function () {
    BaseAnalysisFormModel.prototype._setSchema.call(this, this._filterSchemaFieldsByType({
      type: {
        type: 'Select',
        title: _t('editor.layers.analysis-form.type'),
        options: _.compact(
          GEOREFERENCE_TYPES.map(function (d) {
            if (!this._analyses.isAnalysisValidByType(d.type, this._configModel)) {
              return false;
            }

            return {
              val: d.type,
              label: d.label
            };
          }, this)
        )
      },
      source: {
        type: 'NodeDataset',
        text: _t('editor.layers.analysis-form.source'),
        options: this._getSourceOption(),
        editorAttrs: { disabled: true }
      },
      latitude: {
        type: 'Select',
        title: _t('editor.layers.analysis-form.latitude'),
        options: this._columnOptions.filterByType(['string', 'number']),
        validators: this._getFieldValidator('latitude'),
        placeholder: _t('editor.layers.analysis-form.georeference.select-latitude'),
        searchPlaceholder: _t('editor.layers.analysis-form.search-by-column-name')
      },
      longitude: {
        type: 'Select',
        title: _t('editor.layers.analysis-form.longitude'),
        options: this._columnOptions.filterByType(['string', 'number']),
        validators: this._getFieldValidator('longitude'),
        placeholder: _t('editor.layers.analysis-form.georeference.select-longitude'),
        searchPlaceholder: _t('editor.layers.analysis-form.search-by-column-name')
      },
      admin_region: {
        type: 'EnablerEditor',
        title: '',
        label: _t('editor.layers.analysis-form.admin-region'),
        help: _t('editor.layers.analysis-form.georeference.admin-region-help'),
        validators: this._getFieldValidator('admin_region'),
        editor: {
          type: 'Select',
          options: this._getColumnOptions('admin_region', 'string'),
          editorAttrs: this._getFieldAttrs('admin_region'),
          placeholder: _t('editor.layers.analysis-form.georeference.select-admin'),
          searchPlaceholder: _t('editor.layers.analysis-form.georeference.enter-region-name')
        }
      },
      admin_region_column: {
        type: 'Select',
        title: _t('editor.layers.analysis-form.admin-region'),
        options: this._columnOptions.filterByType('string'),
        help: _t('editor.layers.analysis-form.georeference.admin-region-help'),
        validators: this._getFieldValidator('admin_region'),
        editorAttrs: this._getFieldAttrs('admin_region')
      },
      country_column: {
        type: 'Select',
        title: _t('editor.layers.analysis-form.country'),
        options: this._columnOptions.filterByType('string'),
        help: _t('editor.layers.analysis-form.georeference.country-help'),
        validators: this._getFieldValidator('country_column'),
        editorAttrs: this._getFieldAttrs('country_column'),
        placeholder: _t('editor.layers.analysis-form.georeference.select-a-country'),
        searchPlaceholder: _t('editor.layers.analysis-form.search-by-column-name')
      },
      city: {
        type: 'EnablerEditor',
        title: '',
        label: _t('editor.layers.analysis-form.city'),
        help: _t('editor.layers.analysis-form.city-help'),
        validators: this._getFieldValidator('city'),<commit_msg>Admin region fixes<commit_after>var _ = require('underscore');

var BaseAnalysisFormModel = require('./base-analysis-form-model');
var template = require('./georeference-form.tpl');
var ColumnOptions = require('../column-options');
var GEOREFERENCE_TYPES = require('./georeference-types');

var TYPE_TO_META_MAP = {};
GEOREFERENCE_TYPES.map(function (d) {
  TYPE_TO_META_MAP[d.type] = d;
});

/**
 * Form model for a georeference analysis
 * It has a rather complicated schema, that depends on several data points and state.
 */
module.exports = BaseAnalysisFormModel.extend({
  parse: function (attrs) {
    return _.defaults(
      _.pick(attrs, 'id', 'source'), // maintain default attrs
      this._typeDef(attrs.type).parse(attrs)
    );
  },

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

    this._columnOptions = new ColumnOptions({}, {
      configModel: this._configModel,
      nodeDefModel: this._layerDefinitionModel.findAnalysisDefinitionNodeModel(this.get('source'))
    });

    this.listenTo(this._columnOptions, 'columnsFetched', this._setSchema);

    this.on('change:type', this._onTypeChanged, this);

    this._setSchema();
  },

  updateNodeDefinition: function (nodeDefModel) {
    var attrs = this._formatAttrs(this.attributes);
    nodeDefModel.clear({silent: true});
    nodeDefModel.set(attrs);
  },

  _formatAttrs: function (formAttrs) {
    var customFormattedFormAttrs = this._typeDef().formatAttrs(formAttrs, this._columnOptions);
    return BaseAnalysisFormModel.prototype._formatAttrs.call(this, customFormattedFormAttrs);
  },

  getTemplate: function () {
    return template;
  },

  getTemplateData: function () {
    return {parametersDataFields: this._typeDef().parametersDataFields};
  },

  _filterSchemaFieldsByType: function (schema) {
    // Always include the source field in addition to the type-specific fields
    var fields = ['source', 'type'].concat(this._typeDef().parametersDataFields.split(','));
    return _.pick(schema, fields);
  },

  /*
  /**
   * @override {BaseAnalysisFormModel._setSchema}
   */
  _setSchema: function () {
    BaseAnalysisFormModel.prototype._setSchema.call(this, this._filterSchemaFieldsByType({
      type: {
        type: 'Select',
        title: _t('editor.layers.analysis-form.type'),
        options: _.compact(
          GEOREFERENCE_TYPES.map(function (d) {
            if (!this._analyses.isAnalysisValidByType(d.type, this._configModel)) {
              return false;
            }

            return {
              val: d.type,
              label: d.label
            };
          }, this)
        )
      },
      source: {
        type: 'NodeDataset',
        text: _t('editor.layers.analysis-form.source'),
        options: this._getSourceOption(),
        editorAttrs: { disabled: true }
      },
      latitude: {
        type: 'Select',
        title: _t('editor.layers.analysis-form.latitude'),
        options: this._columnOptions.filterByType(['string', 'number']),
        validators: this._getFieldValidator('latitude'),
        placeholder: _t('editor.layers.analysis-form.georeference.select-latitude'),
        searchPlaceholder: _t('editor.layers.analysis-form.search-by-column-name')
      },
      longitude: {
        type: 'Select',
        title: _t('editor.layers.analysis-form.longitude'),
        options: this._columnOptions.filterByType(['string', 'number']),
        validators: this._getFieldValidator('longitude'),
        placeholder: _t('editor.layers.analysis-form.georeference.select-longitude'),
        searchPlaceholder: _t('editor.layers.analysis-form.search-by-column-name')
      },
      admin_region: {
        type: 'EnablerEditor',
        title: '',
        label: _t('editor.layers.analysis-form.admin-region'),
        help: _t('editor.layers.analysis-form.georeference.admin-region-help'),
        validators: this._getFieldValidator('admin_region'),
        editor: {
          type: 'Select',
          options: this._getColumnOptions('admin_region', 'string'),
          editorAttrs: this._getFieldAttrs('admin_region'),
          placeholder: _t('editor.layers.analysis-form.georeference.select-admin'),
          searchPlaceholder: _t('editor.layers.analysis-form.georeference.enter-region-name')
        }
      },
      admin_region_column: {
        type: 'Select',
        title: _t('editor.layers.analysis-form.admin-region'),
        options: this._columnOptions.filterByType('string'),
        help: _t('editor.layers.analysis-form.georeference.admin-region-help'),
        validators: this._getFieldValidator('admin_region'),
        editorAttrs: this._getFieldAttrs('admin_region'),
        placeholder: _t('editor.layers.analysis-form.georeference.select-admin'),
        searchPlaceholder: _t('editor.layers.analysis-form.search-by-column-name')
      },
      country_column: {
        type: 'Select',
        title: _t('editor.layers.analysis-form.country'),
        options: this._columnOptions.filterByType('string'),
        help: _t('editor.layers.analysis-form.georeference.country-help'),
        validators: this._getFieldValidator('country_column'),
        editorAttrs: this._getFieldAttrs('country_column'),
        placeholder: _t('editor.layers.analysis-form.georeference.select-a-country'),
        searchPlaceholder: _t('editor.layers.analysis-form.search-by-column-name')
      },
      city: {
        type: 'EnablerEditor',
        title: '',
        label: _t('editor.layers.analysis-form.city'),
        help: _t('editor.layers.analysis-form.city-help'),
        validators: this._getFieldValidator('city'),
{'fraction_non_alphanumeric': 0.13851138511385114, 'fraction_numerical': 0.0, 'mean_word_length': 3.18058690744921, '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': '3116490', 'n_tokens_mistral': 3218, 'n_tokens_neox': 3192, 'n_words': 499}