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 _class, _temp;

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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

import React, { Component } from 'react';
import PropTypes from 'prop-types';

var Photos = (_temp = _class = function (_Component) {
  _inherits(Photos, _Component);

  function Photos(props) {
    _classCallCheck(this, Photos);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props));

    _this.onClick = function (e) {
      var _this$props = _this.props,
          list = _this$props.list,
          onChoose = _this$props.onChoose,
          onDelete = _this$props.onDelete,
          onClick = _this$props.onClick;

      var target = e.target;
      if (target.classList.contains('photos-item')) {
        // 点击照片
        var index = target.getAttribute('data-index');
        if (index && onClick) onClick(e, list[index], Number(index));
      } else if (target.classList.contains('photos-delete')) {
        // 点击删除
        var _index = target.parentNode.getAttribute('data-index');
        if (_index && onDelete) onDelete(e, list[_index], Number(_index));
      } else if (target.classList.contains('photos-upload')) {
        // 点击添加
        if (onChoose) onChoose(e);
      }
    };

    _this.onFile = function (e) {
      if (_this.props.onFile) _this.props.onFile(e);
      e.target.value = ''; // 防止选择重复图片时不触发
    };

    _this.onLoad = function (e) {
      var target = e.target;
      target.parentNode.setAttribute('data-complete', '1');
    };

    _this.onError = function (e) {
      var target = e.target;
      target.parentNode.setAttribute('data-complete', '0');
    };

    return _this;
  }
  // 点击整个photos容器

  // 点击file框

  // 图片加载完成

  // 图片加载失败


  Photos.prototype.render = function render() {
    var _this2 = this;

    var _props = this.props,
        _props$list = _props.list,
        list = _props$list === undefined ? [] : _props$list,
        uploading = _props.uploading,
        onChoose = _props.onChoose,
        onFile = _props.onFile,
        onDelete = _props.onDelete,
        onClick = _props.onClick,
        others = _objectWithoutProperties(_props, ['list', 'uploading', 'onChoose', 'onFile', 'onDelete', 'onClick']);

    return React.createElement(
      'div',
      _extends({
        ref: function ref(el) {
          _this2.$el = el;
        }
      }, others, {
        className: 'photos' + (others.className ? ' ' + others.className : ''),
        onClick: this.onClick
      }),
      list.map(function (item, index) {
        return React.createElement(
          'div',
          {
            key: index,
            'data-index': index,
            className: 'photos-item' + (item.className ? ' ' + item.className : ''),
            style: Object.assign({ backgroundImage: 'url(' + item.thumb + ')' }, item.style || {})
          },
          React.createElement('img', { className: 'photos-item-img', src: item.thumb, alt: '', onLoad: _this2.onLoad, onError: _this2.onError }),
          React.createElement('div', { className: 'photos-item-error' }),
          React.createElement('div', { className: 'photos-item-load' }),
          onDelete && React.createElement(
            'div',
            { className: 'photos-delete' },
            React.createElement('div', { className: 'photos-delete-icon' })
          )
        );
      }),
      (onChoose || onFile) && React.createElement(
        'div',
        { className: 'photos-item photos-upload' },
        onFile && React.createElement('input', { type: 'file', name: 'uploadPic', onChange: this.onFile, accept: 'image/jpg,image/jpeg,image/png,image/gif,image/bmp' }),
        uploading && React.createElement(
          'div',
          { className: 'photos-upload-loading' },
          React.createElement('div', { className: 'photos-upload-loading-icon' })
        )
      )
    );
  };

  return Photos;
}(Component), _class.defaultProps = {}, _temp);
export { Photos as default };
Photos.propTypes = process.env.NODE_ENV !== "production" ? {
  list: PropTypes.array, // [{thumb: '', src: ''}]
  uploading: PropTypes.bool, // 是否上传中
  onChoose: PropTypes.func,
  onFile: PropTypes.func, // 显示file框, 并监听file框change事件
  onDelete: PropTypes.func,
  onClick: PropTypes.func
} : {};