"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _extends2 = require("babel-runtime/helpers/extends");

var _extends3 = _interopRequireDefault(_extends2);

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

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

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

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn");

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

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

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _modal = require("antd/lib/modal");

var _modal2 = _interopRequireDefault(_modal);

var _pagePointEnhanced = require("../page-point/pagePointEnhanced");

var _pagePointEnhanced2 = _interopRequireDefault(_pagePointEnhanced);

var _context = require("antd/lib/config-provider/context");

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

require("antd/lib/modal/style");

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

var __rest = undefined && undefined.__rest || function (s, e) {
    var t = {};
    for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
    }if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
        if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
    }
    return t;
};
var confirm = _modal2["default"].confirm,
    info = _modal2["default"].info,
    success = _modal2["default"].success,
    error = _modal2["default"].error,
    warning = _modal2["default"].warning;

var destroyAll = _modal2["default"].destroyAll;

var Modal = function (_Component) {
    (0, _inherits3["default"])(Modal, _Component);

    function Modal(props) {
        (0, _classCallCheck3["default"])(this, Modal);

        var _this = (0, _possibleConstructorReturn3["default"])(this, (Modal.__proto__ || Object.getPrototypeOf(Modal)).call(this, props));

        _this.mouseDownX = 0;
        _this.mouseDownY = 0;
        _this.deltaX = 0;
        _this.deltaY = 0;
        _this.sumX = 0;
        _this.sumY = 0;
        _this.state = {
            visible: false
        };
        _this.handleMove = function (event) {
            var deltaX = event.pageX - _this.mouseDownX;
            var deltaY = event.pageY - _this.mouseDownY;
            _this.deltaX = deltaX;
            _this.deltaY = deltaY;
            _this.modalContent.style.transform = "translate(" + (deltaX + _this.sumX) + "px, " + (deltaY + _this.sumY) + "px)";
        };
        _this.show = function () {
            _this.setState({ visible: true });
        };
        _this.hide = function () {
            _this.setState({ visible: false });
        };
        _this.initialEvent = function (visible) {
            var title = _this.props.title;

            var prefix = _this.context ? _this.context.getPrefixCls('modal') : 'ant-modal';
            if (title && visible) {
                setTimeout(function () {
                    window.removeEventListener("mouseup", _this.removeUp, false);
                    _this.contain = document.getElementsByClassName(_this.simpleClass)[0];
                    _this.header = _this.contain.getElementsByClassName(prefix + "-header")[0];
                    _this.modalContent = _this.contain.getElementsByClassName(prefix + "-content")[0];
                    if (_this.header) {
                        _this.header.style.cursor = "all-scroll";
                        _this.header.onmousedown = function (e) {
                            _this.mouseDownX = e.pageX;
                            _this.mouseDownY = e.pageY;
                            document.body.onselectstart = function () {
                                return false;
                            };
                            window.addEventListener("mousemove", _this.handleMove, false);
                        };
                    }
                    window.addEventListener("mouseup", _this.removeUp, false);
                }, 0);
            }
            if (!visible) {
                _this.mouseDownX = 0;
                _this.mouseDownY = 0;
                _this.deltaX = 0;
                _this.deltaY = 0;
                _this.sumX = 0;
                _this.sumY = 0;
            }
        };
        _this.removeMove = function () {
            window.removeEventListener("mousemove", _this.handleMove, false);
        };
        _this.removeUp = function () {
            document.body.onselectstart = function () {
                return true;
            };
            _this.sumX = _this.sumX + _this.deltaX;
            _this.sumY = _this.sumY + _this.deltaY;
            _this.removeMove();
        };
        _this.simpleClass = Math.random().toString(36).substring(2);
        return _this;
    }

    (0, _createClass3["default"])(Modal, [{
        key: "componentDidMount",
        value: function componentDidMount() {
            var _props$draggable = this.props.draggable,
                draggable = _props$draggable === undefined ? true : _props$draggable;

            if (draggable) {
                this.initialEvent(this.props.visible);
            }
        }
    }, {
        key: "componentWillUnmount",
        value: function componentWillUnmount() {
            var _props$draggable2 = this.props.draggable,
                draggable = _props$draggable2 === undefined ? true : _props$draggable2;

            if (draggable) {
                this.removeMove();
                window.removeEventListener("mouseup", this.removeUp, false);
            }
        }
    }, {
        key: "componentDidUpdate",
        value: function componentDidUpdate() {
            var _props$draggable3 = this.props.draggable,
                draggable = _props$draggable3 === undefined ? true : _props$draggable3;

            if (draggable) {
                this.initialEvent(this.props.visible);
            }
        }
    }, {
        key: "render",
        value: function render() {
            var _this2 = this;

            var _a = this.props,
                children = _a.children,
                wrapClassName = _a.wrapClassName,
                other = __rest(_a, ["children", "wrapClassName"]);
            var defaultOpts = {
                destroyOnClose: true
            };
            var opts = (0, _extends3["default"])((0, _extends3["default"])({}, defaultOpts), other);
            if (!Object.prototype.hasOwnProperty.call(opts, 'visible') || typeof opts.visible !== 'boolean') {
                opts.visible = this.state.visible;
            }
            var wrapModalClassName = wrapClassName ? wrapClassName + " " + this.simpleClass : "" + this.simpleClass;
            return _react2["default"].createElement(
                _configProvider.ConfigConsumer,
                null,
                function (_ref) {
                    var pagePoint = _ref.pagePoint;
                    return pagePoint ? _react2["default"].createElement(_pagePointEnhanced2["default"], (0, _extends3["default"])({ compType: "Modal" }, _this2.props, { render: function render(eventprops) {
                            return _react2["default"].createElement(
                                _modal2["default"],
                                (0, _extends3["default"])({}, opts, eventprops, { wrapClassName: wrapModalClassName, "data-ui-role": "Modal" }),
                                children
                            );
                        } })) : _react2["default"].createElement(
                        _modal2["default"],
                        (0, _extends3["default"])({}, opts, { wrapClassName: wrapModalClassName }),
                        children
                    );
                }
            );
        }
    }], [{
        key: "getDerivedStateFromProps",
        value: function getDerivedStateFromProps(nextProps, prevState) {
            var visible = nextProps.visible;

            if (typeof visible === 'boolean' && visible !== prevState.visible) {
                return {
                    visible: visible
                };
            }
            return null;
        }
    }]);
    return Modal;
}(_react.Component);

exports["default"] = Modal;

Modal.destroyAll = destroyAll;
Modal.contextType = _context.ConfigContext;
Modal.info = info;
Modal.confirm = confirm;
Modal.success = success;
Modal.error = error;
Modal.warning = warning;
module.exports = exports['default'];