import _extends from"babel-runtime/helpers/extends";import _defineProperty from"babel-runtime/helpers/defineProperty";import _classCallCheck from"babel-runtime/helpers/classCallCheck";import _createClass from"babel-runtime/helpers/createClass";import _possibleConstructorReturn from"babel-runtime/helpers/possibleConstructorReturn";import _inherits from"babel-runtime/helpers/inherits";import*as React from"react";import Animate from"rc-animate";import Icon from"../icon";import Tooltip from"../tooltip";import Progress from"../progress";import classNames from"classnames";var previewFile=function(e,t){var r=new FileReader;r.onloadend=function(){return t(r.result)},r.readAsDataURL(e)},UploadList=function(e){function t(){_classCallCheck(this,t);var e=_possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments));return e.handleClose=function(t){var r=e.props.onRemove;r&&r(t)},e.handlePreview=function(t,r){var n=e.props.onPreview;if(n)return r.preventDefault(),n(t)},e}return _inherits(t,e),_createClass(t,[{key:"componentDidUpdate",value:function(){var e=this;"picture"!==this.props.listType&&"picture-card"!==this.props.listType||(this.props.items||[]).forEach(function(t){"undefined"!=typeof document&&"undefined"!=typeof window&&window.FileReader&&window.File&&t.originFileObj instanceof File&&void 0===t.thumbUrl&&(t.thumbUrl="",previewFile(t.originFileObj,function(r){t.thumbUrl=r,e.forceUpdate()}))})}},{key:"render",value:function(){var e,t=this,r=this.props,n=r.prefixCls,a=r.items,i=void 0===a?[]:a,l=r.listType,o=r.showPreviewIcon,s=r.showRemoveIcon,c=r.locale,m=i.map(function(e){var r,a=void 0,i=React.createElement(Icon,{type:"paper-clip"});if("picture"!==l&&"picture-card"!==l||(i="picture-card"===l&&"uploading"===e.status?React.createElement("div",{className:n+"-list-item-uploading-text"},c.uploading):e.thumbUrl||e.url?null:React.createElement(Icon,{className:n+"-list-item-thumbnail",type:"picture"})),"uploading"===e.status&&"text"!==l){var m="percent"in e?React.createElement(Progress,{type:"line",percent:e.percent,size:"small",showInfo:!1}):null;a=React.createElement("div",{className:n+"-list-item-progress",key:"progress"},m)}var p=classNames((_defineProperty(r={},n+"-list-item",!0),_defineProperty(r,n+"-list-item-"+e.status,!0),r)),u=void 0,d=parseFloat((e.size/1024).toFixed(1)),f=d<1024;if(!isNaN(d)){var h=f?(e.percent/100*d).toFixed(1):(e.percent/100*d/1024).toFixed(1);u="uploading"===e.status?h+(f?"KB":"M")+" / "+(f?d+"KB":(d/1024).toFixed(1)+"M")+"\n          (已上传"+e.percent.toFixed(2)+"%)":"error"===e.status?"上传失败":f?d+"KB":(d/1024).toFixed(1)+"M"}var v="text"===l?e.url?React.createElement("a",_extends({},e.linkProps,{href:e.url,target:"_blank",rel:"noopener noreferrer",className:n+"-list-item-name",onClick:function(r){return t.handlePreview(e,r)},title:e.name}),i,React.createElement("span",{className:n+"-list-item-name-title"},e.name),React.createElement("span",{className:n+"-list-item-name-progress"},u)):React.createElement("span",{className:n+"-list-item-name",onClick:function(r){return t.handlePreview(e,r)},title:e.name},i,React.createElement("span",{className:n+"-list-item-name-title"},e.name),React.createElement("span",{className:n+"-list-item-name-progress"},u)):React.createElement("a",{className:n+"-list-item-thumbnail",onClick:function(r){return t.handlePreview(e,r)},href:e.url||e.thumbUrl,target:"_blank",rel:"noopener noreferrer"},React.createElement("img",{src:e.thumbUrl||e.url,alt:e.name})),R=e.url||e.thumbUrl?void 0:{pointerEvents:"none",opacity:.5},b=o?React.createElement("a",{href:e.url||e.thumbUrl,target:"_blank",rel:"noopener noreferrer",style:R,onClick:function(r){return t.handlePreview(e,r)},title:c.previewFile},React.createElement(Icon,{type:"eye-o"})):null,y=s?React.createElement(Icon,{type:"delete",title:c.removeFile,onClick:function(){return t.handleClose(e)}}):null,E=s?React.createElement(Icon,{type:"close",title:c.removeFile,onClick:function(){return t.handleClose(e)}}):null,C="picture-card"===l&&"uploading"!==e.status?React.createElement("span",{className:n+"-list-item-actions"},b,y):E,w=void 0;w=e.response&&"string"==typeof e.response?e.response:e.error&&e.error.statusText||c.uploadError;var _="error"===e.status?React.createElement(Tooltip,{title:w},v):React.createElement("span",null,v);return React.createElement("div",{className:p,key:e.uid},React.createElement("div",{className:n+"-list-item-info"},_),C,React.createElement(Animate,{transitionName:"fade",component:""},a))}),p=classNames((_defineProperty(e={},n+"-list",!0),_defineProperty(e,n+"-list-"+l,!0),e)),u="picture-card"===l?"animate-inline":"animate";return React.createElement(Animate,{transitionName:n+"-"+u,component:"div",className:p},m)}}]),t}(React.Component);export default UploadList;UploadList.defaultProps={listType:"text",prefixCls:"dt-upload",showRemoveIcon:!0,showPreviewIcon:!0};