"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

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

var _useUpdateEffect = _interopRequireDefault(require("ahooks/es/useUpdateEffect"));

var _useSetState5 = _interopRequireDefault(require("ahooks/es/useSetState"));

var _CloseOutlined = _interopRequireDefault(require("@ant-design/icons/CloseOutlined"));

var _FullscreenExitOutlined = _interopRequireDefault(require("@ant-design/icons/FullscreenExitOutlined"));

var _FullscreenOutlined = _interopRequireDefault(require("@ant-design/icons/FullscreenOutlined"));

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

var _antd = require("antd");

var _Title = _interopRequireDefault(require("./Title"));

require("./style");

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var TabPane = _antd.Tabs.TabPane; // 对话框位置初始参数

var initDragArg = {
  init_x: 0,
  init_y: 0,
  x_move: 0,
  y_move: 0,
  documentMouseMove: null,
  documentMouseUp: null
}; // 全屏参数

var initFullScreen = {
  isFull: true,
  className: 'vtx-modal-maxClass'
};

var VtxFullScreenModal = function VtxFullScreenModal(props) {
  var _useSetState = (0, _useSetState5["default"])(initFullScreen),
      _useSetState2 = _slicedToArray(_useSetState, 2),
      fullScreen = _useSetState2[0],
      setFullScreen = _useSetState2[1]; // 是否是全屏状态


  var _useSetState3 = (0, _useSetState5["default"])(initDragArg),
      _useSetState4 = _slicedToArray(_useSetState3, 2),
      dragArg = _useSetState4[0],
      setDragArg = _useSetState4[1]; // 拖动配置


  var classId = (0, _react.useRef)(new Date().getTime() + Math.random());
  var _props$wrapClassName = props.wrapClassName,
      wrapClassName = _props$wrapClassName === void 0 ? '' : _props$wrapClassName,
      _props$closable = props.closable,
      closable = _props$closable === void 0 ? true : _props$closable,
      _props$title = props.title,
      title = _props$title === void 0 ? '' : _props$title,
      _props$maximize = props.maximize,
      maximize = _props$maximize === void 0 ? true : _props$maximize,
      onModalResize = props.onModalResize,
      _props$moveable = props.moveable,
      moveable = _props$moveable === void 0 ? true : _props$moveable,
      _props$tabs = props.tabs,
      tabs = _props$tabs === void 0 ? [] : _props$tabs; // 监听对话框大小变化回调(忽略首次执行)

  (0, _useUpdateEffect["default"])(function () {
    var timer = setTimeout(function () {
      if (onModalResize && typeof onModalResize === 'function') {
        var el = document.getElementsByClassName('ant-modal-body')[0];
        var option = {
          height: window.getComputedStyle(el).height,
          isFull: fullScreen.isFull
        };
        onModalResize(option);
      }
    }, 100);
    return function () {
      return clearTimeout(timer);
    };
  }, [fullScreen.isFull]); // 拖拽

  (0, _react.useEffect)(function () {
    if (props.visible && moveable) {
      var timer = setTimeout(function () {
        var _document$getElements, _document$getElements2, _document$getElements3;

        var modalHead = (_document$getElements = document.getElementsByClassName(classId.current)) === null || _document$getElements === void 0 ? void 0 : (_document$getElements2 = _document$getElements[0]) === null || _document$getElements2 === void 0 ? void 0 : (_document$getElements3 = _document$getElements2.getElementsByClassName('ant-modal-header')) === null || _document$getElements3 === void 0 ? void 0 : _document$getElements3[0];

        if (modalHead) {
          modalHead.style.cursor = 'move';
          modalHead.onmousedown = startDrag;
        }
      }, 200);
      return function () {
        return clearTimeout(timer);
      };
    }
  });

  var startDrag = function startDrag(e) {
    e.preventDefault(); // 全屏模式下禁止拖拽

    if (!fullScreen.isFull) {
      setDragArg({
        documentMouseUp: document.onmouseup,
        documentMouseMove: document.onmousemove,
        init_x: e.clientX - dragArg.x_move,
        init_y: e.clientY - dragArg.y_move
      });

      document.onmousemove = function (e) {
        setDragArg(function (prev) {
          return {
            x_move: e.clientX - prev.init_x,
            y_move: e.clientY - prev.init_y
          };
        });
      };

      document.onmouseup = function () {
        document.onmousemove = dragArg.documentMouseMove;
        document.onmouseup = dragArg.documentMouseUp;
      };
    }
  }; // 关闭对话框，初始化位置


  var onCancel = function onCancel() {
    if (props.onCancel && typeof props.onCancel === 'function') {
      props.onCancel(); // TODO 关闭弹框会闪一下,待解决

      setDragArg(initDragArg);
      setFullScreen(initFullScreen);
    }
  };
  /*
   *重写title，加入对话框全屏功能
   */


  title = function renderTitle() {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "vtx-modal-title",
      style: {
        paddingRight: closable ? '32px' : '0px'
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "vtx-modal-title_name"
    }, title), maximize ? /*#__PURE__*/_react["default"].createElement("div", {
      className: "vtx-modal-maximizeIcon"
    }, /*#__PURE__*/_react["default"].createElement("p", {
      onClick: function onClick() {
        var fullScreenClass = '';

        if (!fullScreen.isFull) {
          fullScreenClass = 'vtx-modal-maxClass';
        }

        setFullScreen({
          isFull: !fullScreen.isFull,
          className: fullScreenClass
        });
      }
    }, fullScreen.isFull ? /*#__PURE__*/_react["default"].createElement(_FullscreenExitOutlined["default"], null) : /*#__PURE__*/_react["default"].createElement(_FullscreenOutlined["default"], null))) : null, closable ? /*#__PURE__*/_react["default"].createElement("div", {
      className: "vtx-modal-close"
    }, /*#__PURE__*/_react["default"].createElement("p", {
      onClick: onCancel
    }, /*#__PURE__*/_react["default"].createElement(_CloseOutlined["default"], null))) : '');
  }();

  var bodyStyle = _objectSpread({}, props.bodyStyle);
  /* 限制最大高度 */


  if (!fullScreen.isFull) {
    bodyStyle = _objectSpread({
      maxHeight: "".concat(window.innerHeight * 0.7, "px")
    }, bodyStyle);
  }

  var transformStyle = {
    transform: "translate(".concat(dragArg.x_move, "px,").concat(dragArg.y_move, "px)")
  };

  var newProps = _objectSpread(_objectSpread({
    maskClosable: false,
    destroyOnClose: true,
    width: 700
  }, props), {}, {
    closable: false,
    title: title,
    wrapClassName: (0, _classnames["default"])('vtx-modal', wrapClassName, fullScreen.className, classId.current),
    bodyStyle: bodyStyle,
    onCancel: onCancel,
    style: _objectSpread(_objectSpread({}, props.style), fullScreen.isFull ? {} : transformStyle)
  });

  return /*#__PURE__*/_react["default"].createElement(_antd.Modal, newProps, tabs.length === 0 ? props.children : /*#__PURE__*/_react["default"].createElement(_antd.Tabs, {
    defaultActiveKey: 1
  }, tabs.map(function (tab) {
    return /*#__PURE__*/_react["default"].createElement(TabPane, {
      tab: tab.value,
      key: tab.value
    }, tab.content);
  })));
};

VtxFullScreenModal.info = _antd.Modal.info;
VtxFullScreenModal.success = _antd.Modal.success;
VtxFullScreenModal.error = _antd.Modal.error;
VtxFullScreenModal.warning = _antd.Modal.warning;
VtxFullScreenModal.confirm = _antd.Modal.confirm;
VtxFullScreenModal.propTypes = {
  visible: _propTypes["default"].bool,
  wrapClassName: _propTypes["default"].string,
  closable: _propTypes["default"].bool,
  title: _propTypes["default"].oneOfType([_propTypes["default"].element, _propTypes["default"].string]),
  maximize: _propTypes["default"].bool,
  onModalResize: _propTypes["default"].func,
  moveable: _propTypes["default"].bool,
  onCancel: _propTypes["default"].func,
  bodyStyle: _propTypes["default"].object,
  style: _propTypes["default"].object,
  tabs: _propTypes["default"].array,
  children: _propTypes["default"].node
};
VtxFullScreenModal.Title = _Title["default"];
var size = {
  xsmall: 392,
  small: 500,
  middle: 744,
  large: 1096
};
VtxFullScreenModal.size = size;
var _default = VtxFullScreenModal;
exports["default"] = _default;
//# sourceMappingURL=index.js.map
