123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- 'use strict';
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.UploadProps = undefined;
- var _babelHelperVueJsxMergeProps = require('babel-helper-vue-jsx-merge-props');
- var _babelHelperVueJsxMergeProps2 = _interopRequireDefault(_babelHelperVueJsxMergeProps);
- var _defineProperty2 = require('babel-runtime/helpers/defineProperty');
- var _defineProperty3 = _interopRequireDefault(_defineProperty2);
- var _extends2 = require('babel-runtime/helpers/extends');
- var _extends3 = _interopRequireDefault(_extends2);
- var _classnames = require('classnames');
- var _classnames2 = _interopRequireDefault(_classnames);
- var _uniqBy = require('lodash/uniqBy');
- var _uniqBy2 = _interopRequireDefault(_uniqBy);
- var _findIndex = require('lodash/findIndex');
- var _findIndex2 = _interopRequireDefault(_findIndex);
- var _pick = require('lodash/pick');
- var _pick2 = _interopRequireDefault(_pick);
- var _vcUpload = require('../vc-upload');
- var _vcUpload2 = _interopRequireDefault(_vcUpload);
- var _BaseMixin = require('../_util/BaseMixin');
- var _BaseMixin2 = _interopRequireDefault(_BaseMixin);
- var _propsUtil = require('../_util/props-util');
- var _LocaleReceiver = require('../locale-provider/LocaleReceiver');
- var _LocaleReceiver2 = _interopRequireDefault(_LocaleReceiver);
- var _default2 = require('../locale-provider/default');
- var _default3 = _interopRequireDefault(_default2);
- var _configConsumerProps = require('../config-provider/configConsumerProps');
- var _Dragger = require('./Dragger');
- var _Dragger2 = _interopRequireDefault(_Dragger);
- var _UploadList = require('./UploadList');
- var _UploadList2 = _interopRequireDefault(_UploadList);
- var _interface = require('./interface');
- var _utils = require('./utils');
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
- exports.UploadProps = _interface.UploadProps;
- exports['default'] = {
- name: 'AUpload',
- mixins: [_BaseMixin2['default']],
- inheritAttrs: false,
- Dragger: _Dragger2['default'],
- props: (0, _propsUtil.initDefaultProps)(_interface.UploadProps, {
- type: 'select',
- multiple: false,
- action: '',
- data: {},
- accept: '',
- beforeUpload: _utils.T,
- showUploadList: true,
- listType: 'text', // or pictrue
- disabled: false,
- supportServerRender: true
- }),
- inject: {
- configProvider: { 'default': function _default() {
- return _configConsumerProps.ConfigConsumerProps;
- } }
- },
- // recentUploadStatus: boolean | PromiseLike<any>;
- data: function data() {
- this.progressTimer = null;
- return {
- sFileList: this.fileList || this.defaultFileList || [],
- dragState: 'drop'
- };
- },
- watch: {
- fileList: function fileList(val) {
- this.sFileList = val || [];
- }
- },
- beforeDestroy: function beforeDestroy() {
- this.clearProgressTimer();
- },
- methods: {
- onStart: function onStart(file) {
- var targetItem = (0, _utils.fileToObject)(file);
- targetItem.status = 'uploading';
- var nextFileList = this.sFileList.concat();
- var fileIndex = (0, _findIndex2['default'])(nextFileList, function (_ref) {
- var uid = _ref.uid;
- return uid === targetItem.uid;
- });
- if (fileIndex === -1) {
- nextFileList.push(targetItem);
- } else {
- nextFileList[fileIndex] = targetItem;
- }
- this.onChange({
- file: targetItem,
- fileList: nextFileList
- });
- // fix ie progress
- if (!window.File || process.env.TEST_IE) {
- this.autoUpdateProgress(0, targetItem);
- }
- },
- onSuccess: function onSuccess(response, file, xhr) {
- this.clearProgressTimer();
- try {
- if (typeof response === 'string') {
- response = JSON.parse(response);
- }
- } catch (e) {
- /* do nothing */
- }
- var fileList = this.sFileList;
- var targetItem = (0, _utils.getFileItem)(file, fileList);
- // removed
- if (!targetItem) {
- return;
- }
- targetItem.status = 'done';
- targetItem.response = response;
- targetItem.xhr = xhr;
- this.onChange({
- file: (0, _extends3['default'])({}, targetItem),
- fileList: fileList
- });
- },
- onProgress: function onProgress(e, file) {
- var fileList = this.sFileList;
- var targetItem = (0, _utils.getFileItem)(file, fileList);
- // removed
- if (!targetItem) {
- return;
- }
- targetItem.percent = e.percent;
- this.onChange({
- event: e,
- file: (0, _extends3['default'])({}, targetItem),
- fileList: this.sFileList
- });
- },
- onError: function onError(error, response, file) {
- this.clearProgressTimer();
- var fileList = this.sFileList;
- var targetItem = (0, _utils.getFileItem)(file, fileList);
- // removed
- if (!targetItem) {
- return;
- }
- targetItem.error = error;
- targetItem.response = response;
- targetItem.status = 'error';
- this.onChange({
- file: (0, _extends3['default'])({}, targetItem),
- fileList: fileList
- });
- },
- onReject: function onReject(fileList) {
- this.$emit('reject', fileList);
- },
- handleRemove: function handleRemove(file) {
- var _this = this;
- var onRemove = this.remove;
- var fileList = this.$data.sFileList;
- Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(function (ret) {
- // Prevent removing file
- if (ret === false) {
- return;
- }
- var removedFileList = (0, _utils.removeFileItem)(file, fileList);
- if (removedFileList) {
- file.status = 'removed'; // eslint-disable-line
- if (_this.upload) {
- _this.upload.abort(file);
- }
- _this.onChange({
- file: file,
- fileList: removedFileList
- });
- }
- });
- },
- handleManualRemove: function handleManualRemove(file) {
- if (this.$refs.uploadRef) {
- this.$refs.uploadRef.abort(file);
- }
- this.handleRemove(file);
- },
- onChange: function onChange(info) {
- if (!(0, _propsUtil.hasProp)(this, 'fileList')) {
- this.setState({ sFileList: info.fileList });
- }
- this.$emit('change', info);
- },
- onFileDrop: function onFileDrop(e) {
- this.setState({
- dragState: e.type
- });
- },
- reBeforeUpload: function reBeforeUpload(file, fileList) {
- var beforeUpload = this.$props.beforeUpload;
- var stateFileList = this.$data.sFileList;
- if (!beforeUpload) {
- return true;
- }
- var result = beforeUpload(file, fileList);
- if (result === false) {
- this.onChange({
- file: file,
- fileList: (0, _uniqBy2['default'])(stateFileList.concat(fileList.map(_utils.fileToObject)), function (item) {
- return item.uid;
- })
- });
- return false;
- }
- if (result && result.then) {
- return result;
- }
- return true;
- },
- clearProgressTimer: function clearProgressTimer() {
- clearInterval(this.progressTimer);
- },
- autoUpdateProgress: function autoUpdateProgress(_, file) {
- var _this2 = this;
- var getPercent = (0, _utils.genPercentAdd)();
- var curPercent = 0;
- this.clearProgressTimer();
- this.progressTimer = setInterval(function () {
- curPercent = getPercent(curPercent);
- _this2.onProgress({
- percent: curPercent * 100
- }, file);
- }, 200);
- },
- renderUploadList: function renderUploadList(locale) {
- var h = this.$createElement;
- var _getOptionProps = (0, _propsUtil.getOptionProps)(this),
- _getOptionProps$showU = _getOptionProps.showUploadList,
- showUploadList = _getOptionProps$showU === undefined ? {} : _getOptionProps$showU,
- listType = _getOptionProps.listType,
- previewFile = _getOptionProps.previewFile,
- disabled = _getOptionProps.disabled,
- propLocale = _getOptionProps.locale;
- var showRemoveIcon = showUploadList.showRemoveIcon,
- showPreviewIcon = showUploadList.showPreviewIcon,
- showDownloadIcon = showUploadList.showDownloadIcon;
- var fileList = this.$data.sFileList;
- var uploadListProps = {
- props: {
- listType: listType,
- items: fileList,
- previewFile: previewFile,
- showRemoveIcon: !disabled && showRemoveIcon,
- showPreviewIcon: showPreviewIcon,
- showDownloadIcon: showDownloadIcon,
- locale: (0, _extends3['default'])({}, locale, propLocale)
- },
- on: (0, _extends3['default'])({
- remove: this.handleManualRemove
- }, (0, _pick2['default'])((0, _propsUtil.getListeners)(this), ['download', 'preview']))
- };
- return h(_UploadList2['default'], uploadListProps);
- }
- },
- render: function render() {
- var _classNames2;
- var h = arguments[0];
- var _getOptionProps2 = (0, _propsUtil.getOptionProps)(this),
- customizePrefixCls = _getOptionProps2.prefixCls,
- showUploadList = _getOptionProps2.showUploadList,
- listType = _getOptionProps2.listType,
- type = _getOptionProps2.type,
- disabled = _getOptionProps2.disabled;
- var _$data = this.$data,
- fileList = _$data.sFileList,
- dragState = _$data.dragState;
- var getPrefixCls = this.configProvider.getPrefixCls;
- var prefixCls = getPrefixCls('upload', customizePrefixCls);
- var vcUploadProps = {
- props: (0, _extends3['default'])({}, this.$props, {
- prefixCls: prefixCls,
- beforeUpload: this.reBeforeUpload
- }),
- on: {
- start: this.onStart,
- error: this.onError,
- progress: this.onProgress,
- success: this.onSuccess,
- reject: this.onReject
- },
- ref: 'uploadRef',
- attrs: (0, _extends3['default'])({}, this.$attrs)
- };
- var children = this.$slots['default'];
- // Remove id to avoid open by label when trigger is hidden
- // https://github.com/ant-design/ant-design/issues/14298
- if (!children || disabled) {
- delete vcUploadProps.props.id;
- delete vcUploadProps.attrs.id;
- }
- var uploadList = showUploadList ? h(_LocaleReceiver2['default'], {
- attrs: {
- componentName: 'Upload',
- defaultLocale: _default3['default'].Upload
- },
- scopedSlots: { 'default': this.renderUploadList }
- }) : null;
- if (type === 'drag') {
- var _classNames;
- 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) {
- return file.status === 'uploading';
- })), (0, _defineProperty3['default'])(_classNames, prefixCls + '-drag-hover', dragState === 'dragover'), (0, _defineProperty3['default'])(_classNames, prefixCls + '-disabled', disabled), _classNames));
- return h('span', [h(
- 'div',
- {
- 'class': dragCls,
- on: {
- 'drop': this.onFileDrop,
- 'dragover': this.onFileDrop,
- 'dragleave': this.onFileDrop
- }
- },
- [h(
- _vcUpload2['default'],
- (0, _babelHelperVueJsxMergeProps2['default'])([vcUploadProps, { 'class': prefixCls + '-btn' }]),
- [h(
- 'div',
- { 'class': prefixCls + '-drag-container' },
- [children]
- )]
- )]
- ), uploadList]);
- }
- 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));
- var uploadButton = h(
- 'div',
- { 'class': uploadButtonCls, style: children ? undefined : { display: 'none' } },
- [h(
- _vcUpload2['default'],
- vcUploadProps,
- [children]
- )]
- );
- if (listType === 'picture-card') {
- return h(
- 'span',
- { 'class': prefixCls + '-picture-card-wrapper' },
- [uploadList, uploadButton]
- );
- }
- return h('span', [uploadButton, uploadList]);
- }
- };
|