UNPKG

@txdfe/at

Version:

一个设计体系组件库

150 lines (149 loc) 6.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _util = require("../../util"); var _util2 = require("../util"); var _request = _interopRequireDefault(require("./request")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var noop = _util.func.noop; var Uploader = exports["default"] = /*#__PURE__*/function () { function Uploader(options) { _classCallCheck(this, Uploader); this.options = _objectSpread({ beforeUpload: noop, onProgress: noop, onSuccess: noop, onError: noop, data: {}, name: 'file', method: 'post' }, options); this.reqs = {}; } return _createClass(Uploader, [{ key: "setOptions", value: function setOptions(options) { _extends(this.options, options); } }, { key: "startUpload", value: function startUpload(files) { var _this = this; var filesArr = files.length ? Array.prototype.slice.call(files) : [files]; filesArr.forEach(function (file) { file.uid = file.uid || (0, _util2.uid)(); _this.upload(file); }); } }, { key: "abort", value: function abort(file) { var reqs = this.reqs; if (file) { var _uid = file; if (file && file.uid) { _uid = file.uid; } if (reqs[_uid]) { reqs[_uid].abort(); delete reqs[_uid]; } } else { Object.keys(reqs).forEach(function (uid) { if (reqs[uid]) { reqs[uid].abort(); } delete reqs[uid]; }); } } }, { key: "upload", value: function upload(file) { var _this2 = this; var _this$options = this.options, beforeUpload = _this$options.beforeUpload, action = _this$options.action, name = _this$options.name, headers = _this$options.headers, timeout = _this$options.timeout, withCredentials = _this$options.withCredentials, method = _this$options.method, data = _this$options.data; var before = beforeUpload(file, { action: action, name: name, headers: headers, timeout: timeout, withCredentials: withCredentials, method: method, data: data }); _util.func.promiseCall(before, function (options) { if (options === false) { var err = new Error(_util2.errorCode.BEFOREUPLOAD_REJECT); err.code = _util2.errorCode.BEFOREUPLOAD_REJECT; return _this2.options.onError(err, null, file); } _this2.post(file, _util.obj.isPlainObject(options) ? options : undefined); }, function (error) { _this2.options.onError(error, null, file); }); } }, { key: "post", value: function post(file) { var _this3 = this; var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var requestOptions = _objectSpread(_objectSpread({}, this.options), options); var action = requestOptions.action, name = requestOptions.name, headers = requestOptions.headers, timeout = requestOptions.timeout, withCredentials = requestOptions.withCredentials, _onProgress = requestOptions.onProgress, _onSuccess = requestOptions.onSuccess, _onError = requestOptions.onError, method = requestOptions.method; var data = requestOptions.data; if (typeof data === 'function') { data = data(file); } var uid = file.uid; var request = typeof requestOptions.request === 'function' ? requestOptions.request : _request["default"]; this.reqs[uid] = request({ action: action, filename: name, file: file, data: data, timeout: timeout, headers: headers, withCredentials: withCredentials, method: method, onProgress: function onProgress(e) { _onProgress(e, file); }, onSuccess: function onSuccess(ret) { delete _this3.reqs[uid]; _onSuccess(ret, file); }, onError: function onError(err, ret) { delete _this3.reqs[uid]; _onError(err, ret, file); } }); } }]); }();