UNPKG

ant-design-vue

Version:

An enterprise-class UI design language and Vue-based implementation

376 lines (331 loc) 13 kB
import { isVNode as _isVNode, createVNode as _createVNode } from "vue"; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } import classNames from '../_util/classNames'; import uniqBy from 'lodash-es/uniqBy'; import findIndex from 'lodash-es/findIndex'; import VcUpload from '../vc-upload'; import BaseMixin from '../_util/BaseMixin'; import { getOptionProps, hasProp, getSlot } from '../_util/props-util'; import initDefaultProps from '../_util/props-util/initDefaultProps'; import LocaleReceiver from '../locale-provider/LocaleReceiver'; import defaultLocale from '../locale-provider/default'; import { defaultConfigProvider } from '../config-provider'; import Dragger from './Dragger'; import UploadList from './UploadList'; import { UploadProps } from './interface'; import { T, fileToObject, genPercentAdd, getFileItem, removeFileItem } from './utils'; import { defineComponent, inject } from 'vue'; import { getDataAndAria } from '../vc-tree/src/util'; function _isSlot(s) { return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !_isVNode(s); } export default defineComponent({ name: 'AUpload', mixins: [BaseMixin], inheritAttrs: false, Dragger: Dragger, props: initDefaultProps(UploadProps, { type: 'select', multiple: false, action: '', data: {}, accept: '', beforeUpload: T, showUploadList: true, listType: 'text', disabled: false, supportServerRender: true }), setup: function setup() { return { upload: null, progressTimer: null, configProvider: inject('configProvider', defaultConfigProvider) }; }, // recentUploadStatus: boolean | PromiseLike<any>; data: function data() { return { sFileList: this.fileList || this.defaultFileList || [], dragState: 'drop' }; }, watch: { fileList: function fileList(val) { this.sFileList = val || []; } }, beforeUnmount: function beforeUnmount() { this.clearProgressTimer(); }, methods: { onStart: function onStart(file) { var targetItem = fileToObject(file); targetItem.status = 'uploading'; var nextFileList = this.sFileList.concat(); var fileIndex = findIndex(nextFileList, function (_ref) { var uid = _ref.uid; return uid === targetItem.uid; }); if (fileIndex === -1) { nextFileList.push(targetItem); } else { nextFileList[fileIndex] = targetItem; } this.handleChange({ 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 = getFileItem(file, fileList); // removed if (!targetItem) { return; } targetItem.status = 'done'; targetItem.response = response; targetItem.xhr = xhr; this.handleChange({ file: _extends({}, targetItem), fileList: fileList }); }, onProgress: function onProgress(e, file) { var fileList = this.sFileList; var targetItem = getFileItem(file, fileList); // removed if (!targetItem) { return; } targetItem.percent = e.percent; this.handleChange({ event: e, file: _extends({}, targetItem), fileList: this.sFileList }); }, onError: function onError(error, response, file) { this.clearProgressTimer(); var fileList = this.sFileList; var targetItem = getFileItem(file, fileList); // removed if (!targetItem) { return; } targetItem.error = error; targetItem.response = response; targetItem.status = 'error'; this.handleChange({ file: _extends({}, 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 = removeFileItem(file, fileList); if (removedFileList) { file.status = 'removed'; // eslint-disable-line if (_this.upload) { _this.upload.abort(file); } _this.handleChange({ file: file, fileList: removedFileList }); } }); }, handleManualRemove: function handleManualRemove(file) { if (this.$refs.uploadRef) { this.$refs.uploadRef.abort(file); } this.handleRemove(file); }, handleChange: function handleChange(info) { if (!hasProp(this, 'fileList')) { this.setState({ sFileList: info.fileList }); } this.$emit('update:fileList', 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.handleChange({ file: file, fileList: uniqBy(stateFileList.concat(fileList.map(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 = 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 _getOptionProps = getOptionProps(this), _getOptionProps$showU = _getOptionProps.showUploadList, showUploadList = _getOptionProps$showU === void 0 ? {} : _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 _this$$props = this.$props, onDownload = _this$$props.onDownload, onPreview = _this$$props.onPreview; var uploadListProps = { listType: listType, items: fileList, previewFile: previewFile, showRemoveIcon: !disabled && showRemoveIcon, showPreviewIcon: showPreviewIcon, showDownloadIcon: showDownloadIcon, locale: _extends(_extends({}, locale), propLocale), onRemove: this.handleManualRemove, onDownload: onDownload, onPreview: onPreview }; return _createVNode(UploadList, uploadListProps, null); } }, render: function render() { var _classNames2; var _getOptionProps2 = getOptionProps(this), customizePrefixCls = _getOptionProps2.prefixCls, showUploadList = _getOptionProps2.showUploadList, listType = _getOptionProps2.listType, type = _getOptionProps2.type, disabled = _getOptionProps2.disabled; var _this$$data = this.$data, fileList = _this$$data.sFileList, dragState = _this$$data.dragState; var _this$$attrs = this.$attrs, className = _this$$attrs.class, style = _this$$attrs.style; var getPrefixCls = this.configProvider.getPrefixCls; var prefixCls = getPrefixCls('upload', customizePrefixCls); var vcUploadProps = _extends(_extends({}, this.$props), { prefixCls: prefixCls, beforeUpload: this.reBeforeUpload, onStart: this.onStart, onError: this.onError, onProgress: this.onProgress, onSuccess: this.onSuccess, onReject: this.onReject, ref: 'uploadRef' }); var uploadList = showUploadList ? _createVNode(LocaleReceiver, { "componentName": "Upload", "defaultLocale": defaultLocale.Upload, "children": this.renderUploadList }, null) : null; var children = getSlot(this); if (type === 'drag') { var _slot; var _classNames; var dragCls = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-drag"), true), _defineProperty(_classNames, "".concat(prefixCls, "-drag-uploading"), fileList.some(function (file) { return file.status === 'uploading'; })), _defineProperty(_classNames, "".concat(prefixCls, "-drag-hover"), dragState === 'dragover'), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _classNames)); return _createVNode("span", _objectSpread({ "class": className }, getDataAndAria(this.$attrs)), [_createVNode("div", { "class": dragCls, "onDrop": this.onFileDrop, "onDragover": this.onFileDrop, "onDragleave": this.onFileDrop, "style": style }, [_createVNode(VcUpload, _objectSpread(_objectSpread({}, vcUploadProps), {}, { "class": "".concat(prefixCls, "-btn") }), _isSlot(_slot = _createVNode("div", { "class": "".concat(prefixCls, "-drag-container") }, _isSlot(children) ? children : { default: function _default() { return [children]; } })) ? _slot : { default: function _default() { return [_slot]; } })]), uploadList]); } var uploadButtonCls = classNames(prefixCls, (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-select"), true), _defineProperty(_classNames2, "".concat(prefixCls, "-select-").concat(listType), true), _defineProperty(_classNames2, "".concat(prefixCls, "-disabled"), disabled), _classNames2)); // Remove id to avoid open by label when trigger is hidden // https://github.com/ant-design/ant-design/issues/14298 if (!children.length || disabled) { delete vcUploadProps.id; } var uploadButton = _createVNode("div", { "class": uploadButtonCls, "style": children.length ? undefined : { display: 'none' } }, [_createVNode(VcUpload, vcUploadProps, _isSlot(children) ? children : { default: function _default() { return [children]; } })]); if (listType === 'picture-card') { return _createVNode("span", { "class": classNames("".concat(prefixCls, "-picture-card-wrapper"), className) }, [uploadList, uploadButton]); } return _createVNode("span", { "class": className }, [uploadButton, uploadList]); } });