'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

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 _class, _temp, _initialiseProps; /**
                                      * Created by baoyinghai on 4/18/17.
                                      */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _double_arrow = require('../../asset/double_arrow.png');

var _double_arrow2 = _interopRequireDefault(_double_arrow);

var _Link = require('./Link');

var _Link2 = _interopRequireDefault(_Link);

var _anchorHref = require('./anchorHref');

var _anchorHref2 = _interopRequireDefault(_anchorHref);

var _EmptyUtils = require('../../framework/utils/EmptyUtils');

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 Anchor = (_temp = _class = function (_React$Component) {
  _inherits(Anchor, _React$Component);

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

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

    _initialiseProps.call(_this);

    var _this$props = _this.props,
        links = _this$props.links,
        offset = _this$props.offset;

    _this.state = {
      offset: offset,
      shortcutName: null,
      isOpen: false
    };
    return _this;
  }

  _createClass(Anchor, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      window.addEventListener('scroll', this._moveArrow);
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      window.addEventListener('scroll', this._moveArrow);
    }
  }, {
    key: 'render',
    value: function render() {
      var _props$links = this.props.links,
          links = _props$links === undefined ? [] : _props$links;

      var affixWrapperStyle = { border: 0, padding: 0, minWidth: 100 };
      var shortcutName = this.state.shortcutName;
      if (links.length > 0 && !this.state.shortcutName) {
        shortcutName = links[0].title;
        // const linksTop = [];
        // links.forEach(item => linksTop.push(item.offsetTop));
        // const currentScrollY = window.scrollY + window.innerHeight;
        // linksTop.push(currentScrollY);
        // linksTop.sort((a, b) => a - b);
        // if (links[linksTop.indexOf(currentScrollY) - 1]) {
        //   shortcutName = links[linksTop.indexOf(currentScrollY) - 1].title;
        // }
      }
      /* eslint-disable */
      return _react2.default.createElement(
        'div',
        { className: 'affix-wrapper', style: links.length ? null : affixWrapperStyle },
        _react2.default.createElement(
          'div',
          { className: 'btn-list' },
          _react2.default.createElement(
            'div',
            { style: { width: 100, float: 'right', marginBottom: 3 } },
            this.props.children
          ),
          _react2.default.createElement(
            'div',
            { className: 'affix-shortcut-wrapper', onMouseOver: this.affixShortcutMouseOver },
            links.length > 0 && _react2.default.createElement(
              'div',
              { className: 'affix-shortcut', style: !this.state.isOpen ? { width: 100, float: 'right', backgroundColor: '#272E31' } : { backgroundColor: '#00AEEC' } },
              _react2.default.createElement(
                'span',
                null,
                this.state.isOpen ? shortcutName : this._dealShortcutName(shortcutName)
              ),
              this.state.isOpen && _react2.default.createElement('img', { src: _double_arrow2.default, onClick: this._arrowClick })
            )
          )
        ),
        this.state.isOpen && _react2.default.createElement(
          'div',
          { className: 'href-list', style: { display: links.length ? 'block' : 'none' } },
          _react2.default.createElement(
            'div',
            { className: 'arrow-pointer', style: { marginTop: this.state.offset + 5 } },
            _react2.default.createElement('div', { className: 'right-arrow' })
          ),
          _react2.default.createElement(
            'ul',
            null,
            links.map(function (p) {
              return _react2.default.createElement(_Link2.default, _extends({ key: p.href }, p));
            })
          )
        )
      );
    }
  }]);

  return Anchor;
}(_react2.default.Component), _class.defaultProps = {
  offset: 6
}, _initialiseProps = function _initialiseProps() {
  var _this2 = this;

  this._moveArrow = function (e) {
    var _props$links2 = _this2.props.links,
        links = _props$links2 === undefined ? [] : _props$links2;

    if (links.length > 0) {
      var tag = links.filter(function (p) {
        return p.offsetTop >= window.scrollY;
      });
      if (tag[0]) {
        var index = tag[0].index;
        /* eslint-disable */
        _this2.setState({ offset: _this2.props.offset + index * 31, shortcutName: tag[0].title });
      }
    }
  };

  this.affixShortcutMouseOver = function (e) {
    if (e.target.className === 'affix-shortcut' && !_this2.state.isOpen) {
      _this2.setState({
        isOpen: true
      });
    }
  };

  this._dealShortcutName = function (shortcutName) {
    if (shortcutName.length > 4) {
      return shortcutName.substr(0, 4) + '...';
    }
    return shortcutName;
  };

  this._arrowClick = function (e) {
    _this2.setState({
      isOpen: false
    });
  };
}, _temp);
exports.default = Anchor;
var ArchorHref = exports.ArchorHref = _anchorHref2.default;

var archorQuery = exports.archorQuery = _anchorHref.queryAnchor;