UNPKG

uikit

Version:

UIkit is a lightweight and modular front-end framework for developing fast and powerful web interfaces.

2 lines (1 loc) 3.61 kB
/*! UIkit 3.21.0 | https://www.getuikit.com | (c) 2014 - 2024 YOOtheme | MIT License */(function(o,i){typeof exports=="object"&&typeof module<"u"?module.exports=i(require("uikit-util")):typeof define=="function"&&define.amd?define("uikitupload",["uikit-util"],i):(o=typeof globalThis<"u"?globalThis:o||self,o.UIkitUpload=i(o.UIkit.util))})(this,function(o){"use strict";var i={props:{i18n:Object},data:{i18n:null},methods:{t(e,...r){var s,a,t;let n=0;return((t=((s=this.i18n)==null?void 0:s[e])||((a=this.$options.i18n)==null?void 0:a[e]))==null?void 0:t.replace(/%s/g,()=>r[n++]||""))||""}}},d={mixins:[i],i18n:{invalidMime:"Invalid File Type: %s",invalidName:"Invalid File Name: %s",invalidSize:"Invalid File Size: %s Kilobytes Max"},props:{allow:String,clsDragover:String,concurrent:Number,maxSize:Number,method:String,mime:String,multiple:Boolean,name:String,params:Object,type:String,url:String},data:{allow:!1,clsDragover:"uk-dragover",concurrent:1,maxSize:0,method:"POST",mime:!1,multiple:!1,name:"files[]",params:{},type:"",url:"",abort:o.noop,beforeAll:o.noop,beforeSend:o.noop,complete:o.noop,completeAll:o.noop,error:o.noop,fail:o.noop,load:o.noop,loadEnd:o.noop,loadStart:o.noop,progress:o.noop},events:{change(e){o.matches(e.target,'input[type="file"]')&&(e.preventDefault(),e.target.files&&this.upload(e.target.files),e.target.value="")},drop(e){l(e);const r=e.dataTransfer;r!=null&&r.files&&(o.removeClass(this.$el,this.clsDragover),this.upload(r.files))},dragenter(e){l(e)},dragover(e){l(e),o.addClass(this.$el,this.clsDragover)},dragleave(e){l(e),o.removeClass(this.$el,this.clsDragover)}},methods:{async upload(e){if(e=o.toArray(e),!e.length)return;o.trigger(this.$el,"upload",[e]);for(const a of e){if(this.maxSize&&this.maxSize*1e3<a.size){this.fail(this.t("invalidSize",this.maxSize));return}if(this.allow&&!h(this.allow,a.name)){this.fail(this.t("invalidName",this.allow));return}if(this.mime&&!h(this.mime,a.type)){this.fail(this.t("invalidMime",this.mime));return}}this.multiple||(e=e.slice(0,1)),this.beforeAll(this,e);const r=f(e,this.concurrent),s=async a=>{const t=new FormData;a.forEach(n=>t.append(this.name,n));for(const n in this.params)t.append(n,this.params[n]);try{const n=await u(this.url,{data:t,method:this.method,responseType:this.type,beforeSend:p=>{const{xhr:c}=p;o.on(c.upload,"progress",this.progress);for(const m of["loadStart","load","loadEnd","abort"])o.on(c,m.toLowerCase(),this[m]);return this.beforeSend(p)}});this.complete(n),r.length?await s(r.shift()):this.completeAll(n)}catch(n){this.error(n)}};await s(r.shift())}}};function h(e,r){return r.match(new RegExp(`^${e.replace(/\//g,"\\/").replace(/\*\*/g,"(\\/[^\\/]+)*").replace(/\*/g,"[^\\/]+").replace(/((?!\\))\?/g,"$1.")}$`,"i"))}function f(e,r){const s=[];for(let a=0;a<e.length;a+=r)s.push(e.slice(a,a+r));return s}function l(e){e.preventDefault(),e.stopPropagation()}async function u(e,r){const s={data:null,method:"GET",headers:{},xhr:new XMLHttpRequest,beforeSend:o.noop,responseType:"",...r};return await s.beforeSend(s),g(e,s)}function g(e,r){return new Promise((s,a)=>{const{xhr:t}=r;for(const n in r)if(n in t)try{t[n]=r[n]}catch{}t.open(r.method.toUpperCase(),e);for(const n in r.headers)t.setRequestHeader(n,r.headers[n]);o.on(t,"load",()=>{t.status===0||t.status>=200&&t.status<300||t.status===304?s(t):a(o.assign(Error(t.statusText),{xhr:t,status:t.status}))}),o.on(t,"error",()=>a(o.assign(Error("Network Error"),{xhr:t}))),o.on(t,"timeout",()=>a(o.assign(Error("Network Timeout"),{xhr:t}))),t.send(r.data)})}return typeof window<"u"&&window.UIkit&&window.UIkit.component("upload",d),d});