"use strict";

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

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

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

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

var _util2 = 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 _extends() { _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; }; return _extends.apply(this, arguments); }

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 noop = _util.func.noop;
/**
 * Upload.Selecter
 * @description [底层能力] 可自定义样式的文件选择器
 */

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

  function Selecter() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, Selecter);

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

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(Selecter)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "onSelect", function (e) {
      var files = e.target.files;
      var filesArr = files.length ? Array.prototype.slice.call(files) : [files];
      filesArr.forEach(function (file) {
        file.uid = (0, _util2.uid)();
      });

      _this.props.onSelect(filesArr);
    });

    _defineProperty(_assertThisInitialized(_this), "onClick", function () {
      var el = _this.fileRef;

      if (!el) {
        return;
      }

      el.click();
      el.value = '';
    });

    _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
      if (e.key === 'Enter') {
        _this.onClick();
      }
    });

    _defineProperty(_assertThisInitialized(_this), "onDrop", function (e) {
      e.preventDefault();
      var files = e.dataTransfer.files;
      var filesArr = Array.prototype.slice.call(files);

      _this.props.onDrop(filesArr);
    });

    _defineProperty(_assertThisInitialized(_this), "onDragOver", function (e) {
      e.preventDefault();

      _this.props.onDragOver(e);
    });

    _defineProperty(_assertThisInitialized(_this), "preventDragOver", function (e) {
      e.preventDefault();
      e.stopPropagation();
      e.dataTransfer.dropEffect = 'none';
    });

    _defineProperty(_assertThisInitialized(_this), "saveFileRef", function (ref) {
      _this.fileRef = ref;
    });

    return _this;
  }

  _createClass(Selecter, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var disabled = this.props.disabled;

      if (disabled) {
        window.addEventListener('dragover', this.preventDragOver, false);
      } else {
        window.removeEventListener('dragover', this.preventDragOver, false);
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      var disabled = this.props.disabled;

      if (disabled) {
        window.addEventListener('dragover', this.preventDragOver, false);
      } else {
        window.removeEventListener('dragover', this.preventDragOver, false);
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      window.removeEventListener('dragover', this.preventDragOver, false);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          accept = _this$props.accept,
          multiple = _this$props.multiple,
          children = _this$props.children,
          id = _this$props.id,
          disabled = _this$props.disabled,
          dragable = _this$props.dragable,
          style = _this$props.style,
          className = _this$props.className,
          name = _this$props.name;
      var events = {};

      if (!disabled) {
        events = _extends({
          onClick: this.onClick,
          onKeyDown: this.onKeyDown,
          tabIndex: '0'
        }, dragable ? {
          onDrop: this.onDrop,
          onDragOver: this.onDragOver,
          onDragLeave: this.props.onDragLeave
        } : {});
      }

      return _react["default"].createElement("div", _extends({
        role: "application",
        style: style,
        className: className
      }, events), _react["default"].createElement("input", {
        type: "file",
        name: name,
        id: id,
        ref: this.saveFileRef,
        style: {
          display: 'none'
        },
        accept: accept,
        "aria-hidden": true,
        multiple: multiple,
        onChange: this.onSelect,
        disabled: disabled
      }), children);
    }
  }]);

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

exports["default"] = Selecter;

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

  /**
   * 是否禁用上传功能
   */
  disabled: _propTypes["default"].bool,

  /**
   * 是否支持多选文件，`ie10+` 支持。开启后按住 ctrl 可选择多个文件
   */
  multiple: _propTypes["default"].bool,

  /**
   * 是否支持拖拽上传，`ie10+` 支持。
   */
  dragable: _propTypes["default"].bool,

  /**
   * 接受上传的文件类型 (image/png, image/jpg, .doc, .ppt) 详见 [input accept attribute](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/Input#attr-accept)
   */
  accept: _propTypes["default"].string,

  /**
   * 文件选择回调
   */
  onSelect: _propTypes["default"].func,

  /**
   * 拖拽经过回调
   */
  onDragOver: _propTypes["default"].func,

  /**
   * 拖拽离开回调
   */
  onDragLeave: _propTypes["default"].func,

  /**
   * 拖拽完成回调
   */
  onDrop: _propTypes["default"].func,
  children: _propTypes["default"].node,
  name: _propTypes["default"].string
});

_defineProperty(Selecter, "defaultProps", {
  name: 'file',
  multiple: false,
  onSelect: noop,
  onDragOver: noop,
  onDragLeave: noop,
  onDrop: noop
});