"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "TriggerViewMode", {
  enumerable: true,
  get: function get() {
    return _enum2.TriggerViewMode;
  }
});
exports["default"] = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _get3 = _interopRequireDefault(require("@babel/runtime/helpers/get"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _tslib = require("tslib");

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

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

var _noop = _interopRequireDefault(require("lodash/noop"));

var _mobx = require("mobx");

var _Trigger = _interopRequireDefault(require("../../../lib/trigger/Trigger"));

var _enum = require("../../../lib/trigger/enum");

var _utils = require("../data-set/utils");

var _TextField2 = require("../text-field/TextField");

var _autobind = _interopRequireDefault(require("../_util/autobind"));

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

var _TaskRunner = _interopRequireDefault(require("../_util/TaskRunner"));

var _placements = _interopRequireDefault(require("./placements"));

var _enum2 = require("./enum");

var _singleton = require("../tooltip/singleton");

var TriggerField = /*#__PURE__*/function (_TextField) {
  (0, _inherits2["default"])(TriggerField, _TextField);

  var _super = (0, _createSuper2["default"])(TriggerField);

  function TriggerField(props, context) {
    var _this;

    (0, _classCallCheck2["default"])(this, TriggerField);
    _this = _super.call(this, props, context);
    (0, _mobx.runInAction)(function () {
      _this.statePopup = false;
    });
    return _this;
  }

  (0, _createClass2["default"])(TriggerField, [{
    key: "popup",
    get: function get() {
      return this.statePopup;
    }
  }, {
    key: "saveTrigger",
    value: function saveTrigger(node) {
      this.trigger = node;
    }
  }, {
    key: "isValidationMessageHidden",
    value: function isValidationMessageHidden(message) {
      return (0, _get3["default"])((0, _getPrototypeOf2["default"])(TriggerField.prototype), "isValidationMessageHidden", this).call(this, message) || this.popup;
    }
  }, {
    key: "setPopup",
    value: function setPopup(statePopup) {
      var _this$props$trigger = this.props.trigger,
          trigger = _this$props$trigger === void 0 ? this.getContextConfig('selectTrigger') : _this$props$trigger;

      if (statePopup !== this.statePopup) {
        this.statePopup = statePopup;
        var _this$props$onPopupHi = this.props.onPopupHiddenChange,
            onPopupHiddenChange = _this$props$onPopupHi === void 0 ? _noop["default"] : _this$props$onPopupHi;
        onPopupHiddenChange(!statePopup);

        if (statePopup && !trigger.includes(_enum.Action.hover)) {
          (0, _singleton.hide)();
        }
      }
    }
  }, {
    key: "getRootDomNode",
    value: function getRootDomNode() {
      return (0, _reactDom.findDOMNode)(this);
    }
  }, {
    key: "getOmitPropsKeys",
    value: function getOmitPropsKeys() {
      return (0, _get3["default"])((0, _getPrototypeOf2["default"])(TriggerField.prototype), "getOmitPropsKeys", this).call(this).concat(['popupContent', 'popupCls', 'popupStyle', 'popupPlacement', 'editable', 'trigger', 'triggerShowDelay', 'triggerHiddenDelay', 'onPopupHiddenChange', 'getPopupContainer', 'getPopupAlignTarget', 'extraFooterPlacement', 'renderExtraFooter', 'treeDefaultExpandedKeys', 'treeDefaultExpandAll', 'tabIntoPopupContent', 'viewMode']);
    }
  }, {
    key: "getObservableProps",
    value: function getObservableProps(props, context) {
      return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, (0, _get3["default"])((0, _getPrototypeOf2["default"])(TriggerField.prototype), "getObservableProps", this).call(this, props, context)), {}, {
        viewMode: props.viewMode
      });
    }
  }, {
    key: "getPopupProps",
    value: function getPopupProps() {
      return {
        setValue: this.setValue.bind(this),
        setPopup: this.setPopup.bind(this)
      };
    }
  }, {
    key: "getPopupClassName",
    value: function getPopupClassName(defaultClassName) {
      return defaultClassName;
    }
  }, {
    key: "getPopupWrapper",
    value: function getPopupWrapper() {
      var trigger = this.trigger;

      if (trigger) {
        return trigger.getPopupWrapper();
      }
    }
  }, {
    key: "renderPopupContent",
    value: function renderPopupContent() {
      var popupContent = this.props.popupContent;

      if (popupContent === undefined) {
        return this.getPopupContent();
      }

      if (popupContent instanceof Function) {
        return popupContent(this.getPopupProps());
      }

      return popupContent;
    }
  }, {
    key: "getDefaultAction",
    value: function getDefaultAction() {
      return [_enum.Action.focus, _enum.Action.click];
    }
  }, {
    key: "getWrappedEditor",
    value: function getWrappedEditor(renderedValue) {
      var prefixCls = this.prefixCls,
          _this$props = this.props,
          popupCls = _this$props.popupCls,
          popupStyle = _this$props.popupStyle,
          popupPlacement = _this$props.popupPlacement,
          hidden = _this$props.hidden,
          _this$props$trigger2 = _this$props.trigger,
          trigger = _this$props$trigger2 === void 0 ? this.getDefaultAction() : _this$props$trigger2,
          triggerShowDelay = _this$props.triggerShowDelay,
          triggerHiddenDelay = _this$props.triggerHiddenDelay,
          getPopupContainer = _this$props.getPopupContainer,
          tabIntoPopupContent = _this$props.tabIntoPopupContent,
          _this$props$getPopupA = _this$props.getPopupAlignTarget,
          getPopupAlignTarget = _this$props$getPopupA === void 0 ? this.getRootDomNode : _this$props$getPopupA;
      return /*#__PURE__*/_react["default"].createElement(_Trigger["default"], {
        ref: this.saveTrigger,
        action: this.readOnly || this.disabled ? [] : trigger,
        focusDelay: triggerShowDelay,
        blurDelay: triggerHiddenDelay,
        mouseEnterDelay: triggerShowDelay,
        mouseLeaveDelay: triggerHiddenDelay,
        prefixCls: prefixCls,
        popupCls: this.getPopupClassName(popupCls),
        popupStyle: popupStyle,
        popupContent: this.renderPopupContent,
        popupPlacement: popupPlacement,
        popupHidden: hidden || !this.popup,
        builtinPlacements: _placements["default"],
        onPopupAnimateAppear: this.handlePopupAnimateAppear,
        onPopupAnimateEnd: this.handlePopupAnimateEnd,
        onPopupHiddenChange: this.handlePopupHiddenChange,
        getPopupStyleFromAlign: this.getPopupStyleFromAlign,
        getRootDomNode: getPopupAlignTarget,
        getPopupContainer: getPopupContainer,
        tabIntoPopupContent: tabIntoPopupContent,
        childrenProps: renderedValue,
        getContextConfig: this.getContextConfig
      }, this.getEditor);
    }
  }, {
    key: "getWrapperClassNames",
    value: function getWrapperClassNames() {
      var _get2, _ref;

      var prefixCls = this.prefixCls;

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

      return (_get2 = (0, _get3["default"])((0, _getPrototypeOf2["default"])(TriggerField.prototype), "getWrapperClassNames", this)).call.apply(_get2, [this].concat(args, [(_ref = {}, (0, _defineProperty2["default"])(_ref, "".concat(prefixCls, "-expand"), this.popup), (0, _defineProperty2["default"])(_ref, "".concat(prefixCls, "-not-editable"), !this.disabled && !this.editable), _ref)]));
    }
  }, {
    key: "getDefaultSuffix",
    value: function getDefaultSuffix() {
      var prefixCls = this.prefixCls;
      return /*#__PURE__*/_react["default"].createElement(_icon["default"], {
        type: this.getTriggerIconFont(),
        className: "".concat(prefixCls, "-trigger")
      });
    }
  }, {
    key: "handleTagAnimateEnd",
    value: function handleTagAnimateEnd() {
      this.forcePopupAlign();
    }
  }, {
    key: "handlePopupMouseDown",
    value: function handlePopupMouseDown(e) {
      e.preventDefault();
      var _this$props$onMouseDo = this.props.onMouseDown,
          onMouseDown = _this$props$onMouseDo === void 0 ? _noop["default"] : _this$props$onMouseDo;
      onMouseDown(e);
    }
  }, {
    key: "handlePopupHiddenChange",
    value: function handlePopupHiddenChange(hidden) {
      this.setPopup(!hidden);
    }
  }, {
    key: "forcePositionChanged",
    value: function forcePositionChanged() {
      this.forcePopupAlign();
    }
  }, {
    key: "forcePopupAlign",
    value: function forcePopupAlign() {
      if (this.trigger) {
        this.trigger.forcePopupAlign();
      }
    }
  }, {
    key: "expand",
    value: function expand() {
      var _this2 = this;

      var popupTask = (0, _utils.getIf)(this, 'popupTask', function () {
        return new _TaskRunner["default"]();
      });
      popupTask.cancel();

      if (!this.readOnly && !this.popup) {
        popupTask.delay(this.props.triggerShowDelay, function () {
          _this2.setPopup(true);
        });
      }
    }
  }, {
    key: "collapse",
    value: function collapse() {
      var _this3 = this;

      var popupTask = (0, _utils.getIf)(this, 'popupTask', function () {
        return new _TaskRunner["default"]();
      });
      popupTask.cancel();

      if (!this.readOnly && this.popup) {
        var element = this.element; // 如果当前焦点在popup中，将焦点换给输入框

        var triggerHiddenDelay = this.props.triggerHiddenDelay;

        if (this.isFocused && element !== document.activeElement) {
          element.focus();
          var triggerShowDelay = this.props.triggerShowDelay;
          triggerHiddenDelay += triggerShowDelay;
        }

        popupTask.delay(triggerHiddenDelay, function () {
          _this3.setPopup(false);
        });
      }
    }
  }]);
  return TriggerField;
}(_TextField2.TextField);

