<commit_before>import React, {Component, PropTypes} from 'react'
import classnames from 'classnames'
import {connect} from 'react-redux'
import uuid from 'node-uuid'

import OnClickOutside from 'shared/components/OnClickOutside'
import AnnotationWindow from 'shared/components/AnnotationWindow'
import * as schema from 'shared/schemas'
import * as actions from 'shared/actions/annotations'

class NewAnnotation extends Component {
  state = {
    isMouseOver: false,
    gatherMode: 'startTime',
  }

  handleMouseDown = e => {
    const {tempAnnotation, dygraph, onUpdateAnnotation} = this.props

    const wrapperRect = this.wrapper.getBoundingClientRect()
    const trueGraphX = e.pageX - wrapperRect.left
    const startTime = `${dygraph.toDataXCoord(trueGraphX)}`

    onUpdateAnnotation({...tempAnnotation, startTime})
    this.setState({gatherMode: 'endTime'})
  }

  handleMouseMove = e => {
    if (this.props.isTempHovering === false) {
      return
    }

    const {dygraph, tempAnnotation, onUpdateAnnotation} = this.props
    const wrapperRect = this.wrapper.getBoundingClientRect()
    const trueGraphX = e.pageX - wrapperRect.left

    const newTime = `${dygraph.toDataXCoord(trueGraphX)}`

    if (this.state.gatherMode === 'startTime') {
      onUpdateAnnotation({
        ...tempAnnotation,
        startTime: newTime,
        endTime: newTime,
      })
    } else {
      onUpdateAnnotation({...tempAnnotation, endTime: newTime})
    }
  }

  handleMouseUp = e => {
    const {
      addAnnotationAsync,
      onAddingAnnotationSuccess,
      onMouseLeaveTempAnnotation,
    } = this.props

    const createUrl = this.context.source.links.annotations

    const {dygraph, tempAnnotation, onUpdateAnnotation} = this.props
    const wrapperRect = this.wrapper.getBoundingClientRect()
    const trueGraphX = e.pageX - wrapperRect.left
    const upTime = `${dygraph.toDataXCoord(trueGraphX)}`

    const downTime = tempAnnotation.startTime
    const [startTime, endTime] = [downTime, upTime].sort()

    const newAnnotation = {...tempAnnotation, startTime, endTime}
    onUpdateAnnotation(newAnnotation)
    addAnnotationAsync(createUrl, {...newAnnotation, id: uuid.v4()})
<commit_msg>Prevent user from creating annotations outside the visible graph<commit_after>import React, {Component, PropTypes} from 'react'
import classnames from 'classnames'
import {connect} from 'react-redux'
import uuid from 'node-uuid'

import OnClickOutside from 'shared/components/OnClickOutside'
import AnnotationWindow from 'shared/components/AnnotationWindow'
import * as schema from 'shared/schemas'
import * as actions from 'shared/actions/annotations'

class NewAnnotation extends Component {
  state = {
    isMouseOver: false,
    gatherMode: 'startTime',
  }

  enforceGraphBounds = newTime => {
    const {dygraph} = this.props
    const xRangeStart = dygraph.xAxisRange()[0]

    return newTime < xRangeStart ? `${xRangeStart}` : `${newTime}`
  }

  handleMouseDown = e => {
    const {tempAnnotation, dygraph, onUpdateAnnotation} = this.props

    const wrapperRect = this.wrapper.getBoundingClientRect()
    const trueGraphX = e.pageX - wrapperRect.left
    const startTime = this.enforceGraphBounds(dygraph.toDataXCoord(trueGraphX))

    onUpdateAnnotation({...tempAnnotation, startTime})
    this.setState({gatherMode: 'endTime'})
  }

  handleMouseMove = e => {
    if (this.props.isTempHovering === false) {
      return
    }

    const {dygraph, tempAnnotation, onUpdateAnnotation} = this.props
    const wrapperRect = this.wrapper.getBoundingClientRect()
    const trueGraphX = e.pageX - wrapperRect.left
    const newTime = this.enforceGraphBounds(dygraph.toDataXCoord(trueGraphX))

    if (this.state.gatherMode === 'startTime') {
      onUpdateAnnotation({
        ...tempAnnotation,
        startTime: newTime,
        endTime: newTime,
      })
    } else {
      onUpdateAnnotation({...tempAnnotation, endTime: newTime})
    }
  }

  handleMouseUp = e => {
    const {
      addAnnotationAsync,
      onAddingAnnotationSuccess,
      onMouseLeaveTempAnnotation,
    } = this.props

    const createUrl = this.context.source.links.annotations

    const {dygraph, tempAnnotation, onUpdateAnnotation} = this.props
    const wrapperRect = this.wrapper.getBoundingClientRect()
    const trueGraphX = e.pageX - wrapperRect.left
    const upTime = this.enforceGraphBounds(dygraph.toDataXCoord(trueGraphX))

    const downTime = tempAnnotation.startTime
    const [startTime, endTime] = [downTime, upTime].sort()

    const newAnnotation = {...tempAnnotation, startTime, endTime}
    onUpdateAnnotation(newAnnotation)
    addAnnotationAsync(createUrl, {...newAnnotation, id: uuid.v4()})

{'fraction_non_alphanumeric': 0.10591237771161208, 'fraction_numerical': 0.0006380263717566993, 'mean_word_length': 3.84346035015448, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 10, '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': '5205114', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1362, 'n_words': 307}