'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.LeadingFull = 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 _index = require('../utils/index');

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _dateformat = require('dateformat');

var _dateformat2 = _interopRequireDefault(_dateformat);

var _entities = require('entities');

var _entities2 = _interopRequireDefault(_entities);

var _reactGa = require('react-ga');

var _reactGa2 = _interopRequireDefault(_reactGa);

var _sanitizeHtml = require('sanitize-html');

var _sanitizeHtml2 = _interopRequireDefault(_sanitizeHtml);

var _truncate = require('truncate');

var _truncate2 = _interopRequireDefault(_truncate);

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; }

if (process.env.BROWSER) {}

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

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

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

    _this._handleClick = _this._handleClick.bind(_this);
    return _this;
  }

  _createClass(LeadingFull, [{
    key: '_handleClick',
    value: function _handleClick() {
      _reactGa2.default.event({
        category: this.props.pathName,
        action: 'click',
        label: 'leadingFull'
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var articles = this.props.articles;

      var sortedArticles = _lodash2.default.sortBy(articles, function (o) {
        return new Date(o.publishedDate);
      }).reverse();

      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement('div', { className: 'leadingFull__gradient' }),
        _lodash2.default.map(_lodash2.default.take(sortedArticles, 2), function (a) {
          var image = (0, _index.imageComposer)(a).desktopImage;
          var title = (0, _sanitizeHtml2.default)(_lodash2.default.get(a, ['title'], ''), { allowedTags: [] });
          var hasRelated = _lodash2.default.difference(a.relateds, [null, '']).length > 0 ? 'static' : 'none';
          var linkStyle = _lodash2.default.get(a, 'style', '') == 'projects' ? '/projects/' : '/story/';

          var brief = (0, _sanitizeHtml2.default)(_lodash2.default.get(a, ['brief', 'html'], ''), { allowedTags: [] });
          var content = (0, _sanitizeHtml2.default)(_lodash2.default.get(a, ['content', 'html'], ''), { allowedTags: [] });
          var briefContent = brief.length > 0 ? brief : content;

          var writers = '文｜' + _lodash2.default.map(a.writers, 'name').join('、');
          var relateds = _lodash2.default.filter(a.relateds, 'id');

          return _react2.default.createElement(
            'section',
            { className: '', key: 'choiceFull' + a.id },
            _react2.default.createElement(
              'a',
              { href: linkStyle + a.slug + '/', onClick: _this2._handleClick },
              _react2.default.createElement('figure', { className: 'post-image', style: { background: 'url(' + image + ') no-repeat center center', backgroundSize: 'cover' } })
            ),
            _react2.default.createElement(
              'div',
              { className: 'post-block' },
              _react2.default.createElement(
                'div',
                { className: 'post meta' },
                _react2.default.createElement(
                  'div',
                  { className: 'author' },
                  _lodash2.default.get(a, ['writers', 'length'], 0) > 0 ? writers + ' ' : null
                ),
                _react2.default.createElement(
                  'div',
                  { className: 'date' },
                  (0, _dateformat2.default)(a.publishedDate, 'yyyy.mm.dd')
                )
              ),
              _react2.default.createElement(
                'div',
                { className: 'post content' },
                _react2.default.createElement(
                  'a',
                  { href: linkStyle + a.slug + '/', onClick: _this2._handleClick },
                  _react2.default.createElement(
                    'div',
                    { className: 'post title' },
                    _react2.default.createElement('h2', { dangerouslySetInnerHTML: { __html: title } })
                  )
                ),
                _react2.default.createElement(
                  'div',
                  { className: 'post brief' },
                  (0, _truncate2.default)(_entities2.default.decodeHTML(briefContent), 200)
                )
              ),
              _react2.default.createElement('div', { className: 'post dfp' })
            ),
            _react2.default.createElement(
              'div',
              { className: 'related-post-block', style: { display: hasRelated } },
              _react2.default.createElement(
                'div',
                { className: 'related-post-container' },
                _react2.default.createElement(
                  'span',
                  null,
                  '相關文章 Related Stories'
                ),
                _react2.default.createElement('div', { className: 'related-divider' }),
                _lodash2.default.map(_lodash2.default.take(relateds, 2), function (r) {
                  var image = (0, _index.imageComposer)(r).mobileImage;
                  var linkStyle = _lodash2.default.get(r, 'style', '') == 'projects' ? '/projects/' : '/story/';

                  var brief = (0, _sanitizeHtml2.default)(_lodash2.default.get(r, ['brief', 'html'], ''), { allowedTags: [] });
                  var content = (0, _sanitizeHtml2.default)(_lodash2.default.get(r, ['content', 'html'], ''), { allowedTags: [] });
                  var briefContent = brief.length > 0 ? brief : content;
                  var writers = '文｜' + _lodash2.default.map(r.writers, 'name').join('、');

                  return _react2.default.createElement(
                    'div',
                    { className: 'related-post', key: r.id },
                    _react2.default.createElement(
                      'a',
                      { href: linkStyle + r.slug + '/', onClick: _this2._handleClick },
                      _react2.default.createElement('figure', { className: 'related-post__img', style: { background: 'url(' + image + ') no-repeat center center', backgroundSize: 'cover' } })
                    ),
                    _react2.default.createElement(
                      'div',
                      { className: 'related-post__content' },
                      _react2.default.createElement(
                        'a',
                        { href: linkStyle + r.slug + '/', onClick: _this2._handleClick },
                        _react2.default.createElement(
                          'div',
                          { className: 'related-post__title' },
                          ' ',
                          r.title
                        )
                      ),
                      _react2.default.createElement(
                        'div',
                        { className: 'related-post__brief' },
                        (0, _truncate2.default)(_entities2.default.decodeHTML(briefContent), 55)
                      ),
                      _react2.default.createElement(
                        'div',
                        { className: 'related-post__meta' },
                        _react2.default.createElement(
                          'div',
                          { className: 'related-post__author' },
                          _lodash2.default.get(a, ['writers', 'length'], 0) > 0 ? writers + ' ' : null
                        ),
                        _react2.default.createElement(
                          'div',
                          { className: 'related-post__date' },
                          (0, _dateformat2.default)(r.publishedDate, 'yyyy.mm.dd')
                        )
                      )
                    )
                  );
                })
              )
            )
          );
        })
      );
    }
  }]);

  return LeadingFull;
}(_react.Component);

exports.default = LeadingFull;
exports.LeadingFull = LeadingFull;