"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _animate = _interopRequireDefault(require("../../animate"));

var _icon = _interopRequireDefault(require("../../icon"));

var _util = require("../../util");

var _item = _interopRequireDefault(require("./item"));

var _selectableItem = _interopRequireDefault(require("./selectable-item"));

var _popupItem = _interopRequireDefault(require("./popup-item"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _extends() { _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; }; return _extends.apply(this, arguments); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

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

var Expand = _animate["default"].Expand;
var bindCtx = _util.func.bindCtx;
/**
 * Menu.SubMenu
 * @order 1
 */

var SubMenu =
/*#__PURE__*/
function (_Component) {
  _inherits(SubMenu, _Component);

  function SubMenu(props) {
    var _this;

    _classCallCheck(this, SubMenu);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(SubMenu).call(this, props));
    bindCtx(_assertThisInitialized(_this), ['handleMouseEnter', 'handleMouseLeave', 'handleClick', 'handleOpen', 'afterLeave']);
    return _this;
  }

  _createClass(SubMenu, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.itemNode = (0, _reactDom.findDOMNode)(this);
    }
  }, {
    key: "afterLeave",
    value: function afterLeave() {
      var _this$props = this.props,
          focused = _this$props.focused,
          root = _this$props.root;
      var focusable = root.props.focusable;

      if (focusable && focused) {
        this.itemNode.focus();
      }
    }
  }, {
    key: "getOpen",
    value: function getOpen() {
      var _this$props2 = this.props,
          _key = _this$props2._key,
          root = _this$props2.root;
      var openKeys = root.state.openKeys;
      return openKeys.indexOf(_key) > -1;
    }
  }, {
    key: "handleMouseEnter",
    value: function handleMouseEnter(e) {
      this.handleOpen(true);
      this.props.onMouseEnter && this.props.onMouseEnter(e);
    }
  }, {
    key: "handleMouseLeave",
    value: function handleMouseLeave(e) {
      this.handleOpen(false);
      this.props.onMouseLeave && this.props.onMouseLeave(e);
    }
  }, {
    key: "handleClick",
    value: function handleClick(e) {
      var _this$props3 = this.props,
          root = _this$props3.root,
          selectable = _this$props3.selectable;
      var selectMode = root.props.selectMode;

      if (selectMode && selectable) {
        e.stopPropagation();
      }

      var open = this.getOpen();
      this.handleOpen(!open);
    }
  }, {
    key: "handleOpen",
    value: function handleOpen(open, triggerType, e) {
      var _this$props4 = this.props,
          _key = _this$props4._key,
          root = _this$props4.root;
      root.handleOpen(_key, open, triggerType, e);
    }
  }, {
    key: "passParentToChildren",
    value: function passParentToChildren(children) {
      var _this2 = this;

      var _this$props5 = this.props,
          mode = _this$props5.mode,
          root = _this$props5.root;
      return _react.Children.map(children, function (child) {
        return (0, _react.cloneElement)(child, {
          parent: _this2,
          parentMode: mode || root.props.mode
        });
      });
    }
  }, {
    key: "renderInline",
    value: function renderInline() {
      var _cx, _cx2, _cx3;

      var _this$props6 = this.props,
          _key = _this$props6._key,
          level = _this$props6.level,
          root = _this$props6.root,
          className = _this$props6.className,
          selectableFromProps = _this$props6.selectable,
          label = _this$props6.label,
          children = _this$props6.children,
          noIcon = _this$props6.noIcon,
          subMenuContentClassName = _this$props6.subMenuContentClassName,
          propsTriggerType = _this$props6.triggerType,
          parentMode = _this$props6.parentMode;
      var _root$props = root.props,
          prefix = _root$props.prefix,
          selectMode = _root$props.selectMode,
          rootTriggerType = _root$props.triggerType,
          inlineArrowDirection = _root$props.inlineArrowDirection,
          iconArrowType = _root$props.iconArrowType,
          expandAnimation = _root$props.expandAnimation,
          rtl = _root$props.rtl;
      var triggerType = propsTriggerType || rootTriggerType;
      var open = this.getOpen();

      var others = _util.obj.pickOthers(Object.keys(SubMenu.propTypes), this.props);

      var liProps = {
        className: (0, _classnames["default"])((_cx = {}, _defineProperty(_cx, "".concat(prefix, "menu-sub-menu-wrapper"), true), _defineProperty(_cx, className, !!className), _cx))
      };
      var itemProps = {
        'aria-expanded': open,
        _key: _key,
        level: level,
        root: root,
        type: 'submenu',
        component: 'div',
        parentMode: parentMode
      };
      var arrorProps = {
        type: inlineArrowDirection === 'right' ? "".concat(iconArrowType, "-right-s") : "".concat(iconArrowType, "-down-s"),
        className: (0, _classnames["default"])((_cx2 = {}, _defineProperty(_cx2, "".concat(prefix, "menu-icon-arrow"), true), _defineProperty(_cx2, "".concat(prefix, "menu-icon-arrow-down"), inlineArrowDirection === 'down'), _defineProperty(_cx2, "".concat(prefix, "menu-icon-arrow-right"), inlineArrowDirection === 'right'), _defineProperty(_cx2, "".concat(prefix, "open"), open), _cx2))
      };
      var selectable = !!selectMode && selectableFromProps;
      var NewItem = selectable ? _selectableItem["default"] : _item["default"];

      if (triggerType === 'hover') {
        liProps.onMouseEnter = this.handleMouseEnter;
        liProps.onMouseLeave = this.handleMouseLeave;
      } else if (selectable) {
        arrorProps.onClick = this.handleClick;
      } else {
        itemProps.onClick = this.handleClick;
      }

      if (open) {
        itemProps.className = "".concat(prefix, "opened");
      }

      var newSubMenuContentClassName = (0, _classnames["default"])((_cx3 = {}, _defineProperty(_cx3, "".concat(prefix, "menu-sub-menu"), true), _defineProperty(_cx3, subMenuContentClassName, !!subMenuContentClassName), _cx3));
      var roleMenu = 'menu',
          roleItem = 'menuitem';

      if ('selectMode' in root.props) {
        roleMenu = 'listbox';
        roleItem = 'listitem';
      }

      var subMenu = open ? _react["default"].createElement("ul", {
        role: roleMenu,
        dir: rtl ? 'rtl' : undefined,
        ref: "subMenu",
        className: newSubMenuContentClassName
      }, this.passParentToChildren(children)) : null;
      return _react["default"].createElement("li", _extends({
        role: roleItem
      }, others, liProps), _react["default"].createElement(NewItem, itemProps, _react["default"].createElement("span", {
        className: "".concat(prefix, "menu-item-text")
      }, label), noIcon ? null : _react["default"].createElement(_icon["default"], arrorProps)), expandAnimation ? _react["default"].createElement(Expand, {
        animationAppear: false,
        afterLeave: this.afterLeave
      }, subMenu) : subMenu);
    }
  }, {
    key: "renderPopup",
    value: function renderPopup() {
      var _cx4;

      var _this$props7 = this.props,
          children = _this$props7.children,
          subMenuContentClassName = _this$props7.subMenuContentClassName,
          noIcon = _this$props7.noIcon,
          others = _objectWithoutProperties(_this$props7, ["children", "subMenuContentClassName", "noIcon"]);

      var root = this.props.root;
      var _root$props2 = root.props,
          prefix = _root$props2.prefix,
          popupClassName = _root$props2.popupClassName,
          popupStyle = _root$props2.popupStyle,
          rtl = _root$props2.rtl;
      var newClassName = (0, _classnames["default"])((_cx4 = {}, _defineProperty(_cx4, "".concat(prefix, "menu"), true), _defineProperty(_cx4, "".concat(prefix, "ver"), true), _defineProperty(_cx4, popupClassName, !!popupClassName), _defineProperty(_cx4, subMenuContentClassName, !!subMenuContentClassName), _cx4));
      others.rtl = rtl;
      return _react["default"].createElement(_popupItem["default"], _extends({}, others, {
        noIcon: noIcon,
        hasSubMenu: true
      }), _react["default"].createElement("ul", {
        role: "menu",
        dir: rtl ? 'rtl' : undefined,
        className: newClassName,
        style: popupStyle
      }, this.passParentToChildren(children)));
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props8 = this.props,
          mode = _this$props8.mode,
          root = _this$props8.root;
      var newMode = mode || root.props.mode;
      return newMode === 'popup' ? this.renderPopup() : this.renderInline();
    }
  }]);

  return SubMenu;
}(_react.Component);

