"use strict";

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

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

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

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

var _configProvider = _interopRequireDefault(require("../config-provider"));

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

var _button = _interopRequireDefault(require("../button"));

var _input = _interopRequireDefault(require("../input"));

var _switch = _interopRequireDefault(require("../switch"));

var _balloon = _interopRequireDefault(require("../balloon"));

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

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

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 _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 Tooltip = _balloon["default"].Tooltip;

var Modal =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Modal, _React$Component);

  function Modal(props) {
    var _this;

    _classCallCheck(this, Modal);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Modal).call(this, props));

    _defineProperty(_assertThisInitialized(_this), "addDocumentEvents", function () {
      if (_this.props.canCloseByEsc) {
        _this._keydownEvents = _util.events.on(document, 'keydown', _this.handleDocumentKeyDown);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleDocumentKeyDown", function (e) {
      if (_this.state.visible && e.keyCode === _util.KEYCODE.ESC) {
        _this.setState({
          visible: false
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "removeDocumentEvents", function () {
      if (_this._keydownEvents) {
        _this._keydownEvents.off();

        _this._keydownEvents = null;
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onSideDrawerVisibleChange", function (checked) {
      _this.props.onSideDrawerVisibleChange(checked);
    });

    _defineProperty(_assertThisInitialized(_this), "onEditTitleChange", function (value) {
      _this.props.onEditTitleChange(value);
    });

    _defineProperty(_assertThisInitialized(_this), "onEnterEditing", function () {
      _this.setState({
        isEditing: true
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onLeaveEditing", function () {
      _this.setState({
        isEditing: false
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onClose", function () {
      var onClose = _this.props.onClose;

      if (!('visible' in _this.props)) {
        _this.setState({
          visible: false
        });
      }

      onClose();
    });

    _defineProperty(_assertThisInitialized(_this), "renderTitle", function () {
      var _this$props = _this.props,
          prefix = _this$props.prefix,
          title = _this$props.title,
          titleEditable = _this$props.titleEditable,
          titleEditTooltip = _this$props.titleEditTooltip,
          titleInputWidth = _this$props.titleInputWidth;
      var isEditing = _this.state.isEditing;
      var editTitle = isEditing ? _react["default"].createElement(_input["default"], {
        size: "xs",
        autoFocus: true,
        value: title,
        onChange: _this.onEditTitleChange,
        onBlur: _this.onLeaveEditing,
        onPressEnter: _this.onLeaveEditing,
        style: {
          width: titleInputWidth
        }
      }) : _react["default"].createElement("span", null, _react["default"].createElement("span", {
        className: "edit-title"
      }, title), _react["default"].createElement(Tooltip, {
        trigger: _react["default"].createElement(_button["default"].Icon, {
          className: "".concat(prefix, "modal-edit-icon"),
          type: "secondary",
          color: "gray",
          name: "pencil",
          onClick: _this.onEnterEditing
        }),
        align: "r"
      }, titleEditTooltip));
      return _react["default"].createElement("div", {
        className: "".concat(prefix, "modal-title")
      }, titleEditable ? editTitle : title);
    });

    _defineProperty(_assertThisInitialized(_this), "renderHeader", function () {
      var _this$props2 = _this.props,
          prefix = _this$props2.prefix,
          description = _this$props2.description,
          operations = _this$props2.operations,
          sideDrawer = _this$props2.sideDrawer,
          sideDrawerLabel = _this$props2.sideDrawerLabel,
          sideDrawerVisible = _this$props2.sideDrawerVisible;

      var title = _this.renderTitle();

      return _react["default"].createElement("div", {
        className: "".concat(prefix, "modal-header")
      }, _react["default"].createElement("div", {
        className: "left-part"
      }, title, _react["default"].createElement("div", {
        className: "".concat(prefix, "modal-description")
      }, description)), _react["default"].createElement("div", {
        className: "right-part"
      }, _react["default"].createElement("div", {
        className: "".concat(prefix, "modal-operations")
      }, operations), sideDrawer ? _react["default"].createElement("div", {
        className: "".concat(prefix, "modal-side-drawer-switch")
      }, _react["default"].createElement("span", {
        className: "".concat(prefix, "modal-side-drawer-switch-label")
      }, sideDrawerLabel), _react["default"].createElement(_switch["default"], {
        size: "small",
        checked: sideDrawerVisible,
        onChange: _this.onSideDrawerVisibleChange
      })) : null, _react["default"].createElement(_button["default"].Icon, {
        className: "".concat(prefix, "modal-close"),
        name: "remove",
        color: "gray",
        onClick: _this.onClose
      })));
    });

    _defineProperty(_assertThisInitialized(_this), "renderBody", function () {
      var _cls;

      var _this$props3 = _this.props,
          prefix = _this$props3.prefix,
          sideDrawer = _this$props3.sideDrawer,
          sideDrawerVisible = _this$props3.sideDrawerVisible,
          sideDrawerWidth = _this$props3.sideDrawerWidth,
          children = _this$props3.children;
      return _react["default"].createElement("div", {
        className: "".concat(prefix, "modal-body")
      }, _react["default"].createElement("div", {
        className: "".concat(prefix, "modal-children")
      }, children), sideDrawer ? _react["default"].createElement("div", {
        className: (0, _classnames["default"])((_cls = {}, _defineProperty(_cls, "".concat(prefix, "modal-side-drawer"), true), _defineProperty(_cls, "".concat(prefix, "modal-side-drawer-visible"), sideDrawerVisible), _cls)),
        style: {
          width: sideDrawerVisible ? sideDrawerWidth : 0
        }
      }, sideDrawer) : null);
    });

    _defineProperty(_assertThisInitialized(_this), "renderModal", function () {
      var _cls2;

      var _this$props4 = _this.props,
          prefix = _this$props4.prefix,
          className = _this$props4.className,
          style = _this$props4.style;

      var header = _this.renderHeader();

      var body = _this.renderBody();

      return _react["default"].createElement("div", {
        className: (0, _classnames["default"])((_cls2 = {}, _defineProperty(_cls2, "".concat(prefix, "modal"), true), _defineProperty(_cls2, className, !!className), _cls2)),
        style: style
      }, header, body);
    });

    _this.state = {
      visible: 'visible' in props ? props.visible : props.defaultVisible || false,
      isEditing: false
    };
    _this.container = document.createElement('div');
    return _this;
  }

  _createClass(Modal, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      document.body.appendChild(this.container);

      if (this.state.visible) {
        document.body.classList.add('next-modal-open');
      }

      this.addDocumentEvents();
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if (nextProps.visible !== this.props.visible) {
        this.setState({
          visible: nextProps.visible
        });
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      if (this.state.visible === false) {
        document.body.classList.remove('next-modal-open');
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      document.body.removeChild(this.container);
      document.body.classList.remove('next-modal-open');
      this.container = null;
      this.removeDocumentEvents();
    }
  }, {
    key: "render",
    value: function render() {
      var visible = this.state.visible;

      if (visible) {
        return _reactDom["default"].createPortal(this.renderModal(), this.container);
      }

      return null;
    }
  }]);

  return Modal;
}(_react["default"].Component);

_defineProperty(Modal, "propTypes", {
  prefix: _propTypes["default"].string,
  style: _propTypes["default"].object,
  className: _propTypes["default"].string,

  /**
   * 是否默认可见，非受控
   */
  defaultVisible: _propTypes["default"].bool,

  /**
   * 是否可见，受控
   */
  visible: _propTypes["default"].bool,

  /**
   * 点击关闭按钮回调
   */
  onClose: _propTypes["default"].func,

  /**
   * Modal标题
   */
  title: _propTypes["default"].string.isRequired,

  /**
   * Modal标题是否可编辑
   */
  titleEditable: _propTypes["default"].bool,

  /**
   * 标题编辑图标提示文案
   */
  titleEditTooltip: _propTypes["default"].string,

  /**
   * 可编辑时标题输入框的宽度
   */
  titleInputWidth: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].number]),

  /**
   * 标题编辑回调
   */
  onEditTitleChange: _propTypes["default"].func,

  /**
   * Modal描述
   */
  description: _propTypes["default"].string,

  /**
   * 操作区
   */
  operations: _propTypes["default"].element,

  /**
   * 侧边栏内容
   */
  sideDrawer: _propTypes["default"].node,

  /**
   * 侧边栏内容宽度
   */
  sideDrawerWidth: _propTypes["default"].number,

  /**
   * 侧边栏切换显示Switch前label
   */
  sideDrawerLabel: _propTypes["default"].string,

  /**
   * 侧边栏内容是否可见
   */
  sideDrawerVisible: _propTypes["default"].bool,

  /**
   * 侧边栏内容切换可见回调
   */
  onSideDrawerVisibleChange: _propTypes["default"].func,

  /**
   * 是否支持 esc 按键关闭弹层
   */
  canCloseByEsc: _propTypes["default"].bool
});

_defineProperty(Modal, "defaultProps", {
  prefix: 'next-',
  sideDrawerWidth: 400,
  titleEditTooltip: '点击即可编辑',
  onClose: function onClose() {},
  onSideDrawerVisibleChange: function onSideDrawerVisibleChange() {},
  onEditTitleChange: function onEditTitleChange() {},
  canCloseByEsc: true
});

var _default = _configProvider["default"].config(Modal);

exports["default"] = _default;