exports["default"] = TriggerField;
TriggerField.displayName = 'TriggerField';
TriggerField.defaultProps = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, _TextField2.TextField.defaultProps), {}, {
  suffixCls: 'trigger',
  clearButton: true,
  popupPlacement: 'bottomLeft',
  triggerShowDelay: 150,
  triggerHiddenDelay: 50,
  viewMode: _enum2.TriggerViewMode.popup
});
(0, _tslib.__decorate)([_mobx.observable], TriggerField.prototype, "statePopup", void 0);
(0, _tslib.__decorate)([_autobind["default"]], TriggerField.prototype, "saveTrigger", null);
(0, _tslib.__decorate)([_autobind["default"]], TriggerField.prototype, "isValidationMessageHidden", null);
(0, _tslib.__decorate)([_mobx.action], TriggerField.prototype, "setPopup", null);
(0, _tslib.__decorate)([_autobind["default"]], TriggerField.prototype, "getRootDomNode", null);
(0, _tslib.__decorate)([_autobind["default"]], TriggerField.prototype, "getPopupWrapper", null);
(0, _tslib.__decorate)([_autobind["default"]], TriggerField.prototype, "renderPopupContent", null);
(0, _tslib.__decorate)([_autobind["default"]], TriggerField.prototype, "handleTagAnimateEnd", null);
(0, _tslib.__decorate)([_autobind["default"]], TriggerField.prototype, "handlePopupMouseDown", null);
(0, _tslib.__decorate)([_autobind["default"]], TriggerField.prototype, "handlePopupHiddenChange", null);
//# sourceMappingURL=TriggerField.js.map
