<commit_before>import React from 'react';
import {Dom as DomUtils, classSet} from 'rc-util';
import Track from './Track';
import Handle from './Handle';
import Steps from './Steps';
import Marks from './Marks';

function noop() {
}

function isNotTouchEvent(e) {
  return e.touches.length > 1 || (e.type.toLowerCase() === 'touchend' && e.touches.length > 0);
}

function getTouchPosition(e) {
  return e.touches[0].pageX;
}

function getMousePosition(e) {
  return e.pageX;
}

function pauseEvent(e) {
  e.stopPropagation();
  e.preventDefault();
}

// This is an utility method, tries to get property, then defaultPropery with
// special check using 'in', because propery can be '0'
function propOrDefault(props, name, fallback) {
  const defaultName = 'default' + name.charAt(0).toUpperCase() + name.substring(1);
  const defaultValue = (defaultName in props ? props[defaultName] : fallback);
  return (name in props ? props[name] : defaultValue);
}

class Slider extends React.Component {
  constructor(props) {
    super(props);

    let upperBound;
    let lowerBound;
    const initialValue = props.range ? [0, 0] : 0;
    if (props.marks.length > 0) {
      const index = propOrDefault(props, 'index', initialValue);
      ({lowerBound, upperBound} = this.getBoundsFromIndex(index, props));
    } else {
      const value = propOrDefault(props, 'value', initialValue);
      if (props.range) {
        lowerBound = this.trimAlignValue(value[0]);
        upperBound = this.trimAlignValue(value[1]);
      } else {
        upperBound = this.trimAlignValue(value);
      }
    }

    let recent;
    if (props.range && upperBound === lowerBound) {
      if (lowerBound === props.max) {
        recent = 'lowerBound';
      }
      if (upperBound === props.min) {
        recent = 'upperBound';
      }
    } else {
      recent = 'upperBound';
    }

    this.state = {
      handle: null,
      recent: recent,
      upperBound: upperBound,
      // If Slider is not range, set `lowerBound` equal to `min`.
      lowerBound: (lowerBound || props.min),
    };
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.range) {
      const value = nextProps.value;
      if (value) {
        this.setState({
          upperBound: value[1],
          lowerBound: value[0],
        });
      }
    } else if ('value' in nextProps) {
      this.setState({
        upperBound: nextProps.value,
      });
    } else if ('index' in nextProps) {
      const index = ('index' in nextProps ? nextProps.index : nextProps.defaultIndex);
      this.setState(this.getBoundsFromIndex(index, nextProps));
    }
  }

  onMouseMove(e) {
    const position = getMousePosition(e);
    this.onMove(e, position);
  }

  onTouchMove(e) {
    if (isNotTouchEvent(e)) {
      this.end('touch');
      return;
    }

    const position = getTouchPosition(e);
    this.onMove(e, position);
  }

  onMove(e, position) {
    pauseEvent(e);
    const props = this.props;
    const state = this.state;

    const diffPosition = position - this.startPosition;
    const diffValue = diffPosition / this.getSliderLength() * (props.max - props.min);

    const value = this.trimAlignValue(this.startValue + diffValue);
    const oldValue = state[state.handle];
    if (value === oldValue) return;

    if (!('value' in props) && !('index' in props)) {
      this.setState({[state.handle]: value}, () => {
        this.triggerEvents('onChange', this.getValue());
      });
    }
  }

  onTouchStart(e) {
    if (isNotTouchEvent(e)) return;

    const position = getTouchPosition(e);
    this.onStart(position);
    this.addDocumentEvents('touch');
    pauseEvent(e);
  }

  onSliderMouseDown(e) {
    const position = getMousePosition(e);
    this.onStart(position);
    this.addDocumentEvents('mouse');
    pauseEvent(e);
  }

  onStart(position) {
    this.triggerEvents('onBeforeChange');

    const value = this.calcValueByPos(position);
    this.startValue = value;
    this.startPosition = position;

    const state = this.state;
    const {upperBound, lowerBound} = state;

    let valueNeedChanging = 'upperBound';
    if (this.props.range) {
      const isLowerBoundCloser = Math.abs(upperBound - value) > Math.abs(lowerBound - value);
      if (isLowerBoundCloser) {<commit_msg>feat: unify `onBeforeChange` `onChange` `onAfterChange`<commit_after>import React from 'react';
import {Dom as DomUtils, classSet} from 'rc-util';
import Track from './Track';
import Handle from './Handle';
import Steps from './Steps';
import Marks from './Marks';

function noop() {
}

function isNotTouchEvent(e) {
  return e.touches.length > 1 || (e.type.toLowerCase() === 'touchend' && e.touches.length > 0);
}

function getTouchPosition(e) {
  return e.touches[0].pageX;
}

function getMousePosition(e) {
  return e.pageX;
}

function pauseEvent(e) {
  e.stopPropagation();
  e.preventDefault();
}

// This is an utility method, tries to get property, then defaultPropery with
// special check using 'in', because propery can be '0'
function propOrDefault(props, name, fallback) {
  const defaultName = 'default' + name.charAt(0).toUpperCase() + name.substring(1);
  const defaultValue = (defaultName in props ? props[defaultName] : fallback);
  return (name in props ? props[name] : defaultValue);
}

class Slider extends React.Component {
  constructor(props) {
    super(props);

    let upperBound;
    let lowerBound;
    const initialValue = props.range ? [0, 0] : 0;
    if (props.marks.length > 0) {
      const index = propOrDefault(props, 'index', initialValue);
      ({lowerBound, upperBound} = this.getBoundsFromIndex(index, props));
    } else {
      const value = propOrDefault(props, 'value', initialValue);
      if (props.range) {
        lowerBound = this.trimAlignValue(value[0]);
        upperBound = this.trimAlignValue(value[1]);
      } else {
        upperBound = this.trimAlignValue(value);
      }
    }

    let recent;
    if (props.range && upperBound === lowerBound) {
      if (lowerBound === props.max) {
        recent = 'lowerBound';
      }
      if (upperBound === props.min) {
        recent = 'upperBound';
      }
    } else {
      recent = 'upperBound';
    }

    this.state = {
      handle: null,
      recent: recent,
      upperBound: upperBound,
      // If Slider is not range, set `lowerBound` equal to `min`.
      lowerBound: (lowerBound || props.min),
    };
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.range) {
      const value = nextProps.value;
      if (value) {
        this.setState({
          upperBound: value[1],
          lowerBound: value[0],
        });
      }
    } else if ('value' in nextProps) {
      this.setState({
        upperBound: nextProps.value,
      });
    } else if ('index' in nextProps) {
      const index = ('index' in nextProps ? nextProps.index : nextProps.defaultIndex);
      this.setState(this.getBoundsFromIndex(index, nextProps));
    }
  }

  onMouseMove(e) {
    const position = getMousePosition(e);
    this.onMove(e, position);
  }

  onTouchMove(e) {
    if (isNotTouchEvent(e)) {
      this.end('touch');
      return;
    }

    const position = getTouchPosition(e);
    this.onMove(e, position);
  }

  onMove(e, position) {
    pauseEvent(e);
    const props = this.props;
    const state = this.state;

    const diffPosition = position - this.startPosition;
    const diffValue = diffPosition / this.getSliderLength() * (props.max - props.min);

    const value = this.trimAlignValue(this.startValue + diffValue);
    const oldValue = state[state.handle];
    if (value === oldValue) return;

    if (!('value' in props) && !('index' in props)) {
      this.setState({[state.handle]: value}, () => {
        this.triggerEvents('onChange', this.getValue());
      });
    }
  }

  onTouchStart(e) {
    if (isNotTouchEvent(e)) return;

    const position = getTouchPosition(e);
    this.onStart(position);
    this.addDocumentEvents('touch');
    pauseEvent(e);
  }

  onSliderMouseDown(e) {
    const position = getMousePosition(e);
    this.onStart(position);
    this.addDocumentEvents('mouse');
    pauseEvent(e);
  }

  onStart(position) {
    this.triggerEvents('onBeforeChange', this.getValue());

    const value = this.calcValueByPos(position);
    this.startValue = value;
    this.startPosition = position;

    const state = this.state;
    const {upperBound, lowerBound} = state;

    let valueNeedChanging = 'upperBound';
    if (this.props.range) {
      const isLowerBoundCloser = Math.abs(upperBound - value) > Math.abs(lowerBound - value);
      if (isLowerBoundCloser) {
{'fraction_non_alphanumeric': 0.13686662783925452, 'fraction_numerical': 0.0032615026208503203, 'mean_word_length': 3.006532897806813, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '5473059', 'n_tokens_mistral': 2716, 'n_tokens_neox': 2683, 'n_words': 631}