'use strict';

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

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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _SettingButton = require('../components/Button/SettingButton');

var _SettingButton2 = _interopRequireDefault(_SettingButton);

var _CancelButton = require('../components/Button/CancelButton');

var _CancelButton2 = _interopRequireDefault(_CancelButton);

var _ResizeButton = require('../components/Button/ResizeButton');

var _ResizeButton2 = _interopRequireDefault(_ResizeButton);

var _constant = require('../constant');

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

var _common = require('./common');

var _board = require('../components/DashBoard/board');

var _Setting = require('../components/Setting');

var _Setting2 = _interopRequireDefault(_Setting);

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; } /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                * Created by baoyinghai on 5/26/17.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                */


exports.default = function (Comp) {
  var Setable = function (_React$Component) {
    _inherits(Setable, _React$Component);

    function Setable() {
      var _ref;

      var _temp, _this, _ret;

      _classCallCheck(this, Setable);

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

      return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Setable.__proto__ || Object.getPrototypeOf(Setable)).call.apply(_ref, [this].concat(args))), _this), _this.popSettingModal = function (e) {
        e.stopPropagation();
        var title = _this.props.title;
        var settingProps = _this.props.setting;
        var body = function body(props) {
          return _react2.default.createElement(_Setting2.default, _extends({}, settingProps, { title: _this.props.title, chartSetting: _this.props.chartSetting, index: _this.props.index, updateSetting: _this.props.updateSetting, onClick: props.callback, createBottomButtons: props.createBottomButtons, dataSource: _this.props.dataSource }));
        };
        (0, _util.popModal)(title, body);
      }, _this.removeCard = function (e) {
        e.stopPropagation();
        (0, _util.popConfirm)('确定删除吗？', function () {
          var removeCard = _this.props.removeCard;

          removeCard && removeCard(_this.props.index);
        }, function () {});
      }, _this.addBodyClass = function () {
        var body = document.querySelector('body');
        _this.cursor = body.style.cursor;
        body.style.cursor = 'se-resize';
      }, _this.removeBodyClass = function () {
        var body = document.querySelector('body');
        body.style.cursor = _this.cursor;
      }, _temp), _possibleConstructorReturn(_this, _ret);
    }

    _createClass(Setable, [{
      key: 'render',
      value: function render() {
        var _this2 = this;

        var _props = this.props,
            layout = _props.layout,
            updateLayout = _props.updateLayout,
            index = _props.index,
            calculateHitBitBeside = _props.calculateHitBitBeside,
            refStr = _props.refStr;

        var style = (0, _common.calculateStyle)(layout);
        var event = {
          onMouseDown: function onMouseDown(e) {
            e.stopPropagation();
            _this2.addBodyClass();
            var hitArr = calculateHitBitBeside(index);
            var startClientX = e.clientX;
            var startClientY = e.clientY;
            var wrapper = document.querySelector('.' + refStr + '.move-container');
            var cloneWrapper = (0, _common.createShadow)(style);
            wrapper.parentNode.appendChild(cloneWrapper);
            cloneWrapper.style.zIndex = 100;
            var offsetWidth = 0;
            var offsetHeight = 0;
            var mouseMoveHandler = function mouseMoveHandler(moveEvent) {
              moveEvent.stopPropagation();
              offsetWidth = Math.floor((moveEvent.clientX - startClientX) / _constant.BIT);
              offsetHeight = Math.floor((moveEvent.clientY - startClientY) / _constant.BIT);
              cloneWrapper.style.width = offsetWidth * _constant.BIT + style.width + 'px';
              cloneWrapper.style.height = offsetHeight * _constant.BIT + style.height + 'px';
            };
            var mouseUpHandler = function mouseUpHandler(upEvent) {
              upEvent.stopPropagation();
              _this2.removeBodyClass();
              // wrapper.style.zIndex = 0;
              wrapper.parentNode.removeChild(cloneWrapper);
              window.removeEventListener('mousemove', mouseMoveHandler);
              window.removeEventListener('mouseup', mouseUpHandler);
              var width = Math.max(layout.width + offsetWidth, 1);
              var height = Math.max(layout.height + offsetHeight, 1);
              if (!(0, _board.isOccupy)(layout.x, layout.y, width, height, hitArr)) {
                updateLayout(index, _extends({}, layout, { width: width, height: height }));
              }
            };
            window.addEventListener('mousemove', mouseMoveHandler);
            window.addEventListener('mouseup', mouseUpHandler);
          }
        };
        return _react2.default.createElement(
          'div',
          { className: 'setting-container' },
          _react2.default.createElement(
            'div',
            { className: 'setting-bar' },
            _react2.default.createElement(_SettingButton2.default, { onMouseDown: this.popSettingModal }),
            _react2.default.createElement(_CancelButton2.default, { onMouseDown: this.removeCard })
          ),
          this.props.exception ? _react2.default.createElement(
            'span',
            { className: 'load-data-exception' },
            '\u6570\u636E\u52A0\u8F7D\u5F02\u5E38'
          ) : _react2.default.createElement(Comp, this.props),
          _react2.default.createElement(_ResizeButton2.default, event)
        );
      }
    }]);

    return Setable;
  }(_react2.default.Component);

  ;

  return Setable;
};