'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = 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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

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; } /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                * Created by baoyinghai on 4/5/17.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                */


var DropMenu = function (_React$Component) {
  _inherits(DropMenu, _React$Component);

  function DropMenu() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, DropMenu);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = DropMenu.__proto__ || Object.getPrototypeOf(DropMenu)).call.apply(_ref, [this].concat(args))), _this), _this._renderOtherBtns = function () {
      var btns = _this.props.renderTopButtons;
      if (btns && btns.length > 1) {
        return _react2.default.createElement(
          'ul',
          { className: 'menu-drop-list' },
          btns.slice(1).map(function (link, index) {
            return _react2.default.createElement(
              'li',
              { key: 'other-btn' + index },
              link
            );
          })
        );
      }
      return _react2.default.createElement(
        'ul',
        { className: 'menu-drop-list' },
        _react2.default.createElement(
          'li',
          { key: 'other-btn' },
          _react2.default.createElement(
            'a',
            null,
            '\u65E0\u5176\u4ED6\u64CD\u4F5C'
          )
        )
      );
    }, _this._switchHandler = function (e) {
      if (_this.tag && _this.tag.compareDocumentPosition(e.target) !== 20 && _this.props.showOtherBtns) {
        var moreBtnBlur = _this.props.moreBtnBlur;

        moreBtnBlur && moreBtnBlur();
      }
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(DropMenu, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var querySelector = this.props.querySelector;
      var tags = document.querySelectorAll('.' + querySelector + ' .menu-drop');
      if (tags.length > 0) {
        this.tag = tags[tags.length - 1];
      }
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      window.removeEventListener('click', this._switchHandler);
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      var _this2 = this;

      if (nextProps.showOtherBtns) {
        setTimeout(function () {
          return window.addEventListener('click', _this2._switchHandler);
        });
      } else {
        window.removeEventListener('click', this._switchHandler);
      }
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        { className: 'menu-drop', style: this.props.style, onClick: this.props.moreBtnBlur },
        this._renderOtherBtns()
      );
    }
  }]);

  return DropMenu;
}(_react2.default.Component);

exports.default = DropMenu;