'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = 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 _class, _temp2; /* eslint-disable */


var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _rcDialog = require('rc-dialog');

var _rcDialog2 = _interopRequireDefault(_rcDialog);

var _addEventListener = require('rc-util/lib/Dom/addEventListener');

var _addEventListener2 = _interopRequireDefault(_addEventListener);

var _index = require('../index');

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

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

var mousePosition = void 0;
var mousePositionEventBinded = void 0;

// export interface ModalProps {
//  /** 对话框是否可见*/
//  visible?: boolean;
//  /** 确定按钮 loading*/
//  confirmLoading?: boolean;
//  /** 标题*/
//  title?: React.ReactNode | string;
//  /** 是否显示右上角的关闭按钮*/
//  closable?: boolean;
//  /** 点击确定回调*/
//  onOk?: () => void;
//  /** 点击模态框右上角叉、取消按钮、Props.maskClosable 值为 true 时的遮罩层或键盘按下 Esc 时的回调*/
//  onCancel?: (e: React.MouseEvent<any>) => void;
//  /** 宽度*/
//  width?: string | number;
//  /** 底部内容*/
//  footer?: React.ReactNode;
//  /** 确认按钮文字*/
//  okText?: string;
//  /** 取消按钮文字*/
//  cancelText?: string;
//  /** 点击蒙层是否允许关闭*/
//  maskClosable?: boolean;
//  style?: React.CSSProperties;
//  wrapClassName?: string;
//  maskTransitionName?: string;
//  transitionName?: string;
//  className?: string;
// }


var Modal = (_temp2 = _class = function (_React$Component) {
  _inherits(Modal, _React$Component);

  function Modal() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, Modal);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Modal.__proto__ || Object.getPrototypeOf(Modal)).call.apply(_ref, [this].concat(args))), _this), _this.handleCancel = function (e) {
      var onCancel = _this.props.onCancel;
      if (onCancel) {
        onCancel(e);
      }
    }, _this.handleOk = function () {
      var onOk = _this.props.onOk;
      if (onOk) {
        onOk();
      }
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Modal, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      if (mousePositionEventBinded) {
        return;
      }
      // 只有点击事件支持从鼠标位置动画展开
      (0, _addEventListener2.default)(document.documentElement, 'click', function (e) {
        mousePosition = {
          x: e.pageX,
          y: e.pageY
        };
        // 100ms 内发生过点击事件，则从点击位置动画展示
        // 否则直接 zoom 展示
        // 这样可以兼容非点击方式展开
        setTimeout(function () {
          return mousePosition = null;
        }, 100);
      });
      mousePositionEventBinded = true;
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          okText = _props.okText,
          cancelText = _props.cancelText,
          confirmLoading = _props.confirmLoading,
          footer = _props.footer,
          visible = _props.visible;


      if (this.context.antLocale && this.context.antLocale.Modal) {
        okText = okText || this.context.antLocale.Modal.okText;
        cancelText = cancelText || this.context.antLocale.Modal.cancelText;
      }

      var defaultFooter = [_react2.default.createElement(
        _index.Button,
        {
          key: 'cancel',
          type: 'ghost',
          size: 'large',
          onClick: this.handleCancel
        },
        cancelText || '取消'
      ), _react2.default.createElement(
        _index.Button,
        {
          key: 'confirm',
          type: 'primary',
          size: 'large',
          loading: confirmLoading,
          onClick: this.handleOk
        },
        okText || '确定'
      )];

      return _react2.default.createElement(_rcDialog2.default, _extends({
        onClose: this.handleCancel,
        footer: footer || defaultFooter
      }, this.props, {
        visible: visible,
        mousePosition: mousePosition
      }));
    }
  }]);

  return Modal;
}(_react2.default.Component), _class.defaultProps = {
  prefixCls: 'mx-modal-self',
  width: 520,
  transitionName: 'zoom',
  maskTransitionName: 'fade',
  confirmLoading: false,
  visible: false
}, _class.propTypes = {
  prefixCls: _react.PropTypes.string,
  onOk: _react.PropTypes.func,
  onCancel: _react.PropTypes.func,
  okText: _react.PropTypes.node,
  cancelText: _react.PropTypes.node,
  width: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
  confirmLoading: _react.PropTypes.bool,
  visible: _react.PropTypes.bool,
  align: _react.PropTypes.object,
  footer: _react.PropTypes.node,
  title: _react.PropTypes.node,
  closable: _react.PropTypes.bool
}, _class.contextTypes = {
  antLocale: _react2.default.PropTypes.object
}, _temp2);
exports.default = Modal;