'use strict';

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

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 _dateTransformer = require('../lib/date-transformer');

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _Category = require('./Category');

var _Category2 = _interopRequireDefault(_Category);

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 ScrollMagic = void 0;

if (process.env.BROWSER) {
  ScrollMagic = require('ScrollMagic');
}

var IMG_RATIO = 0.667;

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

  function FeaturesItem(props, context) {
    _classCallCheck(this, FeaturesItem);

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

    _this._handleResize = _this._handleResize.bind(_this);

    // scrollController and scrollScene are used for ScrollMagic
    _this.scrollController = null;
    _this.scrollScene = null;
    return _this;
  }

  _createClass(FeaturesItem, [{
    key: '_resizeImage',
    value: function _resizeImage() {
      var img = _reactDom2.default.findDOMNode(this.refs.listImg);
      // browser width and height
      var bHeight = window.innerHeight;
      var bWidth = window.innerWidth;
      var bRatio = bHeight / bWidth;
      var height = void 0;
      var width = void 0;
      var marginTop = void 0;
      var marginLeft = void 0;

      // height of browser is longer than image height in ratio.
      // use browser height as criterion
      if (bRatio > IMG_RATIO) {
        height = bHeight;
        width = height / IMG_RATIO;
        marginLeft = bWidth - width;
      } else {
        width = bWidth;
        height = width * IMG_RATIO;
        marginTop = bHeight - height;
      }
      img.style.marginTop = marginTop ? marginTop + 'px' : '';
      img.style.marginLeft = marginLeft ? marginLeft + 'px' : '';
      img.style.height = height + 'px';
      img.style.width = width + 'px';
    }
  }, {
    key: '_recoveryImage',
    value: function _recoveryImage() {
      var img = _reactDom2.default.findDOMNode(this.refs.listImg);
      img.style.marginLeft = img.style.marginTop = img.style.height = img.style.width = '';
    }
  }, {
    key: '_handleResize',
    value: function _handleResize() {

      // only for desktop, we have the parallax animation
      if (window.innerWidth > 800) {

        // resize image
        this._resizeImage();

        if (!this.scrollController) {
          // init controller
          this.scrollController = new ScrollMagic.Controller();
        }

        if (this.scrollScene) {
          // remove scene from controller
          this.scrollScene.remove();
        }

        if (!this.scrollScene) {
          // create a scene
          this.scrollScene = new ScrollMagic.Scene({
            triggerElement: '#parallax-trigger' + this.props.article.id,
            triggerHook: 0 // don't trigger until #parallax-trigger hits the top of the viewport,
          });
        }

        this.scrollScene.duration(window.innerHeight).setPin('#parallax-trigger' + this.props.article.id, { pushFollowers: false }) // pins the element for the the scene's duration
        // for debug
        // .addIndicators()
        .addTo(this.scrollController);
      } else {
        // remove style added by _resizeImage function
        this._recoveryImage();

        if (this.scrollScene) {
          // remove the pin from the scene and reset the pin element to its initial position (spacer is removed)
          this.scrollScene.removePin(true);
          // remove scene from controller it belonged to
          this.scrollScene.remove();
        }
      }
    }
  }, {
    key: 'componentDidMount',
    value: function componentDidMount() {
      this._handleResize();
      window.addEventListener('resize', this._handleResize);
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      window.removeEventListener('resize', this._handleResize);
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props;
      var article = _props.article;
      var image = _props.image;

      var pubDate = (0, _dateTransformer.date2yyyymmdd)(article.publishedDate, '.');
      var url = '/a/' + article.slug;
      var catDisplay = '專題';
      var excerpt = _lodash2.default.get(article, 'ogDescription', '');

      return _react2.default.createElement(
        'li',
        { className: 'listing-item', key: article.id },
        _react2.default.createElement(
          _reactRouter.Link,
          { to: url },
          _react2.default.createElement(
            'div',
            {
              id: 'parallax-trigger' + this.props.article.id,
              className: 'img-wrap'
            },
            _react2.default.createElement('img', {
              ref: 'listImg',
              width: '1800px',
              height: '1200px',
              className: 'img',
              src: image
            }),
            _react2.default.createElement('div', { className: 'img-overlay' })
          ),
          _react2.default.createElement(
            'div',
            { ref: 'parallaxIndicator', className: 'listing-item-container' },
            _react2.default.createElement(
              'div',
              { className: 'border clearfix' },
              _react2.default.createElement(
                'div',
                { className: 'featurebox' },
                _react2.default.createElement(
                  'div',
                  { className: 'cat-container' },
                  _react2.default.createElement(
                    _Category2.default,
                    null,
                    catDisplay
                  )
                ),
                _react2.default.createElement(
                  'div',
                  { className: 'infobox' },
                  _react2.default.createElement(
                    'div',
                    { className: 'subtitle' },
                    article.subtitle
                  ),
                  _react2.default.createElement(
                    'div',
                    { className: 'title' },
                    article.title
                  ),
                  _react2.default.createElement(
                    'div',
                    { className: 'excerpt' },
                    excerpt
                  ),
                  _react2.default.createElement(
                    'div',
                    { className: 'published' },
                    pubDate
                  )
                )
              )
            )
          )
        )
      );
    }
  }]);

  return FeaturesItem;
}(_react.Component);

exports.default = FeaturesItem;