index.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _vue = require("vue");
  8. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  9. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  10. var _LoadingOutlined = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/LoadingOutlined"));
  11. var _PaperClipOutlined = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/PaperClipOutlined"));
  12. var _PictureTwoTone = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/PictureTwoTone"));
  13. var _FileTwoTone = _interopRequireDefault(require("@ant-design/icons-vue/lib/icons/FileTwoTone"));
  14. var _interface = require("../interface");
  15. var _utils = require("../utils");
  16. var _button = _interopRequireDefault(require("../../button"));
  17. var _ListItem = _interopRequireDefault(require("./ListItem"));
  18. var _propsUtil = require("../../_util/props-util");
  19. var _useConfigInject = _interopRequireDefault(require("../../config-provider/hooks/useConfigInject"));
  20. var _transition = require("../../_util/transition");
  21. var _collapseMotion = _interopRequireDefault(require("../../_util/collapseMotion"));
  22. const HackSlot = (_, _ref) => {
  23. let {
  24. slots
  25. } = _ref;
  26. var _a;
  27. return (0, _propsUtil.filterEmpty)((_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots))[0];
  28. };
  29. var _default = exports.default = (0, _vue.defineComponent)({
  30. compatConfig: {
  31. MODE: 3
  32. },
  33. name: 'AUploadList',
  34. props: (0, _propsUtil.initDefaultProps)((0, _interface.uploadListProps)(), {
  35. listType: 'text',
  36. progress: {
  37. strokeWidth: 2,
  38. showInfo: false
  39. },
  40. showRemoveIcon: true,
  41. showDownloadIcon: false,
  42. showPreviewIcon: true,
  43. previewFile: _utils.previewImage,
  44. isImageUrl: _utils.isImageUrl,
  45. items: [],
  46. appendActionVisible: true
  47. }),
  48. setup(props, _ref2) {
  49. let {
  50. slots,
  51. expose
  52. } = _ref2;
  53. const motionAppear = (0, _vue.shallowRef)(false);
  54. (0, _vue.onMounted)(() => {
  55. motionAppear.value == true;
  56. });
  57. const mergedItems = (0, _vue.shallowRef)([]);
  58. (0, _vue.watch)(() => props.items, function () {
  59. let val = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  60. mergedItems.value = val.slice();
  61. }, {
  62. immediate: true,
  63. deep: true
  64. });
  65. (0, _vue.watchEffect)(() => {
  66. if (props.listType !== 'picture' && props.listType !== 'picture-card') {
  67. return;
  68. }
  69. let hasUpdate = false;
  70. (props.items || []).forEach((file, index) => {
  71. if (typeof document === 'undefined' || typeof window === 'undefined' || !window.FileReader || !window.File || !(file.originFileObj instanceof File || file.originFileObj instanceof Blob) || file.thumbUrl !== undefined) {
  72. return;
  73. }
  74. file.thumbUrl = '';
  75. if (props.previewFile) {
  76. props.previewFile(file.originFileObj).then(previewDataUrl => {
  77. // Need append '' to avoid dead loop
  78. const thumbUrl = previewDataUrl || '';
  79. if (thumbUrl !== file.thumbUrl) {
  80. mergedItems.value[index].thumbUrl = thumbUrl;
  81. hasUpdate = true;
  82. }
  83. });
  84. }
  85. });
  86. if (hasUpdate) {
  87. (0, _vue.triggerRef)(mergedItems);
  88. }
  89. });
  90. // ============================= Events =============================
  91. const onInternalPreview = (file, e) => {
  92. if (!props.onPreview) {
  93. return;
  94. }
  95. e === null || e === void 0 ? void 0 : e.preventDefault();
  96. return props.onPreview(file);
  97. };
  98. const onInternalDownload = file => {
  99. if (typeof props.onDownload === 'function') {
  100. props.onDownload(file);
  101. } else if (file.url) {
  102. window.open(file.url);
  103. }
  104. };
  105. const onInternalClose = file => {
  106. var _a;
  107. (_a = props.onRemove) === null || _a === void 0 ? void 0 : _a.call(props, file);
  108. };
  109. const internalIconRender = _ref3 => {
  110. let {
  111. file
  112. } = _ref3;
  113. const iconRender = props.iconRender || slots.iconRender;
  114. if (iconRender) {
  115. return iconRender({
  116. file,
  117. listType: props.listType
  118. });
  119. }
  120. const isLoading = file.status === 'uploading';
  121. const fileIcon = props.isImageUrl && props.isImageUrl(file) ? (0, _vue.createVNode)(_PictureTwoTone.default, null, null) : (0, _vue.createVNode)(_FileTwoTone.default, null, null);
  122. let icon = isLoading ? (0, _vue.createVNode)(_LoadingOutlined.default, null, null) : (0, _vue.createVNode)(_PaperClipOutlined.default, null, null);
  123. if (props.listType === 'picture') {
  124. icon = isLoading ? (0, _vue.createVNode)(_LoadingOutlined.default, null, null) : fileIcon;
  125. } else if (props.listType === 'picture-card') {
  126. icon = isLoading ? props.locale.uploading : fileIcon;
  127. }
  128. return icon;
  129. };
  130. const actionIconRender = opt => {
  131. const {
  132. customIcon,
  133. callback,
  134. prefixCls,
  135. title
  136. } = opt;
  137. const btnProps = {
  138. type: 'text',
  139. size: 'small',
  140. title,
  141. onClick: () => {
  142. callback();
  143. },
  144. class: `${prefixCls}-list-item-action`
  145. };
  146. if ((0, _propsUtil.isValidElement)(customIcon)) {
  147. return (0, _vue.createVNode)(_button.default, btnProps, {
  148. icon: () => customIcon
  149. });
  150. }
  151. return (0, _vue.createVNode)(_button.default, btnProps, {
  152. default: () => [(0, _vue.createVNode)("span", null, [customIcon])]
  153. });
  154. };
  155. expose({
  156. handlePreview: onInternalPreview,
  157. handleDownload: onInternalDownload
  158. });
  159. const {
  160. prefixCls,
  161. rootPrefixCls
  162. } = (0, _useConfigInject.default)('upload', props);
  163. const listClassNames = (0, _vue.computed)(() => ({
  164. [`${prefixCls.value}-list`]: true,
  165. [`${prefixCls.value}-list-${props.listType}`]: true
  166. }));
  167. const transitionGroupProps = (0, _vue.computed)(() => {
  168. const motion = (0, _extends2.default)({}, (0, _collapseMotion.default)(`${rootPrefixCls.value}-motion-collapse`));
  169. delete motion.onAfterAppear;
  170. delete motion.onAfterEnter;
  171. delete motion.onAfterLeave;
  172. const motionConfig = (0, _extends2.default)((0, _extends2.default)({}, (0, _transition.getTransitionGroupProps)(`${prefixCls.value}-${props.listType === 'picture-card' ? 'animate-inline' : 'animate'}`)), {
  173. class: listClassNames.value,
  174. appear: motionAppear.value
  175. });
  176. return props.listType !== 'picture-card' ? (0, _extends2.default)((0, _extends2.default)({}, motion), motionConfig) : motionConfig;
  177. });
  178. return () => {
  179. const {
  180. listType,
  181. locale,
  182. isImageUrl: isImgUrl,
  183. showPreviewIcon,
  184. showRemoveIcon,
  185. showDownloadIcon,
  186. removeIcon,
  187. previewIcon,
  188. downloadIcon,
  189. progress,
  190. appendAction,
  191. itemRender,
  192. appendActionVisible
  193. } = props;
  194. const appendActionDom = appendAction === null || appendAction === void 0 ? void 0 : appendAction();
  195. const items = mergedItems.value;
  196. return (0, _vue.createVNode)(_transition.TransitionGroup, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, transitionGroupProps.value), {}, {
  197. "tag": "div"
  198. }), {
  199. default: () => [items.map(file => {
  200. const {
  201. uid: key
  202. } = file;
  203. return (0, _vue.createVNode)(_ListItem.default, {
  204. "key": key,
  205. "locale": locale,
  206. "prefixCls": prefixCls.value,
  207. "file": file,
  208. "items": items,
  209. "progress": progress,
  210. "listType": listType,
  211. "isImgUrl": isImgUrl,
  212. "showPreviewIcon": showPreviewIcon,
  213. "showRemoveIcon": showRemoveIcon,
  214. "showDownloadIcon": showDownloadIcon,
  215. "onPreview": onInternalPreview,
  216. "onDownload": onInternalDownload,
  217. "onClose": onInternalClose,
  218. "removeIcon": removeIcon,
  219. "previewIcon": previewIcon,
  220. "downloadIcon": downloadIcon,
  221. "itemRender": itemRender
  222. }, (0, _extends2.default)((0, _extends2.default)({}, slots), {
  223. iconRender: internalIconRender,
  224. actionIconRender
  225. }));
  226. }), appendAction ? (0, _vue.withDirectives)((0, _vue.createVNode)(HackSlot, {
  227. "key": "__ant_upload_appendAction"
  228. }, {
  229. default: () => appendActionDom
  230. }), [[_vue.vShow, !!appendActionVisible]]) : null]
  231. });
  232. };
  233. }
  234. });