<commit_before>/** @jsx React.DOM */
var React = require('react/addons');
var cx = React.addons.classSet;

/**
 * Used to cancel events.
 */
var preventDefault = (ev) => ev.preventDefault();

var ListButton = React.createClass({

  render() {
    return (
      <li className={this.props.className}>
        <a href="#" onClick={this.props.event}>{this.props.children}</a>
      </li>
    );
  }

});

/**
 * Pagination component.
 */
var Pagination = React.createClass({

  mixins: [ React.addons.PureRenderMixin ],

  propTypes: {
    /**
     * Event to trigger. Receives the page number.
     */
    onChangePage: React.PropTypes.func.isRequired,

    /**
     * Total number of pages.
     */
    totalPages: React.PropTypes.number.isRequired,

    /**
     * Current page being displayed.
     */
    currentPage: React.PropTypes.number.isRequired,

    /**
     * The number of pages to show. 5 by default.
     */
    showPages: React.PropTypes.number
  },

  getDefaultProps() {
    return {
      showPages: 5
    };
  },

  onChangePage(pageNumber, event) {
    event.preventDefault();
    this.props.onChangePage(pageNumber);
  },

  render() {
    var {totalPages, showPages, currentPage} = this.props;
    var diff = Math.floor(showPages / 2),
        start = currentPage - diff,
        end = currentPage + diff + 1;

    // Edge cases
    if (totalPages < showPages) {
      start = 0;
      end = totalPages;
    } else if (start <= 0) {
      start = 0;
      end = showPages;
    } else if (end >= totalPages) {
      start = totalPages - showPages;
      end = totalPages;
    }

    var buttons = [], btnClass, btnEvent;
    for (var i = start; i < end; i++) {
      // If the button is for the current page then disable the event.
      if (currentPage === i) {
        btnClass = 'active';
        btnEvent = preventDefault;
      } else {
        btnClass = null;
        btnEvent = this.onChangePage.bind(this, i);
      }
      buttons.push(
        <ListButton
          key={i}
          className={btnClass}
          event={btnEvent}>
          {i + 1}
        </ListButton>
      );
    }

    // First and Prev button handlers and class
    var isFirst = currentPage === 0;
    var firstHandler = isFirst ? preventDefault : this.onChangePage.bind(this, 0);
    var firstClass = cx({
      'first': true,
      'disabled': isFirst
    });
    var prevHandler = isFirst ? preventDefault : this.onChangePage.bind(this, currentPage - 1);
    var prevClass = cx({
      'prev': true,
      'disabled': isFirst
    });

    // Next and Last button handlers and class
    var isLast = currentPage === (totalPages - 1);
    var nextHandler = isLast ? preventDefault : this.onChangePage.bind(this, currentPage + 1);
    var nextClass = cx({
      'next': true,
      'disabled': isLast
    });
    var lastHandler = isLast ? preventDefault : this.onChangePage.bind(this, totalPages - 1);
    var lastClass = cx({
      'last': true,
      'disabled': isLast
    });

    return (
      <ul className={this.props.className}>
        <ListButton
          className={firstClass}
          event={firstHandler}
        />
        <ListButton
          className={prevClass}
          event={prevHandler}
        />
        {buttons}
        <ListButton
          className={nextClass}
          event={nextHandler}
        />
        <ListButton
          className={lastClass}
          event={lastHandler}
        />
      </ul>
    );
  }
});

module.exports = Pagination;
<commit_msg>Hide pagination if no pages<commit_after>/** @jsx React.DOM */
var React = require('react/addons');
var cx = React.addons.classSet;

/**
 * Used to cancel events.
 */
var preventDefault = (ev) => ev.preventDefault();

var ListButton = React.createClass({

  render() {
    return (
      <li className={this.props.className}>
        <a href="#" onClick={this.props.event}>{this.props.children}</a>
      </li>
    );
  }

});

/**
 * Pagination component.
 */
var Pagination = React.createClass({

  mixins: [ React.addons.PureRenderMixin ],

  propTypes: {
    /**
     * Event to trigger. Receives the page number.
     */
    onChangePage: React.PropTypes.func.isRequired,

    /**
     * Total number of pages.
     */
    totalPages: React.PropTypes.number.isRequired,

    /**
     * Current page being displayed.
     */
    currentPage: React.PropTypes.number.isRequired,

    /**
     * The number of pages to show. 5 by default.
     */
    showPages: React.PropTypes.number
  },

  getDefaultProps() {
    return {
      showPages: 5
    };
  },

  onChangePage(pageNumber, event) {
    event.preventDefault();
    this.props.onChangePage(pageNumber);
  },

  render() {
    var {totalPages, showPages, currentPage} = this.props;

    if (totalPages === 0) {
      return null;
    }

    var diff = Math.floor(showPages / 2),
        start = currentPage - diff,
        end = currentPage + diff + 1;

    // Edge cases
    if (totalPages < showPages) {
      start = 0;
      end = totalPages;
    } else if (start <= 0) {
      start = 0;
      end = showPages;
    } else if (end >= totalPages) {
      start = totalPages - showPages;
      end = totalPages;
    }

    var buttons = [], btnClass, btnEvent;
    for (var i = start; i < end; i++) {
      // If the button is for the current page then disable the event.
      if (currentPage === i) {
        btnClass = 'active';
        btnEvent = preventDefault;
      } else {
        btnClass = null;
        btnEvent = this.onChangePage.bind(this, i);
      }
      buttons.push(
        <ListButton
          key={i}
          className={btnClass}
          event={btnEvent}>
          {i + 1}
        </ListButton>
      );
    }

    // First and Prev button handlers and class
    var isFirst = currentPage === 0;
    var firstHandler = isFirst ? preventDefault : this.onChangePage.bind(this, 0);
    var firstClass = cx({
      'first': true,
      'disabled': isFirst
    });
    var prevHandler = isFirst ? preventDefault : this.onChangePage.bind(this, currentPage - 1);
    var prevClass = cx({
      'prev': true,
      'disabled': isFirst
    });

    // Next and Last button handlers and class
    var isLast = currentPage === (totalPages - 1);
    var nextHandler = isLast ? preventDefault : this.onChangePage.bind(this, currentPage + 1);
    var nextClass = cx({
      'next': true,
      'disabled': isLast
    });
    var lastHandler = isLast ? preventDefault : this.onChangePage.bind(this, totalPages - 1);
    var lastClass = cx({
      'last': true,
      'disabled': isLast
    });

    return (
      <ul className={this.props.className}>
        <ListButton
          className={firstClass}
          event={firstHandler}
        />
        <ListButton
          className={prevClass}
          event={prevHandler}
        />
        {buttons}
        <ListButton
          className={nextClass}
          event={nextHandler}
        />
        <ListButton
          className={lastClass}
          event={lastHandler}
        />
      </ul>
    );
  }
});

module.exports = Pagination;

{'fraction_non_alphanumeric': 0.13013408609738886, 'fraction_numerical': 0.004093154551870148, 'mean_word_length': 2.0688609787786922, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 31, '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': '6228229', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2117, 'n_words': 529}