ListItem.js 11 KB

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