<commit_before>var React = require('react/addons');
var { Scroller } = require('scroller');
var DocumentTitle = require('react-document-title');
var Component = require('../component');
var TitleBar = require('../components/TitleBar');
var TouchableArea = require('../helpers/TouchableArea');
var Animated = require('../mixins/Animated');
var clone = require('../lib/niceClone');

// ViewLists are the most complex piece of the UI kit.
// Their usage is simple, but they manage a lot of state,
// encompass many animations, and also need to know about multiple
// child components (see TitleBar, View, Icon)

module.exports = {
  propTypes: {
    scrollToStep: React.PropTypes.number,
    disableScroll: React.PropTypes.bool,
    width: React.PropTypes.number,
    height: React.PropTypes.number,
    onTouchStart: React.PropTypes.func,
    onTouchEnd: React.PropTypes.func,
    onViewEntering: React.PropTypes.func,
    onViewEntered: React.PropTypes.func,
    onViewLeaving: React.PropTypes.func,
    onViewLeft: React.PropTypes.func,
    scrollerProps: React.PropTypes.object
  },

  getViewListInitialState() {
    return {
      // We put children in state, so when a parent removes a view
      // we can animate backwards, and then remove them from state
      children: this.props.children,
      width: this.props.width,
      height: this.props.height,
      step: this.props.scrollToStep || 0
    };
  },

  componentDidMount() {
    this.scroller = new Scroller(this.handleScroll, this.props.scrollerProps);
    this.setupDimensions();
    this.setScrollPosition();
    this.setupViewList(this.props);
    this.runViewCallbacks();
    window.addEventListener('resize', this.resize);
    this.didMount = true;
  },

  componentWillUnmount() {
    window.removeEventListener('resize', this.resize);
  },

  componentWillReceiveProps(nextProps) {
    if (nextProps.disableScroll) {
      this.disableAnimation();
      return;
    }
    // re-enable animations if disabled
    else if (this.props.disableScroll) {
      this.enableAnimation();
    }

    if (this._isAnimating || !this.didMount)
      return;

    // new scrollToStep
    if (nextProps.scrollToStep !== this.props.scrollToStep) {
      var isAdvancing = nextProps.scrollToStep >= this.state.step;
      if (isAdvancing) {
        this.setupViewList(nextProps);
        setTimeout(() => this.scrollToStep(nextProps.scrollToStep));
      }
      else
        this.scrollToStep(nextProps.scrollToStep, () => {
          this.setupViewList(nextProps);
        });
    }
    // else no new scroll position
    else {
      this.setupViewList(nextProps);
    }
  },

  // todo: this shouldn't need to do so much here
  // for now this fixes a bug where if you start with a step > 0
  setScrollPosition() {
    var step = this.state.step;

    // setTimeout because we are fighting Scroller
    setTimeout(() => {
      this.scroller.setPosition(step * this.state.width, 0);
      this.scroller.scrollTo(step * this.state.width, 0, false);
      this.setState({ step  });
    });
  },

  animationContext() {
    return {
      width: this.state.width
    };
  },

  // allow custom title bar heights
  getTitleBarHeight() {
    return this.props.titleBarProps.height || this.getConstant('titleBarHeight');
  },

  setupViewList(props) {
    var { width, height, children } = props;

    if (!children || !children.length)
      return;

    children = children.filter(child => !!child);

    this.setupViewEnterStates(children);
    this.scroller.setSnapSize(width, height);
    this.scroller.setDimensions(width, height, width * children.length, height);
    this.setState({ children });
  },

  // scrolls the viewList to a given step
  scrollToStep(step, cb) {
    if (step !== this.state.step) {
      this._isAnimating = true;
      this.scroller.scrollTo(this.state.width * step, 0, true);

      this.onViewEntered = () => {
        this.onViewEntered = null;
        this._isAnimating = false;
        if (cb) cb();
      };
    }
  },

  setupDimensions() {
    if (this.props.resizeWithWindow)
      this.setState({
        width: window.innerWidth,
        height: window.innerHeight
      });
  },

  resize() {
    this.setupDimensions();
    this.setScrollPosition();
  },

  setupViewEnterStates(children) {
    this.visibleViews = children.map(v => false);
    this.visibleViews[0] = true;
  },

  disableInitialScrollEvent: true,

  // this is called back from Scroller, each time the user scrolls
  handleScroll(left) {
    // this is a hack, but the Scroller lib fires a scroll event that
    // results in not respecting the props.scrollToStep on mount
    // need a better Scroller lib
    if (this.disableInitialScrollEvent) {
      this.disableInitialScrollEvent = false;
      return;
    }

    // disabled
    if (this.props.disableScroll)
      return;

    // don't scroll if we only have one view
    if (this.state.children.length === 1 && this.state.step === 0)
      return;

    var step = this.state.width ? left / this.state.width : 0;

    if (step !== this.state.step) {
      this.setState({ step });
      this.runViewCallbacks(step);
    }
  },
<commit_msg>visibleviews bugfix on one view<commit_after>var React = require('react/addons');
var { Scroller } = require('scroller');
var DocumentTitle = require('react-document-title');
var Component = require('../component');
var TitleBar = require('../components/TitleBar');
var TouchableArea = require('../helpers/TouchableArea');
var Animated = require('../mixins/Animated');
var clone = require('../lib/niceClone');

// ViewLists are the most complex piece of the UI kit.
// Their usage is simple, but they manage a lot of state,
// encompass many animations, and also need to know about multiple
// child components (see TitleBar, View, Icon)

module.exports = {
  propTypes: {
    scrollToStep: React.PropTypes.number,
    disableScroll: React.PropTypes.bool,
    width: React.PropTypes.number,
    height: React.PropTypes.number,
    onTouchStart: React.PropTypes.func,
    onTouchEnd: React.PropTypes.func,
    onViewEntering: React.PropTypes.func,
    onViewEntered: React.PropTypes.func,
    onViewLeaving: React.PropTypes.func,
    onViewLeft: React.PropTypes.func,
    scrollerProps: React.PropTypes.object
  },

  getViewListInitialState() {
    return {
      // We put children in state, so when a parent removes a view
      // we can animate backwards, and then remove them from state
      children: this.props.children,
      width: this.props.width,
      height: this.props.height,
      step: this.props.scrollToStep || 0
    };
  },

  componentDidMount() {
    this.scroller = new Scroller(this.handleScroll, this.props.scrollerProps);
    this.setupDimensions();
    this.setScrollPosition();
    this.setupViewList(this.props);
    this.runViewCallbacks();
    window.addEventListener('resize', this.resize);
    this.didMount = true;
  },

  componentWillUnmount() {
    window.removeEventListener('resize', this.resize);
  },

  componentWillReceiveProps(nextProps) {
    if (nextProps.disableScroll) {
      this.disableAnimation();
      return;
    }
    // re-enable animations if disabled
    else if (this.props.disableScroll) {
      this.enableAnimation();
    }

    if (this._isAnimating || !this.didMount)
      return;

    // new scrollToStep
    if (nextProps.scrollToStep !== this.props.scrollToStep) {
      var isAdvancing = nextProps.scrollToStep >= this.state.step;
      if (isAdvancing) {
        this.setupViewList(nextProps);
        setTimeout(() => this.scrollToStep(nextProps.scrollToStep));
      }
      else
        this.scrollToStep(nextProps.scrollToStep, () => {
          this.setupViewList(nextProps);
        });
    }
    // else no new scroll position
    else {
      this.setupViewList(nextProps);
    }
  },

  // todo: this shouldn't need to do so much here
  // for now this fixes a bug where if you start with a step > 0
  setScrollPosition() {
    var step = this.state.step;

    // setTimeout because we are fighting Scroller
    setTimeout(() => {
      this.scroller.setPosition(step * this.state.width, 0);
      this.scroller.scrollTo(step * this.state.width, 0, false);
      this.setState({ step  });
    });
  },

  animationContext() {
    return {
      width: this.state.width
    };
  },

  // allow custom title bar heights
  getTitleBarHeight() {
    return this.props.titleBarProps.height || this.getConstant('titleBarHeight');
  },

  setupViewList(props) {
    var { width, height, children } = props;
    this.setupViewEnterStates(children);

    if (!children || !children.length)
      return;

    children = children.filter(child => !!child);

    this.scroller.setSnapSize(width, height);
    this.scroller.setDimensions(width, height, width * children.length, height);
    this.setState({ children });
  },

  // scrolls the viewList to a given step
  scrollToStep(step, cb) {
    if (step !== this.state.step) {
      this._isAnimating = true;
      this.scroller.scrollTo(this.state.width * step, 0, true);

      this.onViewEntered = () => {
        this.onViewEntered = null;
        this._isAnimating = false;
        if (cb) cb();
      };
    }
  },

  setupDimensions() {
    if (this.props.resizeWithWindow)
      this.setState({
        width: window.innerWidth,
        height: window.innerHeight
      });
  },

  resize() {
    this.setupDimensions();
    this.setScrollPosition();
  },

  setupViewEnterStates(children) {
    if (!children || !children.length)
      this.visibleViews = [];
    else {
      this.visibleViews = children.map(v => false);
      this.visibleViews[0] = true;
    }
  },

  disableInitialScrollEvent: true,

  // this is called back from Scroller, each time the user scrolls
  handleScroll(left) {
    // this is a hack, but the Scroller lib fires a scroll event that
    // results in not respecting the props.scrollToStep on mount
    // need a better Scroller lib
    if (this.disableInitialScrollEvent) {
      this.disableInitialScrollEvent = false;
      return;
    }

    // disabled
    if (this.props.disableScroll)
      return;

    // don't scroll if we only have one view
    if (this.state.children.length === 1 && this.state.step === 0)
      return;

    var step = this.state.width ? left / this.state.width : 0;

    if (step !== this.state.step) {
      this.setState({ step });
      this.runViewCallbacks(step);
    }
  },

{'fraction_non_alphanumeric': 0.12430568856540893, 'fraction_numerical': 0.001723807699674392, 'mean_word_length': 2.8606284658040666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '6925926', 'n_tokens_mistral': 3169, 'n_tokens_neox': 3094, 'n_words': 753}