<commit_before>import React from 'react';
import autoBind from 'react-autobind';
import { bemComponents } from 'js/libs/reactBemComponents';
import { t } from '../../utils';
import { sluggify, txtid } from '../../../xlform/src/model.utils';
import { Map } from 'immutable';
import Select from 'react-select';
import alertify from 'alertifyjs';

const bem = bemComponents({
  Matrix: 'kobomatrix',
  MatrixCols: 'matrix-cols',
  MatrixCols__col: 'matrix-cols__col',
  MatrixCols__settings: 'matrix-cols__settings',
  MatrixCols__settings_inner: 'matrix-cols__settings_inner',
  MatrixCols__colattr: ['matrix-cols__colattr', '<span>'],
  MatrixItems: ['matrix-items', '<ul>'],
  MatrixItemsNewCol: ['matrix-items-new'],
  MatrixItems__item: ['matrix-items__item', '<li>'],
  MatrixItems__itemrow: ['matrix-items__itemrow'],
  MatrixItems__itemattr: ['matrix-items__itemattr', '<span>'],
  MatrixItems__itemsettings: ['matrix-items__itemsettings'],
  MatrixButton: ['kobomatrix-button', '<button>'],
});

class KoboMatrix extends React.Component {
  constructor (props) {
    super(props);
    this._listDetails = {};

    this.state = {
      data: props.model.data,
      kuid: props.model.kuid,
      kobomatrix_list: props.model.kobomatrix_list,
      expandedColKuid: false,
      expandedRowKuid: false,
      typeChoices: [
        {
          value: 'select_one',
          label: t('Select One')
        },
        {
          value: 'select_many',
          label: t('Select Many')
        },
        {
          value: 'text',
          label: t('Text')
        },
        {
          value: 'integer',
          label: t('Number')
        },
      ]
    };
    autoBind(this);
  }

  componentDidMount() {
    const data = this.state.data;
    const kuid = this.state.kuid;
    localStorage.setItem(`koboMatrix.${kuid}`, JSON.stringify(data.toJS()));

    // generate cols/rows for a new matrix
    if (data.get('cols').size < 1 && data.get('choices').size < 1) {
      this.generateDefault();
    }
  }

  generateDefault() {
    // TODO: find a better way to do this
    this.newColumn();
      window.setTimeout(()=>{
        this.newColumn();
        window.setTimeout(()=>{
          this.newChoiceOption(false);
        }, 500); 
      }, 500); 
  }

  expandColumn(colKuid) {
    if (this.state.expandedColKuid === colKuid )
      this.setState({expandedColKuid: false});
    else
      this.setState({expandedColKuid: colKuid, expandedRowKuid: false});
  }

  expandRow(rowKuid) {
    if (this.state.expandedRowKuid === rowKuid )
      this.setState({expandedRowKuid: false});
    else
      this.setState({expandedRowKuid: rowKuid, expandedColKuid: false});
  }


  autoName(val, type, ln = false) {
    var names = [];
    const kobomatrix_list = this.state.kobomatrix_list;
    var data = this.state.data;

    if (type === 'column') {
      data.get('cols').forEach(function(ch){
        names.push(data.getIn([ch, '$autoname']));
      });
    } else {
      data.get('choices').forEach(function(ch){
        if (ch.get('list_name') == ln)
          names.push(ch.get('$autovalue'));
      });
    }

    return sluggify(val, {
      preventDuplicates: names,
      lowerCase: true,
      lrstrip: true,
      preventDuplicateUnderscores: true,
      stripSpaces: true,
      characterLimit: 14,
      incrementorPadding: false,
      validXmlTag: false,
      replaceNonWordCharacters: false<commit_msg>exclude nonword characters in kobomatrix<commit_after>import React from 'react';
import autoBind from 'react-autobind';
import { bemComponents } from 'js/libs/reactBemComponents';
import { t } from '../../utils';
import { sluggify, txtid } from '../../../xlform/src/model.utils';
import { Map } from 'immutable';
import Select from 'react-select';
import alertify from 'alertifyjs';

const bem = bemComponents({
  Matrix: 'kobomatrix',
  MatrixCols: 'matrix-cols',
  MatrixCols__col: 'matrix-cols__col',
  MatrixCols__settings: 'matrix-cols__settings',
  MatrixCols__settings_inner: 'matrix-cols__settings_inner',
  MatrixCols__colattr: ['matrix-cols__colattr', '<span>'],
  MatrixItems: ['matrix-items', '<ul>'],
  MatrixItemsNewCol: ['matrix-items-new'],
  MatrixItems__item: ['matrix-items__item', '<li>'],
  MatrixItems__itemrow: ['matrix-items__itemrow'],
  MatrixItems__itemattr: ['matrix-items__itemattr', '<span>'],
  MatrixItems__itemsettings: ['matrix-items__itemsettings'],
  MatrixButton: ['kobomatrix-button', '<button>'],
});

class KoboMatrix extends React.Component {
  constructor (props) {
    super(props);
    this._listDetails = {};

    this.state = {
      data: props.model.data,
      kuid: props.model.kuid,
      kobomatrix_list: props.model.kobomatrix_list,
      expandedColKuid: false,
      expandedRowKuid: false,
      typeChoices: [
        {
          value: 'select_one',
          label: t('Select One')
        },
        {
          value: 'select_many',
          label: t('Select Many')
        },
        {
          value: 'text',
          label: t('Text')
        },
        {
          value: 'integer',
          label: t('Number')
        },
      ]
    };
    autoBind(this);
  }

  componentDidMount() {
    const data = this.state.data;
    const kuid = this.state.kuid;
    localStorage.setItem(`koboMatrix.${kuid}`, JSON.stringify(data.toJS()));

    // generate cols/rows for a new matrix
    if (data.get('cols').size < 1 && data.get('choices').size < 1) {
      this.generateDefault();
    }
  }

  generateDefault() {
    // TODO: find a better way to do this
    this.newColumn();
      window.setTimeout(()=>{
        this.newColumn();
        window.setTimeout(()=>{
          this.newChoiceOption(false);
        }, 500); 
      }, 500); 
  }

  expandColumn(colKuid) {
    if (this.state.expandedColKuid === colKuid )
      this.setState({expandedColKuid: false});
    else
      this.setState({expandedColKuid: colKuid, expandedRowKuid: false});
  }

  expandRow(rowKuid) {
    if (this.state.expandedRowKuid === rowKuid )
      this.setState({expandedRowKuid: false});
    else
      this.setState({expandedRowKuid: rowKuid, expandedColKuid: false});
  }


  autoName(val, type, ln = false) {
    var names = [];
    const kobomatrix_list = this.state.kobomatrix_list;
    var data = this.state.data;

    if (type === 'column') {
      data.get('cols').forEach(function(ch){
        names.push(data.getIn([ch, '$autoname']));
      });
    } else {
      data.get('choices').forEach(function(ch){
        if (ch.get('list_name') == ln)
          names.push(ch.get('$autovalue'));
      });
    }

    return sluggify(val, {
      preventDuplicates: names,
      lowerCase: true,
      lrstrip: true,
      preventDuplicateUnderscores: true,
      characterLimit: 14,
      incrementorPadding: false,
      validXmlTag: false,
      replaceNonWordCharacters: true
{'fraction_non_alphanumeric': 0.15059098205165622, 'fraction_numerical': 0.0029184298847220198, 'mean_word_length': 2.8745053702656866, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '2756755', 'n_tokens_mistral': 2259, 'n_tokens_neox': 2204, 'n_words': 419}