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, _temp2;

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 = (_temp2 = _class = function (_Component) {
  _inherits(Photos, _Component);

  function Photos() {
    var _temp, _this, _ret;

    _classCallCheck(this, Photos);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _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-upload')) {
        // 点击添加
        if (onChoose) onChoose(e);
      } else 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));
      }
    }, _this.onFile = function (e) {
      if (_this.props.onChoose) _this.props.onChoose(e);
      e.target.value = ''; // 防止选择重复图片时不触发
      e.stopPropagation();
    }, _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');
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }
  // 点击整个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,
        upload = _props.upload,
        onChoose = _props.onChoose,
        onDelete = _props.onDelete,
        onClick = _props.onClick,
        others = _objectWithoutProperties(_props, ['list', 'uploading', 'upload', 'onChoose', '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 && list.length > 0 && 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' })
          ),
          item.children
        );
      }),
      onChoose && React.createElement(
        'div',
        { className: 'photos-item photos-upload' },
        !navigator.userAgent.toLowerCase().match(/applewebkit.*mobile.*/) && React.createElement('input', { type: 'file', name: 'uploadPic', onChange: this.onFile, accept: 'image/jpg,image/jpeg,image/png,image/gif,image/bmp' }),
        upload && upload,
        uploading && React.createElement(
          'div',
          { className: 'photos-upload-loading' },
          React.createElement('div', { className: 'photos-upload-loading-icon' })
        )
      )
    );
  };

  return Photos;
}(Component), _class.defaultProps = {}, _temp2);
export { Photos as default };
Photos.propTypes = process.env.NODE_ENV !== "production" ? {
  list: PropTypes.array, // [{thumb: '', src: '', children: node}]
  upload: PropTypes.node, // 上传按钮覆盖的dom
  uploading: PropTypes.bool, // 是否上传中
  onChoose: PropTypes.func, // 浏览器会显示file框onChoose(e), 并监听file框change事件
  onDelete: PropTypes.func,
  onClick: PropTypes.func
} : {};