ListItem.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  3. import { withDirectives as _withDirectives, vShow as _vShow, createVNode as _createVNode } from "vue";
  4. import { computed, defineComponent, onBeforeUnmount, onMounted, ref } from 'vue';
  5. import EyeOutlined from "@ant-design/icons-vue/es/icons/EyeOutlined";
  6. import DeleteOutlined from "@ant-design/icons-vue/es/icons/DeleteOutlined";
  7. import DownloadOutlined from "@ant-design/icons-vue/es/icons/DownloadOutlined";
  8. import Tooltip from '../../tooltip';
  9. import Progress from '../../progress';
  10. import useConfigInject from '../../_util/hooks/useConfigInject';
  11. import Transition, { getTransitionProps } from '../../_util/transition';
  12. export var listItemProps = function listItemProps() {
  13. return {
  14. prefixCls: String,
  15. locale: {
  16. type: Object,
  17. default: undefined
  18. },
  19. file: Object,
  20. items: Array,
  21. listType: String,
  22. isImgUrl: Function,
  23. showRemoveIcon: {
  24. type: Boolean,
  25. default: undefined
  26. },
  27. showDownloadIcon: {
  28. type: Boolean,
  29. default: undefined
  30. },
  31. showPreviewIcon: {
  32. type: Boolean,
  33. default: undefined
  34. },
  35. removeIcon: Function,
  36. downloadIcon: Function,
  37. previewIcon: Function,
  38. iconRender: Function,
  39. actionIconRender: Function,
  40. itemRender: Function,
  41. onPreview: Function,
  42. onClose: Function,
  43. onDownload: Function,
  44. progress: {
  45. type: Object,
  46. default: undefined
  47. }
  48. };
  49. };
  50. export default defineComponent({
  51. compatConfig: {
  52. MODE: 3
  53. },
  54. name: 'ListItem',
  55. inheritAttrs: false,
  56. props: listItemProps(),
  57. setup: function setup(props, _ref) {
  58. var slots = _ref.slots,
  59. attrs = _ref.attrs;
  60. var showProgress = ref(false);
  61. var progressRafRef = ref();
  62. onMounted(function () {
  63. progressRafRef.value = setTimeout(function () {
  64. showProgress.value = true;
  65. }, 300);
  66. });
  67. onBeforeUnmount(function () {
  68. clearTimeout(progressRafRef.value);
  69. });
  70. var _useConfigInject = useConfigInject('upload', props),
  71. rootPrefixCls = _useConfigInject.rootPrefixCls;
  72. var transitionProps = computed(function () {
  73. return getTransitionProps("".concat(rootPrefixCls.value, "-fade"));
  74. });
  75. return function () {
  76. var _infoUploadingClass, _listContainerNameCla;
  77. var prefixCls = props.prefixCls,
  78. locale = props.locale,
  79. listType = props.listType,
  80. file = props.file,
  81. items = props.items,
  82. progressProps = props.progress,
  83. _props$iconRender = props.iconRender,
  84. iconRender = _props$iconRender === void 0 ? slots.iconRender : _props$iconRender,
  85. _props$actionIconRend = props.actionIconRender,
  86. actionIconRender = _props$actionIconRend === void 0 ? slots.actionIconRender : _props$actionIconRend,
  87. _props$itemRender = props.itemRender,
  88. itemRender = _props$itemRender === void 0 ? slots.itemRender : _props$itemRender,
  89. isImgUrl = props.isImgUrl,
  90. showPreviewIcon = props.showPreviewIcon,
  91. showRemoveIcon = props.showRemoveIcon,
  92. showDownloadIcon = props.showDownloadIcon,
  93. _props$previewIcon = props.previewIcon,
  94. customPreviewIcon = _props$previewIcon === void 0 ? slots.previewIcon : _props$previewIcon,
  95. _props$removeIcon = props.removeIcon,
  96. customRemoveIcon = _props$removeIcon === void 0 ? slots.removeIcon : _props$removeIcon,
  97. _props$downloadIcon = props.downloadIcon,
  98. customDownloadIcon = _props$downloadIcon === void 0 ? slots.downloadIcon : _props$downloadIcon,
  99. onPreview = props.onPreview,
  100. onDownload = props.onDownload,
  101. onClose = props.onClose;
  102. var className = attrs.class,
  103. style = attrs.style;
  104. // This is used for legacy span make scrollHeight the wrong value.
  105. // We will force these to be `display: block` with non `picture-card`
  106. var spanClassName = "".concat(prefixCls, "-span");
  107. var iconNode = iconRender({
  108. file: file
  109. });
  110. var icon = _createVNode("div", {
  111. "class": "".concat(prefixCls, "-text-icon")
  112. }, [iconNode]);
  113. if (listType === 'picture' || listType === 'picture-card') {
  114. if (file.status === 'uploading' || !file.thumbUrl && !file.url) {
  115. var _uploadingClassName;
  116. var uploadingClassName = (_uploadingClassName = {}, _defineProperty(_uploadingClassName, "".concat(prefixCls, "-list-item-thumbnail"), true), _defineProperty(_uploadingClassName, "".concat(prefixCls, "-list-item-file"), file.status !== 'uploading'), _uploadingClassName);
  117. icon = _createVNode("div", {
  118. "class": uploadingClassName
  119. }, [iconNode]);
  120. } else {
  121. var _aClassName;
  122. var thumbnail = isImgUrl !== null && isImgUrl !== void 0 && isImgUrl(file) ? _createVNode("img", {
  123. "src": file.thumbUrl || file.url,
  124. "alt": file.name,
  125. "class": "".concat(prefixCls, "-list-item-image")
  126. }, null) : iconNode;
  127. var aClassName = (_aClassName = {}, _defineProperty(_aClassName, "".concat(prefixCls, "-list-item-thumbnail"), true), _defineProperty(_aClassName, "".concat(prefixCls, "-list-item-file"), isImgUrl && !isImgUrl(file)), _aClassName);
  128. icon = _createVNode("a", {
  129. "class": aClassName,
  130. "onClick": function onClick(e) {
  131. return onPreview(file, e);
  132. },
  133. "href": file.url || file.thumbUrl,
  134. "target": "_blank",
  135. "rel": "noopener noreferrer"
  136. }, [thumbnail]);
  137. }
  138. }
  139. var infoUploadingClass = (_infoUploadingClass = {}, _defineProperty(_infoUploadingClass, "".concat(prefixCls, "-list-item"), true), _defineProperty(_infoUploadingClass, "".concat(prefixCls, "-list-item-").concat(file.status), true), _defineProperty(_infoUploadingClass, "".concat(prefixCls, "-list-item-list-type-").concat(listType), true), _infoUploadingClass);
  140. var linkProps = typeof file.linkProps === 'string' ? JSON.parse(file.linkProps) : file.linkProps;
  141. var removeIcon = showRemoveIcon ? actionIconRender({
  142. customIcon: customRemoveIcon ? customRemoveIcon({
  143. file: file
  144. }) : _createVNode(DeleteOutlined, null, null),
  145. callback: function callback() {
  146. return onClose(file);
  147. },
  148. prefixCls: prefixCls,
  149. title: locale.removeFile
  150. }) : null;
  151. var downloadIcon = showDownloadIcon && file.status === 'done' ? actionIconRender({
  152. customIcon: customDownloadIcon ? customDownloadIcon({
  153. file: file
  154. }) : _createVNode(DownloadOutlined, null, null),
  155. callback: function callback() {
  156. return onDownload(file);
  157. },
  158. prefixCls: prefixCls,
  159. title: locale.downloadFile
  160. }) : null;
  161. var downloadOrDelete = listType !== 'picture-card' && _createVNode("span", {
  162. "key": "download-delete",
  163. "class": ["".concat(prefixCls, "-list-item-card-actions"), {
  164. picture: listType === 'picture'
  165. }]
  166. }, [downloadIcon, removeIcon]);
  167. var listItemNameClass = "".concat(prefixCls, "-list-item-name");
  168. var preview = file.url ? [_createVNode("a", _objectSpread(_objectSpread({
  169. "key": "view",
  170. "target": "_blank",
  171. "rel": "noopener noreferrer",
  172. "class": listItemNameClass,
  173. "title": file.name
  174. }, linkProps), {}, {
  175. "href": file.url,
  176. "onClick": function onClick(e) {
  177. return onPreview(file, e);
  178. }
  179. }), [file.name]), downloadOrDelete] : [_createVNode("span", {
  180. "key": "view",
  181. "class": listItemNameClass,
  182. "onClick": function onClick(e) {
  183. return onPreview(file, e);
  184. },
  185. "title": file.name
  186. }, [file.name]), downloadOrDelete];
  187. var previewStyle = {
  188. pointerEvents: 'none',
  189. opacity: 0.5
  190. };
  191. var previewIcon = showPreviewIcon ? _createVNode("a", {
  192. "href": file.url || file.thumbUrl,
  193. "target": "_blank",
  194. "rel": "noopener noreferrer",
  195. "style": file.url || file.thumbUrl ? undefined : previewStyle,
  196. "onClick": function onClick(e) {
  197. return onPreview(file, e);
  198. },
  199. "title": locale.previewFile
  200. }, [customPreviewIcon ? customPreviewIcon({
  201. file: file
  202. }) : _createVNode(EyeOutlined, null, null)]) : null;
  203. var actions = listType === 'picture-card' && file.status !== 'uploading' && _createVNode("span", {
  204. "class": "".concat(prefixCls, "-list-item-actions")
  205. }, [previewIcon, file.status === 'done' && downloadIcon, removeIcon]);
  206. var message;
  207. if (file.response && typeof file.response === 'string') {
  208. message = file.response;
  209. } else {
  210. var _file$error, _file$error2;
  211. message = ((_file$error = file.error) === null || _file$error === void 0 ? void 0 : _file$error.statusText) || ((_file$error2 = file.error) === null || _file$error2 === void 0 ? void 0 : _file$error2.message) || locale.uploadError;
  212. }
  213. var iconAndPreview = _createVNode("span", {
  214. "class": spanClassName
  215. }, [icon, preview]);
  216. var dom = _createVNode("div", {
  217. "class": infoUploadingClass
  218. }, [_createVNode("div", {
  219. "class": "".concat(prefixCls, "-list-item-info")
  220. }, [iconAndPreview]), actions, showProgress.value && _createVNode(Transition, transitionProps.value, {
  221. default: function _default() {
  222. return [_withDirectives(_createVNode("div", {
  223. "class": "".concat(prefixCls, "-list-item-progress")
  224. }, ['percent' in file ? _createVNode(Progress, _objectSpread(_objectSpread({}, progressProps), {}, {
  225. "type": "line",
  226. "percent": file.percent
  227. }), null) : null]), [[_vShow, file.status === 'uploading']])];
  228. }
  229. })]);
  230. var listContainerNameClass = (_listContainerNameCla = {}, _defineProperty(_listContainerNameCla, "".concat(prefixCls, "-list-").concat(listType, "-container"), true), _defineProperty(_listContainerNameCla, "".concat(className), !!className), _listContainerNameCla);
  231. var item = file.status === 'error' ? _createVNode(Tooltip, {
  232. "title": message,
  233. "getPopupContainer": function getPopupContainer(node) {
  234. return node.parentNode;
  235. }
  236. }, {
  237. default: function _default() {
  238. return [dom];
  239. }
  240. }) : dom;
  241. return _createVNode("div", {
  242. "class": listContainerNameClass,
  243. "style": style,
  244. "ref": ref
  245. }, [itemRender ? itemRender({
  246. originNode: item,
  247. file: file,
  248. fileList: items,
  249. actions: {
  250. download: onDownload.bind(null, file),
  251. preview: onPreview.bind(null, file),
  252. remove: onClose.bind(null, file)
  253. }
  254. }) : item]);
  255. };
  256. }
  257. });