'use strict';

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

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

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 React = _interopRequireWildcard(_react);

var _rcAnimate = require('rc-animate');

var _rcAnimate2 = _interopRequireDefault(_rcAnimate);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _configProvider = require('antd/lib/config-provider');

var _utils = require('./utils');

var _icon = require('../icon');

var _icon2 = _interopRequireDefault(_icon);

var _tooltip = require('../tooltip');

var _tooltip2 = _interopRequireDefault(_tooltip);

var _progress = require('../progress');

var _progress2 = _interopRequireDefault(_progress);

var _reactNode = require('../_util/reactNode');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }

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

var UploadList = function (_React$Component) {
    (0, _inherits3['default'])(UploadList, _React$Component);

    function UploadList() {
        (0, _classCallCheck3['default'])(this, UploadList);

        var _this = (0, _possibleConstructorReturn3['default'])(this, (UploadList.__proto__ || Object.getPrototypeOf(UploadList)).apply(this, arguments));

        _this.handlePreview = function (file, e) {
            var onPreview = _this.props.onPreview;

            if (!onPreview) {
                return;
            }
            e.preventDefault();
            return onPreview(file);
        };
        _this.handleDownload = function (file) {
            var onDownload = _this.props.onDownload;

            if (typeof onDownload === 'function') {
                onDownload(file);
            } else if (file.url) {
                window.open(file.url);
            }
        };
        _this.handleClose = function (file) {
            var onRemove = _this.props.onRemove;

            if (onRemove) {
                onRemove(file);
            }
        };
        _this.handleActionIconRender = function (customIcon, callback) {
            var btnProps = {
                onClick: function onClick(e) {
                    callback();
                    if ((0, _reactNode.isValidElement)(customIcon) && customIcon.props.onClick) {
                        customIcon.props.onClick(e);
                    }
                }
            };
            if ((0, _reactNode.isValidElement)(customIcon)) {
                var btnIcon = (0, _reactNode.cloneElement)(customIcon, (0, _extends3['default'])((0, _extends3['default'])({}, customIcon.props), btnProps));
                return btnIcon;
            }
            return null;
        };
        _this.renderDownloadIcon = function (file, locale) {
            var customDownloadIcon = _this.props.downloadIcon;

            return _this.handleActionIconRender((typeof customDownloadIcon === 'function' ? customDownloadIcon(file) : customDownloadIcon) || React.createElement(_icon2['default'], { type: 'download', title: locale.downloadFile, onClick: function onClick() {
                    return _this.handleDownload(file);
                } }), function () {
                return _this.handleDownload(file);
            });
        };
        _this.renderRemoveIcon = function (file, locale) {
            var customRemoveIcon = _this.props.removeIcon;

            return _this.handleActionIconRender((typeof customRemoveIcon === 'function' ? customRemoveIcon(file) : customRemoveIcon) || React.createElement(_icon2['default'], { type: 'delete', title: locale.removeFile, onClick: function onClick() {
                    return _this.handleClose(file);
                } }), function () {
                return _this.handleClose(file);
            });
        };
        _this.renderUploadList = function (_ref) {
            var _classNames4;

            var getPrefixCls = _ref.getPrefixCls;
            var _this$props = _this.props,
                customizePrefixCls = _this$props.prefixCls,
                _this$props$items = _this$props.items,
                items = _this$props$items === undefined ? [] : _this$props$items,
                listType = _this$props.listType,
                showPreviewIcon = _this$props.showPreviewIcon,
                showRemoveIcon = _this$props.showRemoveIcon,
                showDownloadIcon = _this$props.showDownloadIcon,
                locale = _this$props.locale,
                progressAttr = _this$props.progressAttr;

            var prefixCls = getPrefixCls('upload', customizePrefixCls);
            var list = items.map(function (file) {
                var _classNames, _classNames2;

                var progress = void 0;
                var icon = React.createElement(_icon2['default'], { type: file.status === 'uploading' ? 'loading' : 'paper-clip' });
                if (listType === 'picture' || listType === 'picture-card') {
                    if (listType === 'picture-card' && file.status === 'uploading') {
                        icon = React.createElement(
                            'div',
                            { className: prefixCls + '-list-item-uploading-text' },
                            locale.uploading
                        );
                    } else if (!file.thumbUrl && !file.url) {
                        icon = React.createElement(_icon2['default'], { className: prefixCls + '-list-item-thumbnail', type: 'picture', theme: 'twoTone' });
                    } else {
                        var thumbnail = (0, _utils.isImageUrl)(file) ? React.createElement('img', { src: file.thumbUrl || file.url, alt: file.name, className: prefixCls + '-list-item-image' }) : React.createElement(_icon2['default'], { type: 'file', className: prefixCls + '-list-item-icon', theme: 'twoTone' });
                        icon = React.createElement(
                            'a',
                            { className: prefixCls + '-list-item-thumbnail', onClick: function onClick(e) {
                                    return _this.handlePreview(file, e);
                                }, href: file.url || file.thumbUrl, target: '_blank', rel: 'noopener noreferrer' },
                            thumbnail
                        );
                    }
                }
                if (file.status === 'uploading') {
                    // show loading icon if upload progress listener is disabled
                    var loadingProgress = 'percent' in file ? React.createElement(_progress2['default'], (0, _extends3['default'])({ type: 'line' }, progressAttr, { percent: file.percent })) : null;
                    progress = React.createElement(
                        'div',
                        { className: prefixCls + '-list-item-progress', key: 'progress' },
                        loadingProgress
                    );
                }
                var infoUploadingClass = (0, _classnames2['default'])((_classNames = {}, (0, _defineProperty3['default'])(_classNames, prefixCls + '-list-item', true), (0, _defineProperty3['default'])(_classNames, prefixCls + '-list-item-' + file.status, true), (0, _defineProperty3['default'])(_classNames, prefixCls + '-list-item-list-type-' + listType, true), _classNames));
                var linkProps = typeof file.linkProps === 'string' ? JSON.parse(file.linkProps) : file.linkProps;
                var removeIcon = showRemoveIcon ? _this.renderRemoveIcon(file, locale) : null;
                var downloadIcon = showDownloadIcon && file.status === 'done' ? _this.renderDownloadIcon(file, locale) : null;
                var downloadOrDelete = listType !== 'picture-card' && React.createElement(
                    'span',
                    { key: 'download-delete', className: prefixCls + '-list-item-card-actions ' + (listType === 'picture' ? 'picture' : '') },
                    downloadIcon && React.createElement(
                        'a',
                        { title: locale.downloadFile },
                        downloadIcon
                    ),
                    removeIcon && React.createElement(
                        'a',
                        { title: locale.removeFile },
                        removeIcon
                    )
                );
                var listItemNameClass = (0, _classnames2['default'])((_classNames2 = {}, (0, _defineProperty3['default'])(_classNames2, prefixCls + '-list-item-name', true), (0, _defineProperty3['default'])(_classNames2, prefixCls + '-list-item-name-icon-count-' + [downloadIcon, removeIcon].filter(function (x) {
                    return x;
                }).length, true), _classNames2));
                var preview = file.url ? [React.createElement(
                    'a',
                    (0, _extends3['default'])({ key: 'view', target: '_blank', rel: 'noopener noreferrer', className: listItemNameClass, title: file.name }, linkProps, { href: file.url, onClick: function onClick(e) {
                            return _this.handlePreview(file, e);
                        } }),
                    file.name
                ), downloadOrDelete] : [React.createElement(
                    'span',
                    { key: 'view', className: listItemNameClass, onClick: function onClick(e) {
                            return _this.handlePreview(file, e);
                        }, title: file.name },
                    file.name
                ), downloadOrDelete];
                var style = {
                    pointerEvents: 'none',
                    opacity: 0.5
                };
                var previewIcon = showPreviewIcon ? React.createElement(
                    'a',
                    { href: file.url || file.thumbUrl, target: '_blank', rel: 'noopener noreferrer', style: file.url || file.thumbUrl ? undefined : style, onClick: function onClick(e) {
                            return _this.handlePreview(file, e);
                        }, title: locale.previewFile },
                    React.createElement(_icon2['default'], { type: 'eye-o' })
                ) : null;
                var actions = listType === 'picture-card' && file.status !== 'uploading' && React.createElement(
                    'span',
                    { className: prefixCls + '-list-item-actions' },
                    previewIcon,
                    file.status === 'done' && downloadIcon,
                    removeIcon
                );
                var message = void 0;
                if (file.response && typeof file.response === 'string') {
                    message = file.response;
                } else {
                    message = file.error && file.error.statusText || locale.uploadError;
                }
                var iconAndPreview = React.createElement(
                    'span',
                    null,
                    icon,
                    preview
                );
                var dom = React.createElement(
                    'div',
                    { className: infoUploadingClass },
                    React.createElement(
                        'div',
                        { className: prefixCls + '-list-item-info' },
                        iconAndPreview
                    ),
                    actions,
                    React.createElement(
                        _rcAnimate2['default'],
                        { transitionName: 'fade', component: '' },
                        progress
                    )
                );
                var listContainerNameClass = (0, _classnames2['default'])((0, _defineProperty3['default'])({}, prefixCls + '-list-picture-card-container', listType === 'picture-card'));
                return React.createElement(
                    'div',
                    { key: file.uid, className: listContainerNameClass },
                    file.status === 'error' ? React.createElement(
                        _tooltip2['default'],
                        { title: message },
                        dom
                    ) : React.createElement(
                        'span',
                        null,
                        dom
                    )
                );
            });
            var listClassNames = (0, _classnames2['default'])((_classNames4 = {}, (0, _defineProperty3['default'])(_classNames4, prefixCls + '-list', true), (0, _defineProperty3['default'])(_classNames4, prefixCls + '-list-' + listType, true), _classNames4));
            var animationDirection = listType === 'picture-card' ? 'animate-inline' : 'animate';
            return React.createElement(
                _rcAnimate2['default'],
                { transitionName: prefixCls + '-' + animationDirection, component: 'div', className: listClassNames },
                list
            );
        };
        return _this;
    }

    (0, _createClass3['default'])(UploadList, [{
        key: 'componentDidUpdate',
        value: function componentDidUpdate() {
            var _this2 = this;

            var _props = this.props,
                listType = _props.listType,
                items = _props.items,
                previewFile = _props.previewFile;

            if (listType !== 'picture' && listType !== 'picture-card') {
                return;
            }
            (items || []).forEach(function (file) {
                if (typeof document === 'undefined' || typeof window === 'undefined' || !window.FileReader || !window.File || !(file.originFileObj instanceof File || file.originFileObj instanceof Blob) || file.thumbUrl !== undefined) {
                    return;
                }
                file.thumbUrl = '';
                if (previewFile) {
                    previewFile(file.originFileObj).then(function (previewDataUrl) {
                        // Need append '' to avoid dead loop
                        file.thumbUrl = previewDataUrl || '';
                        _this2.forceUpdate();
                    });
                }
            });
        }
    }, {
        key: 'render',
        value: function render() {
            return React.createElement(
                _configProvider.ConfigConsumer,
                null,
                this.renderUploadList
            );
        }
    }]);
    return UploadList;
}(React.Component);

exports['default'] = UploadList;

UploadList.defaultProps = {
    listType: 'text',
    progressAttr: {
        strokeWidth: 2,
        showInfo: false
    },
    showRemoveIcon: true,
    showDownloadIcon: true,
    showPreviewIcon: true,
    previewFile: _utils.previewImage
};
module.exports = exports['default'];