Upload.js 12 KB

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