'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PageNavigation = undefined;

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _reactRouter = require('react-router');

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _PageNavigation = {
  "aside-page-navigation": "PageNavigation__aside-page-navigation___2HnQv",
  "article": "PageNavigation__article___g3i1l",
  "thumbnail": "PageNavigation__thumbnail___UfnvH",
  "title": "PageNavigation__title___2jfEj",
  "mobile": "PageNavigation__mobile___2vMxk",
  "previous": "PageNavigation__previous___34dNb",
  "arrow": "PageNavigation__arrow___yWjFC",
  "next": "PageNavigation__next___3HSOB",
  "arrow-right": "PageNavigation__arrow-right___9iRcf",
  "arrow-left": "PageNavigation__arrow-left___3ndUA",
  "no-thumbnail": "PageNavigation__no-thumbnail___2numj"
};

var _PageNavigation2 = _interopRequireDefault(_PageNavigation);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var PageNavigation = function (_Component) {
  _inherits(PageNavigation, _Component);

  function PageNavigation(props) {
    _classCallCheck(this, PageNavigation);

    var _this = _possibleConstructorReturn(this, (PageNavigation.__proto__ || Object.getPrototypeOf(PageNavigation)).call(this, props));

    _this.state = {
      article: props.article
    };
    return _this;
  }

  _createClass(PageNavigation, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      this.setState({
        article: nextProps.article
      });
    }
  }, {
    key: 'componentDidMount',
    value: function componentDidMount() {
      this._handleTitleTextOverflow();
    }
  }, {
    key: 'componentWillUpdate',
    value: function componentWillUpdate() {
      this._handleTitleTextOverflow();
    }
  }, {
    key: '_handleTitleTextOverflow',
    value: function _handleTitleTextOverflow() {
      var containerHeight = _lodash2.default.get(_reactDom2.default.findDOMNode(this.refs.container), 'clientHeight', 75);
      var thumbWidth = _lodash2.default.get(_reactDom2.default.findDOMNode(this.refs.thumb), 'width', 0);
      var titleNode = _reactDom2.default.findDOMNode(this.refs.title);
      if (!titleNode) {
        return;
      }
      var clientWidth = titleNode.clientWidth;
      var clientHeight = titleNode.clientHeight;
      var innerText = titleNode.innerText;

      if (!innerText) {
        return;
      }
      if (clientHeight > containerHeight) {
        var wordArea = Math.floor(clientHeight * clientWidth / innerText.length);
        // 0.67, which means we only want to use 2/3 area to display.
        // Otherwise, the text may overflow due to padding or margin.
        var words = Math.floor((clientWidth - thumbWidth) * (containerHeight * 0.67) / wordArea);
        titleNode.innerText = innerText.slice(0, words) + '...';
      }
    }
  }, {
    key: '_renderArticle',
    value: function _renderArticle(article) {
      var imgUrl = _lodash2.default.get(article, ['heroImage', 'image', 'resizeTargets', 'mobile', 'url'], null);
      var title = _lodash2.default.get(article, 'title', _lodash2.default.get(article, 'og_title'));

      return _react2.default.createElement(
        'article',
        { className: (0, _classnames2.default)(_PageNavigation2.default['article'], imgUrl ? '' : _PageNavigation2.default['no-thumbnail'], 'clearfix') },
        imgUrl ? _react2.default.createElement(
          'div',
          { className: _PageNavigation2.default['thumbnail'] },
          _react2.default.createElement('img', { ref: 'thumb', src: imgUrl, width: 65, height: 65 })
        ) : null,
        _react2.default.createElement(
          'div',
          { className: _PageNavigation2.default['title'] },
          _react2.default.createElement(
            'h3',
            { ref: 'title', title: title },
            title
          )
        )
      );
    }
  }, {
    key: '_renderArrow',
    value: function _renderArrow(navigate) {
      var direction = navigate === 'next' ? 'arrow-right' : 'arrow-left';
      return _react2.default.createElement('div', { className: (0, _classnames2.default)(_PageNavigation2.default['arrow'], _PageNavigation2.default[direction]) });
    }
  }, {
    key: 'render',
    value: function render() {
      var navigate = this.props.navigate;
      var article = this.state.article;

      var slug = _lodash2.default.get(article, 'slug');
      if (!slug) {
        return null;
      }

      var link = '/a/' + _lodash2.default.get(article, 'slug', '');

      return _react2.default.createElement(
        'nav',
        { ref: 'container', className: (0, _classnames2.default)(_PageNavigation2.default['aside-page-navigation'], _PageNavigation2.default[navigate]) },
        _react2.default.createElement(
          _reactRouter.Link,
          { to: link },
          this._renderArticle(article),
          this._renderArrow(navigate)
        )
      );
    }
  }]);

  return PageNavigation;
}(_react.Component);

PageNavigation.propTypes = {
  navigate: _react2.default.PropTypes.string,
  article: _react2.default.PropTypes.object
};

PageNavigation.defaultProps = {
  navigate: 'next',
  article: {}
};

exports.PageNavigation = PageNavigation;