@beeapi/nutui
Version:
一套轻量级移动端Vue组件库
7 lines • 17.3 kB
JavaScript
/*!
* @beeapi/nutui v2.2.10 - uploader.js, 451ed2404bee284b891e, Sat Dec 19 2020 12:06:10 GMT+0800 (中国标准时间)
* (c) 2017-2020 JDC
* Released under the MIT License.
*/
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("vue")):"function"==typeof define&&define.amd?define("uploader",["vue"],e):"object"==typeof exports?exports.uploader=e(require("vue")):t.uploader=e(t.Vue)}("undefined"!=typeof self?self:this,(function(t){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(n,o,function(e){return t[e]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="./",r(r.s=153)}({0:function(t,e,r){"use strict";function n(t,e,r,n,o,i,a,u){var s,c="function"==typeof t?t.options:t;if(e&&(c.render=e,c.staticRenderFns=r,c._compiled=!0),n&&(c.functional=!0),i&&(c._scopeId="data-v-"+i),a?(s=function(t){(t=t||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext)||"undefined"==typeof __VUE_SSR_CONTEXT__||(t=__VUE_SSR_CONTEXT__),o&&o.call(this,t),t&&t._registeredComponents&&t._registeredComponents.add(a)},c._ssrRegister=s):o&&(s=u?function(){o.call(this,(c.functional?this.parent:this).$root.$options.shadowRoot)}:o),s)if(c.functional){c._injectStyles=s;var l=c.render;c.render=function(t,e){return s.call(e),l(t,e)}}else{var f=c.beforeCreate;c.beforeCreate=f?[].concat(f,s):[s]}return{exports:t,options:c}}r.d(e,"a",(function(){return n}))},1:function(e,r){e.exports=t},10:function(t,e,r){"use strict";r(8);var n=r(2),o=r.n(n),i=r(1),a=r.n(i);a.a.config.lang="zn-CH";var u={lang:{okBtnTxt:"确 定",cancelBtnTxt:"取 消",calendar:{loadPrevMonth:"加载上一个月",noMoreMonth:"没有更早月份~",start:"开始",end:"结束",title:"选择日期",week:"日,一,二,三,四,五,六",monthTitle:"{year}年{month}月"},menu:{resetBtnTxt:"重置"},searchbar:{placeText:"请输入内容...",textInfo:"搜索"},shortpassword:{tip1:"输入数字密码",tip2:"忘记密码"},steps:{step:"步骤",stepDesc:"步骤描述"},uploader:{xmlError:"对不起,您的浏览器不支持本组件!",typeError:"不支持上传该类型文件",limitError:"文件大小超过限制"}}},s={},c=!1,l=function(){var t=Object.getPrototypeOf(this||a.a).$t;if("function"==typeof t&&a.a.locale)return c||(c=!0,a.a.locale(a.a.config.lang,o()("zn-CH"===a.a.config.lang?u:{},a.a.locale(a.a.config.lang)||{},s))),t.apply(this,arguments)};function f(t,e){var r=l.apply(this,arguments);if(null!=r)return r;var n=o()({},"zn-CH"===a.a.config.lang?u:{},s),i=t&&t.split(".")||[],c=null;if(i.length)for(var f=0;f<i.length;f++){var h=i[f];if(!h||!n[h]){c=null;break}c=n=n[h]}return p(c,e)}function p(t,e){var r=t&&t.match(/{.+?}/g);return r&&e&&(r=r.map((function(t){return t.replace(/\{\s*(\w+|\d+).*?\}/,"$1")}))).forEach((function(r,n){var o=new RegExp("{\\s*"+r+"\\s*(?:=\\s*(\\S*?))?\\s*?}","g");t=t.replace(o,(function(t,n){return"function"==(o=e[r],Object.prototype.toString.call(o).toLowerCase().match(/\s(\w+)/)[1])?e[r]():void 0!==e[r]?e[r]:n||t;var o}))})),t}e.a={methods:{nutTranslate:function(){return f.apply(this,arguments)}}}},120:function(t,e,r){var n=function(t){"use strict";var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function u(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,r){return t[e]=r}}function s(t,e,r,n){var o=e&&e.prototype instanceof f?e:f,i=Object.create(o.prototype),a=new S(n||[]);return i._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return O()}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var u=x(a,r);if(u){if(u===l)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var s=c(t,e,r);if("normal"===s.type){if(n=r.done?"completed":"suspendedYield",s.arg===l)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(n="completed",r.method="throw",r.arg=s.arg)}}}(t,r,a),i}function c(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=s;var l={};function f(){}function p(){}function h(){}var d={};d[o]=function(){return this};var y=Object.getPrototypeOf,v=y&&y(y(_([])));v&&v!==e&&r.call(v,o)&&(d=v);var g=h.prototype=f.prototype=Object.create(d);function m(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function w(t,e){var n;this._invoke=function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,u){var s=c(t[o],t,i);if("throw"!==s.type){var l=s.arg,f=l.value;return f&&"object"==typeof f&&r.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,a,u)}),(function(t){n("throw",t,a,u)})):e.resolve(f).then((function(t){l.value=t,a(l)}),(function(t){return n("throw",t,a,u)}))}u(s.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function x(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=c(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,l;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function b(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function E(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function S(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(b,this),this.reset(!0)}function _(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return i.next=i}}return{next:O}}function O(){return{value:void 0,done:!0}}return p.prototype=g.constructor=h,h.constructor=p,p.displayName=u(h,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===p||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,h):(t.__proto__=h,u(t,a,"GeneratorFunction")),t.prototype=Object.create(g),t},t.awrap=function(t){return{__await:t}},m(w.prototype),w.prototype[i]=function(){return this},t.AsyncIterator=w,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new w(s(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},m(g),u(g,a,"Generator"),g[o]=function(){return this},g.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=_,S.prototype={constructor:S,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=r.call(i,"catchLoc"),s=r.call(i,"finallyLoc");if(u&&s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),l},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),E(r),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;E(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:_(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),l}},t}(t.exports);try{regeneratorRuntime=n}catch(t){Function("r","regeneratorRuntime = r")(n)}},121:function(t,e,r){},153:function(t,e,r){"use strict";r.r(e);var n=r(72),o=r.n(n),i=r(8),a=r.n(i),u=r(74),s=r.n(u),c=r(2),l=r.n(c),f=r(75),p=r.n(f),h=r(76),d=r.n(h),y=function(){function t(e){p()(this,t),this.options={url:"",formData:null,headers:{},withCredentials:!1,isPreview:!0,previewData:null,maxSize:0,acceptType:[],showMsgFn:null,onStart:null,onProgress:null,onPreview:null,onSuccess:null,onFailure:null,xhrStatus:200,readyState:4,xmlError:null,typeError:null,limitError:null},l()(this.options,e),this[this.options.isPreview?"preview":"uploader"]()}return d()(t,[{key:"triggerFunc",value:function(t){return"function"==typeof t?t.bind(this):(console.warn(t+"is not a function!"),function(){})}},{key:"showMsg",value:function(t){"function"==typeof this.options.showMsgFn?this.options.showMsgFn(t):console.log(t)}},{key:"check",value:function(t){if(Array.isArray(t))for(var e in t){if(this.options.maxSize&&t[e].size>this.options.maxSize)return this.showMsg(this.options.limitError),!1;if(this.options.acceptType.length&&-1===this.options.acceptType.indexOf(t[e].type))return this.showMsg(this.options.typeError),!1}else{if(this.options.maxSize&&t.size>this.options.maxSize)return this.showMsg(this.options.limitError),!1;if(this.options.acceptType.length&&-1===this.options.acceptType.indexOf(t.type))return this.showMsg(this.options.typeError),!1}return!0}},{key:"preview",value:function(){var t=this,e=Array.from(this.options.previewData);if(this.check(e)){var r=[];e.map((function(e){var n=new Promise((function(r,n){var o=new FileReader;o.readAsDataURL(e),o.onload=function(e){t.uploader(),r(e)}}));r.push(n)})),Promise.all(r).then((function(e){console.log(e);var r=[];e&&e.map((function(t){r.push(t.target.result)})),t.triggerFunc.call(t.options,t.options.onPreview)(r)}))}}},{key:"uploader",value:function(){var t=this,e=new XMLHttpRequest,r=this.options,n=r.formData;if(e.upload){for(var o in e.upload.addEventListener("progress",(function(e){t.triggerFunc.call(r,r.onProgress)(n,e.loaded,e.total)}),!1),e.onreadystatechange=function(o){4===e.readyState&&(e.status===r.xhrState?t.triggerFunc.call(r,r.onSuccess)(n,e.responseText):t.triggerFunc.call(r,r.onFailure)(n,e.responseText))},e.withCredentials=r.withCredentials,e.open("POST",r.url,!0),r.headers)e.setRequestHeader(o,r.headers[o]);this.triggerFunc.call(r,r.onStart)(),e.send(n),r.clearInput&&(r.$el.value="")}else this.showMsg(this.xmlError)}}]),t}(),v={name:"nut-uploader",mixins:[r(10).a],props:{name:{type:String,default:"file"},url:{type:String,default:""},multiple:{type:Boolean,default:!1},disabled:{type:Boolean,default:!1},isPreview:{type:Boolean,default:!1},maxSize:{type:Number,default:5242880},acceptType:{type:Array,default:function(){return["image/jpeg","image/png","image/gif","image/bmp"]}},selfData:{type:Object,default:function(){return{}}},attach:{type:Object,default:function(){return{}}},headers:{type:Object,default:function(){return{}}},beforeUpload:{type:Function},xhrState:{type:Number,default:200},clearInput:{type:Boolean,default:!0},xmlError:{type:String,default:""},typeError:{type:String,default:"不支持上传该类型文件"},limitError:{type:String,default:"对不起,您的浏览器不支持本组件"},withCredentials:{type:Boolean,default:!1}},data:function(){return{}},methods:{createUploaderOpts:function(){var t=this;return{$el:{},url:this.url,formData:null,headers:{},isPreview:this.isPreview,previewData:null,maxSize:this.maxSize,acceptType:this.acceptType,xhrState:this.xhrState,clearInput:this.clearInput,withCredentials:this.withCredentials,xmlError:this.xmlError||this.nutTranslate("lang.uploader.xmlError"),typeError:this.typeError||this.nutTranslate("lang.uploader.typeError"),limitError:this.limitError||this.nutTranslate("lang.uploader.limitError"),onStart:function(){t.$emit("start")},onProgress:function(e,r,n){t.$emit("progress",e,r,n)},onPreview:function(e){t.$emit("preview",e)},onSuccess:function(e,r){t.$emit("success",e,r)},onFailure:function(e,r){t.$emit("failure",e,r)}}},uploadData:function(t){var e=this,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t.target;if(!this.url)return this.$emit("showMsg","请先配置上传url"),void this.$emit("afterChange",n,t);var o=new FormData,i=this.createUploaderOpts();i.$el=n,this.isPreview&&(i.previewData=n.files);this.multiple&&n.files.length;o.append(n.name,n.files[0]);for(var a=0,u=Object.keys(this.attach);a<u.length;a++){var s=u[a];o.append(s,this.attach[s])}var c=l()(this.selfData,r);if(c)for(var f in c)o.append(f,c[f]);i.formData=o,i.headers=this.headers||{},i.showMsgFn=function(t){e.$emit("showMsg",t)},new y(i),this.$emit("afterChange",n,t)},upload:function(t){var e=this;return s()(o.a.mark((function r(){var n,i;return o.a.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if("function"!=typeof e.beforeUpload){r.next=8;break}return n=new Promise((function(r,n){r(e.beforeUpload(t))})),r.next=4,n;case 4:i=r.sent,"object"===a()(i)&&"object"===a()(i.event)?e.uploadData(i.event,i.data):console.warn("resData: 必须包含 event字段且为input $event 的事件对象"),r.next=9;break;case 8:e.uploadData(t);case 9:case"end":return r.stop()}}),r)})))()}}},g=r(0),m=Object(g.a)(v,(function(){var t=this,e=t.$createElement,r=t._self._c||e;return r("div",{staticClass:"nut-uploader"},[t._t("default"),t._v(" "),r("input",{staticClass:"uploader",attrs:{type:"file",name:t.name,multiple:t.multiple,disabled:t.disabled,accept:t.acceptType},on:{change:function(e){return t.upload(e)}}})],2)}),[],!1,null,null,null).exports;r(121);m.install=function(t){t.component(m.name,m)};e.default=m},2:function(t,e){function r(){return t.exports=r=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},r.apply(this,arguments)}t.exports=r},72:function(t,e,r){t.exports=r(120)},74:function(t,e){function r(t,e,r,n,o,i,a){try{var u=t[i](a),s=u.value}catch(t){return void r(t)}u.done?e(s):Promise.resolve(s).then(n,o)}t.exports=function(t){return function(){var e=this,n=arguments;return new Promise((function(o,i){var a=t.apply(e,n);function u(t){r(a,o,i,u,s,"next",t)}function s(t){r(a,o,i,u,s,"throw",t)}u(void 0)}))}}},75:function(t,e){t.exports=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}},76:function(t,e){function r(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}t.exports=function(t,e,n){return e&&r(t.prototype,e),n&&r(t,n),t}},8:function(t,e){function r(e){return"function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?t.exports=r=function(t){return typeof t}:t.exports=r=function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r(e)}t.exports=r}})}));
//# sourceMappingURL=uploader.js.map