Upload.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import _mergeJSXProps from 'babel-helper-vue-jsx-merge-props';
  2. import _defineProperty from 'babel-runtime/helpers/defineProperty';
  3. import _extends from 'babel-runtime/helpers/extends';
  4. import classNames from 'classnames';
  5. import uniqBy from 'lodash/uniqBy';
  6. import findIndex from 'lodash/findIndex';
  7. import pick from 'lodash/pick';
  8. import VcUpload from '../vc-upload';
  9. import BaseMixin from '../_util/BaseMixin';
  10. import { getOptionProps, initDefaultProps, hasProp, getListeners } from '../_util/props-util';
  11. import LocaleReceiver from '../locale-provider/LocaleReceiver';
  12. import defaultLocale from '../locale-provider/default';
  13. import { ConfigConsumerProps } from '../config-provider/configConsumerProps';
  14. import Dragger from './Dragger';
  15. import UploadList from './UploadList';
  16. import { UploadProps } from './interface';
  17. import { T, fileToObject, genPercentAdd, getFileItem, removeFileItem } from './utils';
  18. export { UploadProps };
  19. export default {
  20. name: 'AUpload',
  21. mixins: [BaseMixin],
  22. inheritAttrs: false,
  23. Dragger: Dragger,
  24. props: initDefaultProps(UploadProps, {
  25. type: 'select',
  26. multiple: false,
  27. action: '',
  28. data: {},
  29. accept: '',
  30. beforeUpload: T,
  31. showUploadList: true,
  32. listType: 'text', // or pictrue
  33. disabled: false,
  34. supportServerRender: true
  35. }),
  36. inject: {
  37. configProvider: { 'default': function _default() {
  38. return ConfigConsumerProps;
  39. } }
  40. },
  41. // recentUploadStatus: boolean | PromiseLike<any>;
  42. data: function data() {
  43. this.progressTimer = null;
  44. return {
  45. sFileList: this.fileList || this.defaultFileList || [],
  46. dragState: 'drop'
  47. };
  48. },
  49. watch: {
  50. fileList: function fileList(val) {
  51. this.sFileList = val || [];
  52. }
  53. },
  54. beforeDestroy: function beforeDestroy() {
  55. this.clearProgressTimer();
  56. },
  57. methods: {
  58. onStart: function onStart(file) {
  59. var targetItem = fileToObject(file);
  60. targetItem.status = 'uploading';
  61. var nextFileList = this.sFileList.concat();
  62. var fileIndex = findIndex(nextFileList, function (_ref) {
  63. var uid = _ref.uid;
  64. return uid === targetItem.uid;
  65. });
  66. if (fileIndex === -1) {
  67. nextFileList.push(targetItem);
  68. } else {
  69. nextFileList[fileIndex] = targetItem;
  70. }
  71. this.onChange({
  72. file: targetItem,
  73. fileList: nextFileList
  74. });
  75. // fix ie progress
  76. if (!window.File || process.env.TEST_IE) {
  77. this.autoUpdateProgress(0, targetItem);
  78. }
  79. },
  80. onSuccess: function onSuccess(response, file, xhr) {
  81. this.clearProgressTimer();
  82. try {
  83. if (typeof response === 'string') {
  84. response = JSON.parse(response);
  85. }
  86. } catch (e) {
  87. /* do nothing */
  88. }
  89. var fileList = this.sFileList;
  90. var targetItem = getFileItem(file, fileList);
  91. // removed
  92. if (!targetItem) {
  93. return;
  94. }
  95. targetItem.status = 'done';
  96. targetItem.response = response;
  97. targetItem.xhr = xhr;
  98. this.onChange({
  99. file: _extends({}, targetItem),
  100. fileList: fileList
  101. });
  102. },
  103. onProgress: function onProgress(e, file) {
  104. var fileList = this.sFileList;
  105. var targetItem = getFileItem(file, fileList);
  106. // removed
  107. if (!targetItem) {
  108. return;
  109. }
  110. targetItem.percent = e.percent;
  111. this.onChange({
  112. event: e,
  113. file: _extends({}, targetItem),
  114. fileList: this.sFileList
  115. });
  116. },
  117. onError: function onError(error, response, file) {
  118. this.clearProgressTimer();
  119. var fileList = this.sFileList;
  120. var targetItem = getFileItem(file, fileList);
  121. // removed
  122. if (!targetItem) {
  123. return;
  124. }
  125. targetItem.error = error;
  126. targetItem.response = response;
  127. targetItem.status = 'error';
  128. this.onChange({
  129. file: _extends({}, targetItem),
  130. fileList: fileList
  131. });
  132. },
  133. onReject: function onReject(fileList) {
  134. this.$emit('reject', fileList);
  135. },
  136. handleRemove: function handleRemove(file) {
  137. var _this = this;
  138. var onRemove = this.remove;
  139. var fileList = this.$data.sFileList;
  140. Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(function (ret) {
  141. // Prevent removing file
  142. if (ret === false) {
  143. return;
  144. }
  145. var removedFileList = removeFileItem(file, fileList);
  146. if (removedFileList) {
  147. file.status = 'removed'; // eslint-disable-line
  148. if (_this.upload) {
  149. _this.upload.abort(file);
  150. }
  151. _this.onChange({
  152. file: file,
  153. fileList: removedFileList
  154. });
  155. }
  156. });
  157. },
  158. handleManualRemove: function handleManualRemove(file) {
  159. if (this.$refs.uploadRef) {
  160. this.$refs.uploadRef.abort(file);
  161. }
  162. this.handleRemove(file);
  163. },
  164. onChange: function onChange(info) {
  165. if (!hasProp(this, 'fileList')) {
  166. this.setState({ sFileList: info.fileList });
  167. }
  168. this.$emit('change', info);
  169. },
  170. onFileDrop: function onFileDrop(e) {
  171. this.setState({
  172. dragState: e.type
  173. });
  174. },
  175. reBeforeUpload: function reBeforeUpload(file, fileList) {
  176. var beforeUpload = this.$props.beforeUpload;
  177. var stateFileList = this.$data.sFileList;
  178. if (!beforeUpload) {
  179. return true;
  180. }
  181. var result = beforeUpload(file, fileList);
  182. if (result === false) {
  183. this.onChange({
  184. file: file,
  185. fileList: uniqBy(stateFileList.concat(fileList.map(fileToObject)), function (item) {
  186. return item.uid;
  187. })
  188. });
  189. return false;
  190. }
  191. if (result && result.then) {
  192. return result;
  193. }
  194. return true;
  195. },
  196. clearProgressTimer: function clearProgressTimer() {
  197. clearInterval(this.progressTimer);
  198. },
  199. autoUpdateProgress: function autoUpdateProgress(_, file) {
  200. var _this2 = this;
  201. var getPercent = genPercentAdd();
  202. var curPercent = 0;
  203. this.clearProgressTimer();
  204. this.progressTimer = setInterval(function () {
  205. curPercent = getPercent(curPercent);
  206. _this2.onProgress({
  207. percent: curPercent * 100
  208. }, file);
  209. }, 200);
  210. },
  211. renderUploadList: function renderUploadList(locale) {
  212. var h = this.$createElement;
  213. var _getOptionProps = getOptionProps(this),
  214. _getOptionProps$showU = _getOptionProps.showUploadList,
  215. showUploadList = _getOptionProps$showU === undefined ? {} : _getOptionProps$showU,
  216. listType = _getOptionProps.listType,
  217. previewFile = _getOptionProps.previewFile,
  218. disabled = _getOptionProps.disabled,
  219. propLocale = _getOptionProps.locale;
  220. var showRemoveIcon = showUploadList.showRemoveIcon,
  221. showPreviewIcon = showUploadList.showPreviewIcon,
  222. showDownloadIcon = showUploadList.showDownloadIcon;
  223. var fileList = this.$data.sFileList;
  224. var uploadListProps = {
  225. props: {
  226. listType: listType,
  227. items: fileList,
  228. previewFile: previewFile,
  229. showRemoveIcon: !disabled && showRemoveIcon,
  230. showPreviewIcon: showPreviewIcon,
  231. showDownloadIcon: showDownloadIcon,
  232. locale: _extends({}, locale, propLocale)
  233. },
  234. on: _extends({
  235. remove: this.handleManualRemove
  236. }, pick(getListeners(this), ['download', 'preview']))
  237. };
  238. return h(UploadList, uploadListProps);
  239. }
  240. },
  241. render: function render() {
  242. var _classNames2;
  243. var h = arguments[0];
  244. var _getOptionProps2 = getOptionProps(this),
  245. customizePrefixCls = _getOptionProps2.prefixCls,
  246. showUploadList = _getOptionProps2.showUploadList,
  247. listType = _getOptionProps2.listType,
  248. type = _getOptionProps2.type,
  249. disabled = _getOptionProps2.disabled;
  250. var _$data = this.$data,
  251. fileList = _$data.sFileList,
  252. dragState = _$data.dragState;
  253. var getPrefixCls = this.configProvider.getPrefixCls;
  254. var prefixCls = getPrefixCls('upload', customizePrefixCls);
  255. var vcUploadProps = {
  256. props: _extends({}, this.$props, {
  257. prefixCls: prefixCls,
  258. beforeUpload: this.reBeforeUpload
  259. }),
  260. on: {
  261. start: this.onStart,
  262. error: this.onError,
  263. progress: this.onProgress,
  264. success: this.onSuccess,
  265. reject: this.onReject
  266. },
  267. ref: 'uploadRef',
  268. attrs: _extends({}, this.$attrs)
  269. };
  270. var children = this.$slots['default'];
  271. // Remove id to avoid open by label when trigger is hidden
  272. // https://github.com/ant-design/ant-design/issues/14298
  273. if (!children || disabled) {
  274. delete vcUploadProps.props.id;
  275. delete vcUploadProps.attrs.id;
  276. }
  277. var uploadList = showUploadList ? h(LocaleReceiver, {
  278. attrs: {
  279. componentName: 'Upload',
  280. defaultLocale: defaultLocale.Upload
  281. },
  282. scopedSlots: { 'default': this.renderUploadList }
  283. }) : null;
  284. if (type === 'drag') {
  285. var _classNames;
  286. var dragCls = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, prefixCls + '-drag', true), _defineProperty(_classNames, prefixCls + '-drag-uploading', fileList.some(function (file) {
  287. return file.status === 'uploading';
  288. })), _defineProperty(_classNames, prefixCls + '-drag-hover', dragState === 'dragover'), _defineProperty(_classNames, prefixCls + '-disabled', disabled), _classNames));
  289. return h('span', [h(
  290. 'div',
  291. {
  292. 'class': dragCls,
  293. on: {
  294. 'drop': this.onFileDrop,
  295. 'dragover': this.onFileDrop,
  296. 'dragleave': this.onFileDrop
  297. }
  298. },
  299. [h(
  300. VcUpload,
  301. _mergeJSXProps([vcUploadProps, { 'class': prefixCls + '-btn' }]),
  302. [h(
  303. 'div',
  304. { 'class': prefixCls + '-drag-container' },
  305. [children]
  306. )]
  307. )]
  308. ), uploadList]);
  309. }
  310. var uploadButtonCls = classNames(prefixCls, (_classNames2 = {}, _defineProperty(_classNames2, prefixCls + '-select', true), _defineProperty(_classNames2, prefixCls + '-select-' + listType, true), _defineProperty(_classNames2, prefixCls + '-disabled', disabled), _classNames2));
  311. var uploadButton = h(
  312. 'div',
  313. { 'class': uploadButtonCls, style: children ? undefined : { display: 'none' } },
  314. [h(
  315. VcUpload,
  316. vcUploadProps,
  317. [children]
  318. )]
  319. );
  320. if (listType === 'picture-card') {
  321. return h(
  322. 'span',
  323. { 'class': prefixCls + '-picture-card-wrapper' },
  324. [uploadList, uploadButton]
  325. );
  326. }
  327. return h('span', [uploadButton, uploadList]);
  328. }
  329. };