@aimake/nanod
Version:
## 设计模式 NANO DESIGN 是面向于企业级中台化应用的解决方案。服务于 ToB 和 ToE 类型的单页应用,应用于各产品中从而产出了一套设计及前端规范。
474 lines (388 loc) • 14.2 kB
JavaScript
;
var _typeof3 = require("babel-runtime/helpers/typeof");
var _typeof4 = _interopRequireDefault2(_typeof3);
function _interopRequireDefault2(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var React = _interopRequireWildcard(require("react"));
var _reactLifecyclesCompat = require("react-lifecycles-compat");
var _rcUpload = _interopRequireDefault(require("rc-upload"));
var _classnames = _interopRequireDefault(require("classnames"));
var _uniqBy = _interopRequireDefault(require("lodash/uniqBy"));
var _UploadList = _interopRequireDefault(require("./UploadList"));
var _utils = require("./utils");
var _LocaleReceiver = _interopRequireDefault(require("../locale-provider/LocaleReceiver"));
var _default2 = _interopRequireDefault(require("../locale-provider/default"));
var _configProvider = require("../config-provider");
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { "default": obj };
}
function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {};if (obj != null) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {};if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
}newObj["default"] = obj;return newObj;
}
}
function _typeof(obj) {
if (typeof Symbol === "function" && (0, _typeof4["default"])(Symbol.iterator) === "symbol") {
_typeof = function _typeof(obj) {
return typeof obj === "undefined" ? "undefined" : (0, _typeof4["default"])(obj);
};
} else {
_typeof = function _typeof(obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj === "undefined" ? "undefined" : (0, _typeof4["default"])(obj);
};
}return _typeof(obj);
}
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);
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _createClass(Constructor, protoProps, staticProps) {
if (protoProps) _defineProperties(Constructor.prototype, protoProps);if (staticProps) _defineProperties(Constructor, staticProps);return Constructor;
}
function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
return call;
}return _assertThisInitialized(self);
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}return self;
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};return _getPrototypeOf(o);
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } });if (superClass) _setPrototypeOf(subClass, superClass);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;return o;
};return _setPrototypeOf(o, p);
}
var Upload =
/*#__PURE__*/
function (_React$Component) {
_inherits(Upload, _React$Component);
function Upload(props) {
var _this;
_classCallCheck(this, Upload);
_this = _possibleConstructorReturn(this, _getPrototypeOf(Upload).call(this, props));
_this.onStart = function (file) {
var targetItem = (0, _utils.fileToObject)(file);
targetItem.status = 'uploading';
var nextFileList = _this.state.fileList.concat();
var fileIndex = nextFileList.findIndex(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.FormData) {
_this.autoUpdateProgress(0, targetItem);
}
};
_this.onSuccess = function (response, file) {
_this.clearProgressTimer();
try {
if (typeof response === 'string') {
response = JSON.parse(response);
}
} catch (e) {
/* do nothing */
}
var fileList = _this.state.fileList;
var targetItem = (0, _utils.getFileItem)(file, fileList); // removed
if (!targetItem) {
return;
}
targetItem.status = 'done';
targetItem.response = response;
_this.onChange({
file: _extends({}, targetItem),
fileList: fileList
});
};
_this.onProgress = function (e, file) {
var fileList = _this.state.fileList;
var targetItem = (0, _utils.getFileItem)(file, fileList); // removed
if (!targetItem) {
return;
}
targetItem.percent = e.percent;
_this.onChange({
event: e,
file: _extends({}, targetItem),
fileList: _this.state.fileList
});
};
_this.onError = function (error, response, file) {
_this.clearProgressTimer();
var fileList = _this.state.fileList;
var targetItem = (0, _utils.getFileItem)(file, fileList); // removed
if (!targetItem) {
return;
}
targetItem.error = error;
targetItem.response = response;
targetItem.status = 'error';
_this.onChange({
file: _extends({}, targetItem),
fileList: fileList
});
};
_this.handleManualRemove = function (file) {
_this.upload.abort(file);
file.status = 'removed'; // eslint-disable-line
_this.handleRemove(file);
};
_this.onChange = function (info) {
if (!('fileList' in _this.props)) {
_this.setState({
fileList: info.fileList
});
}
var onChange = _this.props.onChange;
if (onChange) {
onChange(info);
}
};
_this.onFileDrop = function (e) {
_this.setState({
dragState: e.type
});
};
_this.beforeUpload = function (file, fileList) {
if (!_this.props.beforeUpload) {
return true;
}
var result = _this.props.beforeUpload(file, fileList);
if (result === false) {
_this.onChange({
file: file,
fileList: (0, _uniqBy["default"])(_this.state.fileList.concat(fileList.map(_utils.fileToObject)), function (item) {
return item.uid;
})
});
return false;
} else if (result && result.then) {
return result;
}
return true;
};
_this.saveUpload = function (node) {
_this.upload = node;
};
_this.renderUploadList = function (locale) {
var _this$props = _this.props,
showUploadList = _this$props.showUploadList,
listType = _this$props.listType,
onPreview = _this$props.onPreview;
var showRemoveIcon = showUploadList.showRemoveIcon,
showPreviewIcon = showUploadList.showPreviewIcon;
return React.createElement(_UploadList["default"], {
listType: listType,
items: _this.state.fileList,
onPreview: onPreview,
onRemove: _this.handleManualRemove,
showRemoveIcon: showRemoveIcon,
showPreviewIcon: showPreviewIcon,
locale: _extends({}, locale, _this.props.locale)
});
};
_this.renderUpload = function (_ref2) {
var _classNames2;
var getPrefixCls = _ref2.getPrefixCls;
var _this$props2 = _this.props,
customizePrefixCls = _this$props2.prefixCls,
className = _this$props2.className,
showUploadList = _this$props2.showUploadList,
listType = _this$props2.listType,
type = _this$props2.type,
disabled = _this$props2.disabled,
children = _this$props2.children;
var prefixCls = getPrefixCls('upload', customizePrefixCls);
var rcUploadProps = _extends({
onStart: _this.onStart,
onError: _this.onError,
onProgress: _this.onProgress,
onSuccess: _this.onSuccess
}, _this.props, {
prefixCls: prefixCls,
beforeUpload: _this.beforeUpload
});
delete rcUploadProps.className;
var uploadList = showUploadList ? React.createElement(_LocaleReceiver["default"], {
componentName: "Upload",
defaultLocale: _default2["default"].Upload
}, _this.renderUploadList) : null;
if (type === 'drag') {
var _classNames;
var dragCls = (0, _classnames["default"])(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-drag"), true), _defineProperty(_classNames, "".concat(prefixCls, "-drag-uploading"), _this.state.fileList.some(function (file) {
return file.status === 'uploading';
})), _defineProperty(_classNames, "".concat(prefixCls, "-drag-hover"), _this.state.dragState === 'dragover'), _defineProperty(_classNames, "".concat(prefixCls, "-disabled"), disabled), _classNames));
return React.createElement("span", {
className: className
}, React.createElement("div", {
className: dragCls,
onDrop: _this.onFileDrop,
onDragOver: _this.onFileDrop,
onDragLeave: _this.onFileDrop
}, React.createElement(_rcUpload["default"], _extends({}, rcUploadProps, {
ref: _this.saveUpload,
className: "".concat(prefixCls, "-btn")
}), React.createElement("div", {
className: "".concat(prefixCls, "-drag-container")
}, children))), uploadList);
}
var uploadButtonCls = (0, _classnames["default"])(prefixCls, (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-select"), true), _defineProperty(_classNames2, "".concat(prefixCls, "-select-").concat(listType), true), _defineProperty(_classNames2, "".concat(prefixCls, "-disabled"), disabled), _classNames2));
var uploadButton = React.createElement("div", {
className: uploadButtonCls,
style: {
display: children ? '' : 'none'
}
}, React.createElement(_rcUpload["default"], _extends({}, rcUploadProps, {
ref: _this.saveUpload
})));
if (listType === 'picture-card') {
return React.createElement("span", {
className: className
}, uploadList, uploadButton);
}
return React.createElement("span", {
className: className
}, uploadButton, uploadList);
};
_this.state = {
fileList: props.fileList || props.defaultFileList || [],
dragState: 'drop'
};
return _this;
}
_createClass(Upload, [{
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.clearProgressTimer();
}
}, {
key: "autoUpdateProgress",
value: 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);
}
}, {
key: "handleRemove",
value: function handleRemove(file) {
var _this3 = this;
var onRemove = this.props.onRemove;
Promise.resolve(typeof onRemove === 'function' ? onRemove(file) : onRemove).then(function (ret) {
// Prevent removing file
if (ret === false) {
return;
}
var removedFileList = (0, _utils.removeFileItem)(file, _this3.state.fileList);
if (removedFileList) {
_this3.onChange({
file: file,
fileList: removedFileList
});
}
});
}
}, {
key: "clearProgressTimer",
value: function clearProgressTimer() {
clearInterval(this.progressTimer);
}
}, {
key: "render",
value: function render() {
return React.createElement(_configProvider.ConfigConsumer, null, this.renderUpload);
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps) {
if ('fileList' in nextProps) {
return {
fileList: nextProps.fileList || []
};
}
return null;
}
}]);
return Upload;
}(React.Component);
Upload.defaultProps = {
type: 'select',
multiple: false,
action: '',
data: {},
accept: '',
beforeUpload: _utils.T,
showUploadList: true,
listType: 'text',
className: '',
disabled: false,
supportServerRender: true
};
(0, _reactLifecyclesCompat.polyfill)(Upload);
var _default = Upload;
exports["default"] = _default;