exports["default"] = SubMenu;

_defineProperty(SubMenu, "menuChildType", 'submenu');

_defineProperty(SubMenu, "propTypes", {
  _key: _propTypes["default"].string,
  root: _propTypes["default"].object,
  level: _propTypes["default"].number,
  groupIndent: _propTypes["default"].number,
  noIcon: _propTypes["default"].bool,

  /**
   * 标签内容
   */
  label: _propTypes["default"].node,

  /**
   * 是否可选，该属性仅在设置 Menu 组件 selectMode 属性后生效
   */
  selectable: _propTypes["default"].bool,

  /**
   * 子菜单打开方式，如果设置会覆盖 Menu 上的同名属性
   * @default Menu 的 mode 属性值
   */
  mode: _propTypes["default"].oneOf(['inline', 'popup']),

  /**
   * 菜单项或下一级子菜单
   */
  children: _propTypes["default"].node,
  onMouseEnter: _propTypes["default"].func,
  onMouseLeave: _propTypes["default"].func,
  subMenuContentClassName: _propTypes["default"].string,
  triggerType: _propTypes["default"].oneOf(['click', 'hover']),
  align: _propTypes["default"].oneOf(['outside', 'follow']),
  parentMode: _propTypes["default"].oneOf(['inline', 'popup'])
});

_defineProperty(SubMenu, "defaultProps", {
  groupIndent: 0,
  noIcon: false,
  selectable: false
});