'use strict';

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

var _react2 = _interopRequireDefault(_react);

var _qs = require('qs');

var _qs2 = _interopRequireDefault(_qs);

var _IconHelper = require('../../framework/utils/IconHelper');

var _MenuHelper = require('../../framework/utils/MenuHelper');

var _routes = require('../../framework/entrance/routes');

var _ProcessHelper = require('../../framework/utils/ProcessHelper');

var _menuaim = require('./menuaim');

var _menuaim2 = _interopRequireDefault(_menuaim);

var _clickSpaceCallback = require('../clickSpaceCallback');

var _SessionStorage = require('../../framework/service/SessionStorage');

var _windowUtils = require('../../framework/utils/windowUtils');

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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/12/17.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                */


var HNav = exports.HNav = function (_React$Component) {
  _inherits(HNav, _React$Component);

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

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

    _this._switchHandler = function (e) {
      var tag = document.querySelector('.total-menu');
      if (tag && tag.compareDocumentPosition(e.target) !== 20) {
        _this._switchTotalMenu();
      }
    };

    _this._switchTotalMenu = function () {
      _this.props.switchHandler && _this.props.switchHandler();
    };

    _this.handleSwitchactiveSubIndex = function (index) {
      _this.setState({
        activeSubIndex: index,
        activeSubSubIndex: -1,
        activeSubSubSubIndex: -1
      });
    };

    _this.handleSwitchactiveSubSubIndex = function (index) {
      _this.setState({
        activeSubSubIndex: index,
        activeSubSubSubIndex: -1
      });
    };

    _this.handleSwitchactiveSubSubSubIndex = function (index) {
      _this.setState({
        activeSubSubSubIndex: index
      });
    };

    _this._getActionLink = function (item) {
      return item.domainLink || item.actionLink;
    };

    _this._updateEnterMenuId = function (menuCode, processId) {
      _this.props.updateEnterMenuId([menuCode, processId]);
    };

    _this._getMenuBindParams = function (menuItem) {
      var ret = {};
      menuItem.bindParameters && menuItem.bindParameters.forEach(function (item) {
        ret[item.name] = item.value;
      });
      if ((0, _ProcessHelper.isApply)(_this._getActionLink(menuItem))) {
        ret.menu = 'BPMApply.QueryToDo';
      } else if ((0, _ProcessHelper.isReview)(_this._getActionLink(menuItem))) {
        ret.menu = 'BPMReview.QueryToDo';
      }
      return ret;
    };

    _this._menuItemClick = function (item) {
      var link = _this._getActionLink(item);
      if (link) {
        var bindParameters = _this._getMenuBindParams(item);
        (0, _SessionStorage.saveProcessMenuCode)(bindParameters.menu);
        (0, _SessionStorage.saveEnterMenuId)(item.menuCode);
        // 如果menuItemClick 是Info模板的话 就跳新页面
        // if (item.templateType === 'Info') {
        //   jumpToNewTab(
        //     '/' + CONTAINER_PRE + this._getActionLink(item) + '?p=' + btoa(Qs.stringify({ ...bindParameters, enterMenuId: item.menuCode, domainType: item.templateType, s: '1' })),
        //     { buttonCode: item.menuCode }
        //   );
        // } else {
        //   this.props.push({ pathname: '/' + CONTAINER_PRE + this._getActionLink(item), state: { ...bindParameters } });
        // }
        _this.props.push({ pathname: '/' + _routes.CONTAINER_PRE + _this._getActionLink(item), state: _extends({}, bindParameters) });
        _this._switchTotalMenu();
      }
    };

    _this._cancelQuickMenu = function (event, menuCode) {
      event.stopPropagation();
      var quickMenu = _this.props.quickMenu || [];
      _this.props.updateQuickMenu(quickMenu.filter(function (item) {
        return item !== menuCode;
      }));
    };

    _this._addQuickMenu = function (event, menuCode) {
      event.stopPropagation();
      var quickMenu = _this.props.quickMenu || [];
      quickMenu.push(menuCode);
      _this.props.updateQuickMenu(quickMenu);
    };

    _this._isQuickMenu = function (code) {
      var quickMenu = _this.props.quickMenu || [];
      return quickMenu.indexOf(code) !== -1;
    };

    _this._renderIcon = function (item) {
      if (!item.subMenus || item.subMenus.length === 0) {
        if (_this._isQuickMenu(item.menuCode)) {
          return (0, _IconHelper.iconWrapper)('icon-shoucangshixin', null, null, { onClick: function onClick(event) {
              return _this._cancelQuickMenu(event, item.menuCode);
            } });
        }
        if (_this._getActionLink(item)) {
          return (0, _IconHelper.iconWrapper)('icon-shoucang', null, null, { onClick: function onClick(event) {
              return _this._addQuickMenu(event, item.menuCode);
            } });
        }
        return null; //  no sub menu and  can't click
      }
      return (0, _IconHelper.iconWrapper)('icon-arrow');
    };

    _this._renderAllMenuItems = function (cname, activeIndex) {
      var leafs = (0, _MenuHelper.getAllLeafs)(_this.props.menu);
      return leafs.map(function (menu, index) {
        var className = 'leafs menu-item';
        if (index === _this.state[activeIndex]) {
          className += ' active';
        }
        return _react2.default.createElement(
          'li',
          {
            className: className,
            key: index,
            onMouseEnter: function onMouseEnter() {
              return _this['handleSwitch' + activeIndex](index);
            },
            onClick: function onClick() {
              return _this._menuItemClick(menu);
            }
          },
          _react2.default.createElement(
            'span',
            null,
            menu.menuValue
          ),
          _this._renderIcon(menu)
        );
      });
    };

    _this._renderItems = function (menus, cname, activeIndex) {
      return menus.map(function (menu, index) {
        var className = 'menu-item';
        if (index === _this.state[activeIndex]) {
          className += ' active';
        }
        return _react2.default.createElement(
          'li',
          {
            className: className,
            key: index,
            onMouseEnter: function onMouseEnter() {
              _this.handleMouseEnterRow.call(_this, index, _this['handleSwitch' + activeIndex], {
                submenuDirection: 'right',
                menuSelector: '.' + cname,
                delay: 300,
                tolerance: 75
              });
            },
            onClick: function onClick() {
              return _this._menuItemClick(menu);
            }
          },
          _react2.default.createElement(
            'span',
            null,
            menu.menuValue
          ),
          _this._renderIcon(menu)
        );
      });
    };

    _this._renderUl = function (menus, cname) {
      var activeIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';

      return _react2.default.createElement(
        'ul',
        { className: cname + (menus && menus.isAll ? ' leaf-wrapper' : ''), onMouseLeave: _this.handleMouseLeaveMenu },
        function () {
          if (menus) {
            if (menus.isAll) {
              return _this._renderAllMenuItems(cname, activeIndex);
            }
            return _this._renderItems(menus, cname, activeIndex);
          }
          return null;
        }()
      );
    };

    _this._getAppendDataMenu = function () {
      return [{ menuValue: '全部', subMenus: { isAll: true } }].concat(_toConsumableArray(_this.props.menu));
    };

    _this._renderAllMenu = function () {
      var menu = _this._getAppendDataMenu();
      return _react2.default.createElement(
        'div',
        { className: 'total-menu-detail' },
        menu && _this._renderUl(menu, 'menu', 'activeSubIndex'),
        menu && menu[_this.state.activeSubIndex] && _this._renderUl(menu[_this.state.activeSubIndex].subMenus, 'sub-menu', 'activeSubSubIndex'),
        menu && menu[_this.state.activeSubIndex] && menu[_this.state.activeSubIndex].subMenus && menu[_this.state.activeSubIndex].subMenus[_this.state.activeSubSubIndex] && _this._renderUl(menu[_this.state.activeSubIndex].subMenus[_this.state.activeSubSubIndex].subMenus, 'sub-sub-menu', 'activeSubSubSubIndex')
      );
    };

    _this.state = {
      activeSubIndex: -1,
      activeSubSubIndex: -1,
      activeSubSubSubIndex: -1
    };
    _this.handleMouseLeaveMenu = _menuaim2.default.handleMouseLeaveMenu.bind(_this);
    _this.handleMouseEnterRow = _menuaim2.default.handleMouseEnterRow.bind(_this);
    _this.didMount = _menuaim2.default.componentDidMount.bind(_this);
    _this.unMount = _menuaim2.default.componentWillUnmount.bind(_this);
    _this.__getMouseMoveDocumentHandler = _menuaim2.default.__getMouseMoveDocumentHandler.bind(_this);
    return _this;
  }

  // mutile


  // mutile


  _createClass(HNav, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.didMount();
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      this.unMount();
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps() {}
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        { className: 'total-menu ' + (this.props.show ? 'active' : '') + ' ' + (this.props.refStr || ' ') },
        this.props.desc ? _react2.default.createElement(
          'span',
          { style: { paddingRight: '5px' } },
          '/'
        ) : null,
        _react2.default.createElement(
          'span',
          { onClick: this._switchTotalMenu },
          this.props.desc || '产品与服务'
        ),
        this.props.desc ? null : (0, _IconHelper.iconWrapper)('icon-jiantou'),
        _react2.default.createElement(
          'div',
          { className: 'total-menu-detail-wrapper' },
          this._renderAllMenu()
        )
      );
    }
  }]);

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

exports.default = (0, _clickSpaceCallback.clickSpaceWrapper)(HNav, { querySelector: 'total-menu-ref' });