<commit_before>import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Immutable from 'immutable';
import Remove from '../Remove';
import InputNumber from '../InputNumber';
import InputNumberWithInlineButton from '../InputNumber/InlineButton';

const { array, bool, number, string, shape, func, instanceOf } = PropTypes;


const emptyState = {
  // form fields
  labelId: '',
  positionFrom: '',
  positionTo: '',
  comment: '',
  displayTick: true,
  annotationId: null,
  // UI state
  displayRemoveForm: false,
  disablePositions: false,
};

class AnnotationForm extends Component {
  constructor(props, context) {
    super(props, context);

    this.state = emptyState;

    this.onSubmit = this.onSubmit.bind(this);
    this.onPositionFromChange = this.onPositionFromChange.bind(this);
    this.onPositionToChange = this.onPositionToChange.bind(this);
    this.onLabelChange = this.onLabelChange.bind(this);
    this.onCommentChange = this.onCommentChange.bind(this);
    this.onDisplayTickChange = this.onDisplayTickChange.bind(this);
    this.reset = this.reset.bind(this);
    this.toggleActionRemove = this.toggleActionRemove.bind(this);
    this.handleRemove = this.handleRemove.bind(this);
    this.onSwitchPositions = this.onSwitchPositions.bind(this);
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.current && null !== nextProps.current) {
      this.setState({
        labelId: nextProps.current.labelId,
        annotationId: nextProps.current.annotationId,
        comment: nextProps.current.annotation.comment,
        displayTick: nextProps.current.annotation.displayTick,
      });
    }

    const selections = nextProps.selections;

