<commit_before>var React = require('react');
var Step = require('./step.jsx');
var NavigationButton = require('./components/navigationButton.jsx');
var NavigationBeads = require('./components/navigationBeads.jsx');
var SlideReel = require('./components/slideReel.jsx');
var classNames = require('classnames');

var EventsMixin = require('react-event-listener');

require('./react-step-wizard.scss');


var StepWizard = React.createClass({
  statics: {
    Step: Step,
  },

  mixins: [EventsMixin],

  listeners: {
    window: {
      popstate: 'navigateBack'
    }
  },

  getInitialState: function() {
    return {
      currentStepIndex: 0,
    };
  },

  getDefaultProps: function() {
    return {
      loopBeginning: true
    };
  },

  propTypes: {
    loopBeginning: React.PropTypes.bool
  },

  consts: {
    hideableClass: "sw-hidable",
  },

  componentWillMount: function() {
    this.validateChildren();
  },

  componentDidMount: function() {
    this.onMountHistoryFix();
  },

  onMountHistoryFix: function() {
    //IE 9 doesn't support altering history (otherwise IE 6 wouldn't have happened)
    if(!window.history.pushState) {
      return;
    }

    //This will reset the history when you navigate away from thise page and hit back
    if(window.history.state && window.history.state.currentStepIndex > 0) {
      window.history.go(-window.history.state.currentStepIndex);
      return;
    }

    window.history.replaceState({currentStepIndex: 0}, 'Step 0');
  },

  componentWillUnmount: function() {
    this.resetHistory();
  },

  navigateBack: function(event) {
    if(!event || !event.state) {
      return;
    }
    
    if(this.state.currentStepIndex === event.state.currentStepIndex) {
      return;
    }
    
    this.moveToPage(event.state.currentStepIndex);
  },

  resetHistory: function() {
    if(!window.history.pushState) {
      return;
    }

    window.history.go(-this.state.currentStepIndex);
  },

  pushState: function(index) {
    if(!window.history.pushState) {
      return;
    }

    if(index <= window.history.state.currentStepIndex) {
      return;
    }
    
    window.history.pushState({currentStepIndex: index}, 'Step ' + index);
  },

  validateChildren: function() {
    React.Children.forEach(this.props.children, function(child) {
      var type = child.type;

      if(typeof type !== "string") {
        type = type.displayName;
      }

      if(type !== "Step") {
        throw new Error(type + " is not a Step component");
      }
    });

    var childrenCount = React.Children.count(this.props.children);

    if(childrenCount <= 1) {
      throw new Error("You must supply more than 1 Step child");
    }
  },

  onClickNext: function() {
    var stepIndex = this.state.currentStepIndex;
    var maxIndex = React.Children.count(this.props.children) - 1;

    if(stepIndex === maxIndex && this.props.loopBeginning) {
      stepIndex = 0;
    } else {
      stepIndex = Math.min(stepIndex + 1, maxIndex);
    }

    this.moveToPage(stepIndex);
  },

  onClickPrev: function() {
    var stepIndex = this.state.currentStepIndex;

    stepIndex = Math.max(stepIndex - 1, 0);
    
    this.moveToPage(stepIndex);
  },

  executeStepCallbacks: function(start, end) {
    //I hate javascript scoping warnings
    var i, child;

    if(start < end) {
      for(i = start; i < end; i++) {
        child = this.props.children[i];

        child.props.onNext(i);
        this.pushState(i+1);
      }
    } else if (start > end) {
      for(i = start; i > end; i--) {
        child = this.props.children[i];

        child.props.onPrevious(i);
      }
    } else if (start === end) {
      child = this.props.children[start];
      child.props.onError(start);
    }
  },

  calculateFurthestIndex: function() {
    var minIndex = 0;
    var maxIndex = this.props.children.length;
    var i;

    for(i = minIndex; i < maxIndex; i++) {
      var child = this.props.children[i];

      if(!child.props.isValid) {
        return i;
      }
    }

    return i;
  },

  moveToPage: function(stepIndex) {
    var minIndex = 0;
    var maxIndex = React.Children.count(this.props.children) - 1;

    if(stepIndex > maxIndex) {<commit_msg>Added check to prevent errors from occuring on pill selection<commit_after>var React = require('react');
var Step = require('./step.jsx');
var NavigationButton = require('./components/navigationButton.jsx');
var NavigationBeads = require('./components/navigationBeads.jsx');
var SlideReel = require('./components/slideReel.jsx');
var classNames = require('classnames');

var EventsMixin = require('react-event-listener');

require('./react-step-wizard.scss');


var StepWizard = React.createClass({
  statics: {
    Step: Step,
  },

  mixins: [EventsMixin],

  listeners: {
    window: {
      popstate: 'navigateBack'
    }
  },

  getInitialState: function() {
    return {
      currentStepIndex: 0,
    };
  },

  getDefaultProps: function() {
    return {
      loopBeginning: true
    };
  },

  propTypes: {
    loopBeginning: React.PropTypes.bool
  },

  consts: {
    hideableClass: "sw-hidable",
  },

  componentWillMount: function() {
    this.validateChildren();
  },

  componentDidMount: function() {
    this.onMountHistoryFix();
  },

  onMountHistoryFix: function() {
    //IE 9 doesn't support altering history (otherwise IE 6 wouldn't have happened)
    if(!window.history.pushState) {
      return;
    }

    //This will reset the history when you navigate away from thise page and hit back
    if(window.history.state && window.history.state.currentStepIndex > 0) {
      window.history.go(-window.history.state.currentStepIndex);
      return;
    }

    window.history.replaceState({currentStepIndex: 0}, 'Step 0');
  },

  componentWillUnmount: function() {
    this.resetHistory();
  },

  navigateBack: function(event) {
    if(!event || !event.state) {
      return;
    }
    
    if(this.state.currentStepIndex === event.state.currentStepIndex) {
      return;
    }
    
    this.moveToPage(event.state.currentStepIndex);
  },

  resetHistory: function() {
    if(!window.history.pushState) {
      return;
    }

    window.history.go(-this.state.currentStepIndex);
  },

  pushState: function(index) {
    if(!window.history.pushState) {
      return;
    }

    if(index <= window.history.state.currentStepIndex) {
      return;
    }
    
    window.history.pushState({currentStepIndex: index}, 'Step ' + index);
  },

  validateChildren: function() {
    React.Children.forEach(this.props.children, function(child) {
      var type = child.type;

      if(typeof type !== "string") {
        type = type.displayName;
      }

      if(type !== "Step") {
        throw new Error(type + " is not a Step component");
      }
    });

    var childrenCount = React.Children.count(this.props.children);

    if(childrenCount <= 1) {
      throw new Error("You must supply more than 1 Step child");
    }
  },

  onClickNext: function() {
    var stepIndex = this.state.currentStepIndex;
    var maxIndex = React.Children.count(this.props.children) - 1;

    if(stepIndex === maxIndex && this.props.loopBeginning) {
      stepIndex = 0;
    } else {
      stepIndex = Math.min(stepIndex + 1, maxIndex);
    }

    this.moveToPage(stepIndex);
  },

  onClickPrev: function() {
    var stepIndex = this.state.currentStepIndex;

    stepIndex = Math.max(stepIndex - 1, 0);
    
    this.moveToPage(stepIndex);
  },

  executeStepCallbacks: function(start, end) {
    //I hate javascript scoping warnings
    var i, child;

    if(start < end) {
      for(i = start; i < end; i++) {
        child = this.props.children[i];

        child.props.onNext(i);
        this.pushState(i+1);
      }
    } else if (start > end) {
      for(i = start; i > end; i--) {
        child = this.props.children[i];

        child.props.onPrevious(i);
      }
    } else if (start === end) {
      child = this.props.children[start];
      child.props.onError(start);
    }
  },

  calculateFurthestIndex: function() {
    var minIndex = 0;
    var maxIndex = this.props.children.length;
    var i;

    for(i = minIndex; i < maxIndex; i++) {
      var child = this.props.children[i];

      if(!child.props.isValid) {
        return i;
      }
    }

    return i;
  },

  moveToPage: function(stepIndex) {
    if(this.state.currentStepIndex === stepIndex) {
      return;
    }

    var minIndex = 0;
    var maxIndex = React.Children.count(this.props.children) - 1;

    if(stepIndex > maxIndex) {
{'fraction_non_alphanumeric': 0.13927773203152571, 'fraction_numerical': 0.003999529467121515, 'mean_word_length': 2.675745784695201, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '5799266', 'n_tokens_mistral': 2783, 'n_tokens_neox': 2673, 'n_words': 544}