'use strict';

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

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

var _reactGa2 = _interopRequireDefault(_reactGa);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _reactDom2 = _interopRequireDefault(_reactDom);

var _logo = '/' + "525dc9d91569ab7a1a873cf595a73327.svg";

var _logo2 = _interopRequireDefault(_logo);

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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; } /* global $ */


if (process.env.BROWSER) {}

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

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

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

    _this.state = {
      height: 110,
      isScrolledOver: false,
      isClickedExpandNav: false
    };
    _this._getHeaderHeight = _this._getHeaderHeight.bind(_this);
    _this._handleScroll = _this._handleScroll.bind(_this);
    _this._handleClick = _this._handleClick.bind(_this);
    _this._renderMenu = _this._renderMenu.bind(_this);
    _this._openSidebar = _this._openSidebar.bind(_this);
    _this._openSearchbar = _this._openSearchbar.bind(_this);
    _this._expandNavigation = _this._expandNavigation.bind(_this);
    return _this;
  }

  _createClass(HeaderB, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this._getHeaderHeight();
      window.addEventListener('resize', this._getHeaderHeight);

      // detect sroll position
      window.addEventListener('scroll', this._handleScroll);
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      window.removeEventListener('scroll', this._handleScroll);
    }
  }, {
    key: '_handleScroll',
    value: function _handleScroll() {
      var scrollPos = window.scrollY;
      if (scrollPos > this.state.height && !this.state.isScrolledOver) {
        this.setState({ isScrolledOver: true });
      } else if (scrollPos <= this.state.height && this.state.isScrolledOver) {
        this.setState({ isScrolledOver: false });
      }
    }
  }, {
    key: '_handleClick',
    value: function _handleClick() {
      _reactGa2.default.event({
        category: this.props.pathName,
        action: 'click',
        label: 'header'
      });
    }
  }, {
    key: '_getHeaderHeight',
    value: function _getHeaderHeight() {
      if (_reactDom2.default.findDOMNode(this.refs.headerbox)) {
        var rect = _reactDom2.default.findDOMNode(this.refs.headerbox).getBoundingClientRect();
        this.setState({
          height: _lodash2.default.get(rect, 'height', 110)
        });
      }
    }
  }, {
    key: 'shouldComponentUpdate',
    value: function shouldComponentUpdate(nextProps, nextState) {
      // eslint-disable-line
      if (nextState.height !== this.state.height || nextState.isScrolledOver !== this.state.isScrolledOver) {
        return true;
      }
      return true;
    }
  }, {
    key: '_openSidebar',
    value: function _openSidebar() {
      // console.log('open sidebar')
      $('.ui.left.sidebar').sidebar({
        dimPage: false,
        exclusive: true,
        scrollLock: true,
        transition: 'overlay',
        mobileTransition: 'overlay'
      }).sidebar('toggle');
    }
  }, {
    key: '_openSearchbar',
    value: function _openSearchbar() {
      // console.log('open sidebar')
      $('.ui.top.sidebar').sidebar({
        dimPage: false,
        exclusive: true,
        scrollLock: true,
        transition: 'overlay',
        mobileTransition: 'overlay'
      }).sidebar('toggle');
    }
  }, {
    key: '_expandNavigation',
    value: function _expandNavigation() {
      if (this.state.isClickedExpandNav) {
        $('.ui.header.main.menu.menu-item').css('height', '52px');
        $('.item.item-navClick').text('》');
        $('.ui.header.main.menu.menu-item.mobile-hide').removeClass('nav-scrolled-clicked');
        this.state.isClickedExpandNav = false;
      } else {
        $('.ui.header.main.menu.menu-item').css('height', 'auto');
        $('.item.item-navClick').text('︽');
        $('.ui.header.main.menu.menu-item.mobile-hide').addClass('nav-scrolled-clicked');
        this.state.isClickedExpandNav = true;
      }
    }
  }, {
    key: '_renderMenu',
    value: function _renderMenu() {
      var status = this.state.isScrolledOver ? 'fixed top' : 'hidden';

      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          'div',
          { className: (0, _classnames2.default)('ui borderless main menu mobile-only', status) },
          _react2.default.createElement(
            'div',
            { className: 'ui text container', style: { maxWidth: 100 + '% !important', width: 100 + '%' } },
            _react2.default.createElement(
              'div',
              { className: 'item' },
              _react2.default.createElement(
                'a',
                { onClick: this._openSidebar, style: { cursor: 'pointer' } },
                _react2.default.createElement('img', { src: '/asset/icon/hamburger@2x.png', className: 'header-icon hamburger' })
              )
            ),
            _react2.default.createElement(
              'div',
              { className: 'right menu' },
              _react2.default.createElement(
                _reactRouter.Link,
                { to: '/', className: 'item' },
                _react2.default.createElement('img', _defineProperty({ className: 'logo', src: '/asset/icon/logo@2x.png' }, 'className', 'header-icon logo'))
              ),
              _react2.default.createElement(
                'div',
                { className: 'item' },
                _react2.default.createElement(
                  'a',
                  { onClick: this._openSearchbar, style: { cursor: 'pointer' } },
                  _react2.default.createElement('img', { src: '/asset/icon/search@2x.png', className: 'header-icon search' })
                )
              )
            )
          )
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props;
      var sectionList = _props.sectionList;
      var topics = _props.topics;


      var itemsForHeader = {};
      itemsForHeader.topics = [];
      itemsForHeader.sections = [];
      itemsForHeader.categories = [];

      GenerateNav();

      function GenerateNav() {
        _lodash2.default.each(topics.items, function (t) {
          if (t.isFeatured) {
            itemsForHeader.topics.push(t);
          }
        });
        _lodash2.default.each(sectionList.sections, function (s) {
          if (s.isFeatured) {
            itemsForHeader.sections.push(s);
          }
          _lodash2.default.each(s.categories, function (c) {
            if (c.isFeatured) {
              itemsForHeader.categories.push(c);
            }
          });
        });
      }

      return _react2.default.createElement(
        'div',
        { className: 'HeaderB', ref: 'headerbox' },
        _react2.default.createElement(
          'div',
          { className: 'ui borderless header main menu' },
          _react2.default.createElement(
            'div',
            { className: 'ui text container', style: { maxWidth: 1024 + 'px !important' } },
            _react2.default.createElement(
              'div',
              { className: 'left' },
              _react2.default.createElement(
                'div',
                { className: 'item mobile-only hamburger', style: { marginTop: '25px' } },
                _react2.default.createElement(
                  'a',
                  { onClick: this._openSidebar, style: { cursor: 'pointer' } },
                  _react2.default.createElement('img', { src: '/asset/icon/hamburger@2x.png', className: 'header-icon hamburger' })
                )
              )
            ),
            _react2.default.createElement(
              _reactRouter.Link,
              { to: '/', className: 'header item', style: { marginLeft: '122px' } },
              _react2.default.createElement('img', { className: 'logo main', src: _logo2.default })
            ),
            _react2.default.createElement(
              'div',
              { className: 'right menu' },
              _react2.default.createElement(
                'div',
                { className: 'item share' },
                _react2.default.createElement(
                  'span',
                  null,
                  '訂閱：'
                ),
                _react2.default.createElement(
                  'a',
                  { href: _index.SOCIAL_LINK.LINE },
                  _react2.default.createElement('img', { src: '/asset/icon/line@2x.png', className: 'header-icon line' })
                ),
                _react2.default.createElement(
                  'a',
                  { href: _index.SOCIAL_LINK.WEIBO },
                  _react2.default.createElement('img', { src: '/asset/icon/weibo@2x.png', className: 'header-icon weibo' })
                ),
                _react2.default.createElement(
                  'a',
                  { href: _index.SOCIAL_LINK.FACEBOOK },
                  _react2.default.createElement('img', { src: '/asset/icon/facebook@2x.png', className: 'header-icon facebook' })
                ),
                _react2.default.createElement(
                  'a',
                  { href: _index.SOCIAL_LINK.INSTAGRAM },
                  _react2.default.createElement('img', { src: '/asset/icon/instagram@2x.png', className: 'header-icon instagram' })
                ),
                _react2.default.createElement(
                  'a',
                  { href: _index.SOCIAL_LINK.SUBSCRIBE },
                  _react2.default.createElement('img', { src: '/asset/icon/book@2x.png', className: 'header-icon book' })
                ),
                _react2.default.createElement('div', { className: 'vertical line' }),
                _react2.default.createElement(
                  'a',
                  { href: '#', onClick: this._openSearchbar },
                  _react2.default.createElement('img', { src: '/asset/icon/search@2x.png', className: 'header-icon search' })
                )
              ),
              _react2.default.createElement(
                'div',
                { className: 'item mobile-only', style: { marginTop: '25px' } },
                _react2.default.createElement(
                  'a',
                  { href: '#', onClick: this._openSearchbar },
                  _react2.default.createElement('img', { src: '/asset/icon/search@2x.png', className: 'header-icon search' })
                )
              )
            )
          )
        ),
        _react2.default.createElement(
          'div',
          { className: (0, _classnames2.default)('ui borderless header main menu menu-item mobile-hide') },
          _react2.default.createElement(
            'div',
            { className: 'ui text container', style: { maxWidth: 100 + '% !important', width: 100 + '%' } },
            _react2.default.createElement(
              'div',
              { className: 'container', style: { position: 'relative', overflow: 'hidden' } },
              _react2.default.createElement(
                'div',
                { className: 'nav-container' },
                _lodash2.default.map(itemsForHeader.sections, function (i) {
                  return _react2.default.createElement(
                    'a',
                    { href: '/section/' + i.name, key: i.id, className: 'item nav-item', onClick: _this2._handleClick },
                    i.title
                  );
                }),
                _lodash2.default.map(itemsForHeader.categories, function (i) {
                  return _react2.default.createElement(
                    'a',
                    { href: '/category/' + i.name, key: i.id, className: 'item nav-item', onClick: _this2._handleClick },
                    i.title
                  );
                }),
                _lodash2.default.map(itemsForHeader.topics, function (i) {
                  return _react2.default.createElement(
                    'a',
                    { href: '/topic/' + i.id, key: i.id, className: 'item nav-item', onClick: _this2._handleClick },
                    i.name
                  );
                })
              )
            )
          )
        ),
        this._renderMenu()
      );
    }
  }]);

  return HeaderB;
}(_react.Component);

exports.default = HeaderB;
exports.HeaderB = HeaderB;