    if (0 === selections.length) {
      this.reset();
    } else if (1 === selections.length) {
      const selection = selections[0];

      if (undefined !== selection.from) {
        this.setState({
          disablePositions: false,
          positionFrom: selection.from + nextProps.positionFrom,
        });
      }

      if (undefined !== selection.to) {
        this.setState({
          disablePositions: false,
          positionTo: selection.to + nextProps.positionFrom,
        });
      }
    } else {
      this.setState({
        disablePositions: true,
        positionFrom: '',
        positionTo: '',
      });
    }
  }

  onSubmit(event) {
    event.preventDefault();

    this.props.selections.forEach((s) => {
      this.props.onSubmit(
        this.state.labelId,
        {
          positionFrom: s.from + this.props.positionFrom,
          positionTo: s.to + this.props.positionFrom,
          comment: this.state.comment,
          displayTick: this.state.displayTick,
        },
        this.state.annotationId,
      );
    });

    this.reset();
    this.props.onSubmitDone();
  }

  onPositionFromChange(event) {
    let positionFrom = parseInt(event.target.value, 10);

    if (isNaN(positionFrom)) {
      positionFrom = this.props.positionFrom;
    }

    this.props.updateSelectionFrom(positionFrom - this.props.positionFrom);
  }

  onPositionToChange(event) {
    let positionTo = parseInt(event.target.value, 10);

    if (isNaN(positionTo)) {
      positionTo = this.props.positionFrom;
    }

    this.props.updateSelectionTo(positionTo - this.props.positionFrom);
  }

  onSwitchPositions(event) {
    event.preventDefault();

    this.props.updateSelectionFrom(
      this.state.positionTo - this.props.positionFrom,
    );
    this.props.updateSelectionTo(
      this.state.positionFrom - this.props.positionFrom,
    );
  }

  onLabelChange(event) {
    this.setState({ labelId: event.target.value });
  }

  onCommentChange(event) {
    this.setState({ comment: event.target.value });
  }

  onDisplayTickChange() {
    this.setState({ displayTick: !this.state.displayTick });
  }

  toggleActionRemove() {
    this.setState({
      displayRemoveForm: !this.state.displayRemoveForm,
    });
  }

  handleRemove() {
    this.props.onRemove(this.state.labelId, this.state.annotationId);
    this.reset();
  }

  reset() {
    this.setState(emptyState);
  }

  render() {
    return (
      <div className="annotation-form">
        <form onSubmit={this.onSubmit}>
          <InputNumberWithInlineButton
            value={this.state.positionFrom}
            min={this.props.positionFrom}
            placeholder={'From'}
            onChange={this.onPositionFromChange}
            isDisabled={this.state.disablePositions}
            className="input-group-field"
            onClick={this.onSwitchPositions}
            titleText="Invert positions"
            hasButtonDisabled={this.state.positionFrom === this.state.positionTo}
          >
            <i className="fa fa-arrows-v" aria-hidden="true" />
          </InputNumberWithInlineButton>

          <InputNumber<commit_msg>Allow to delete annotations again<commit_after>import PropTypes from 'prop-types';
import React, { Component } from 'react';
import Immutable from 'immutable';
import Remove from '../Remove';
import InputNumber from '../InputNumber';
import InputNumberWithInlineButton from '../InputNumber/InlineButton';

const { array, bool, number, string, shape, func, instanceOf } = PropTypes;


const emptyState = {
  // form fields
  labelId: '',
  positionFrom: '',
  positionTo: '',
  comment: '',
  displayTick: true,
  annotationId: null,
  // UI state
  displayRemoveForm: false,
  disablePositions: false,
};

class AnnotationForm extends Component {
  constructor(props, context) {
    super(props, context);

    this.state = emptyState;

    this.onSubmit = this.onSubmit.bind(this);
    this.onPositionFromChange = this.onPositionFromChange.bind(this);
    this.onPositionToChange = this.onPositionToChange.bind(this);
    this.onLabelChange = this.onLabelChange.bind(this);
    this.onCommentChange = this.onCommentChange.bind(this);
    this.onDisplayTickChange = this.onDisplayTickChange.bind(this);
    this.reset = this.reset.bind(this);
    this.toggleActionRemove = this.toggleActionRemove.bind(this);
    this.handleRemove = this.handleRemove.bind(this);
    this.onSwitchPositions = this.onSwitchPositions.bind(this);
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.current && null !== nextProps.current) {
      this.setState({
        labelId: nextProps.current.labelId,
        annotationId: nextProps.current.annotationId,
        comment: nextProps.current.annotation.comment,
        displayTick: nextProps.current.annotation.displayTick,
      });
    }

    const selections = nextProps.selections;

    if (0 === selections.length) {
      this.reset();
    } else if (1 === selections.length) {
      const selection = selections[0];

      if (undefined !== selection.from) {
        this.setState({
          disablePositions: false,
          positionFrom: selection.from + nextProps.positionFrom,
        });
      }

      if (undefined !== selection.to) {
        this.setState({
          disablePositions: false,
          positionTo: selection.to + nextProps.positionFrom,
        });
      }
    } else {
      this.setState({
        disablePositions: true,
        positionFrom: '',
        positionTo: '',
      });
    }
  }

  onSubmit(event) {
    event.preventDefault();

    this.props.selections.forEach((s) => {
      this.props.onSubmit(
        this.state.labelId,
        {
          positionFrom: s.from + this.props.positionFrom,
          positionTo: s.to + this.props.positionFrom,
          comment: this.state.comment,
          displayTick: this.state.displayTick,
        },
        this.state.annotationId,
      );
    });

    this.reset();
    this.props.onSubmitDone();
  }

  onPositionFromChange(event) {
    let positionFrom = parseInt(event.target.value, 10);

    if (isNaN(positionFrom)) {
      positionFrom = this.props.positionFrom;
    }

    this.props.updateSelectionFrom(positionFrom - this.props.positionFrom);
  }

  onPositionToChange(event) {
    let positionTo = parseInt(event.target.value, 10);

    if (isNaN(positionTo)) {
      positionTo = this.props.positionFrom;
    }

    this.props.updateSelectionTo(positionTo - this.props.positionFrom);
  }

  onSwitchPositions(event) {
    event.preventDefault();

    this.props.updateSelectionFrom(
      this.state.positionTo - this.props.positionFrom,
    );
    this.props.updateSelectionTo(
      this.state.positionFrom - this.props.positionFrom,
    );
  }

  onLabelChange(event) {
    this.setState({ labelId: event.target.value });
  }

  onCommentChange(event) {
    this.setState({ comment: event.target.value });
  }

  onDisplayTickChange() {
    this.setState({ displayTick: !this.state.displayTick });
  }

  toggleActionRemove(event) {
    event.preventDefault();

    this.setState((prevState) => ({
      displayRemoveForm: !prevState.displayRemoveForm,
    }));
  }

  handleRemove() {
    this.props.onRemove(this.state.labelId, this.state.annotationId);
    this.reset();
  }

  reset() {
    this.setState(emptyState);
  }

  render() {
    return (
      <div className="annotation-form">
        <form onSubmit={this.onSubmit}>
          <InputNumberWithInlineButton
            value={this.state.positionFrom}
            min={this.props.positionFrom}
            placeholder={'From'}
            onChange={this.onPositionFromChange}
            isDisabled={this.state.disablePositions}
            className="input-group-field"
            onClick={this.onSwitchPositions}
            titleText="Invert positions"
            hasButtonDisabled={this.state.positionFrom === this.state.positionTo}
          >
            <i className="fa fa-arrows-v" aria-hidden="true" />
          </InputNumberWithInlineButton>

          <InputNumber
{'fraction_non_alphanumeric': 0.1238183312782573, 'fraction_numerical': 0.0014385532264693795, 'mean_word_length': 2.8273692489186, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, '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': '7648749', 'n_tokens_mistral': 2839, 'n_tokens_neox': 2806, 'n_words': 460}