uikit
Version:
UIkit is a lightweight and modular front-end framework for developing fast and powerful web interfaces.
3 lines (2 loc) • 2.93 kB
JavaScript
/*! UIkit 3.6.19 | https://www.getuikit.com | (c) 2014 - 2021 YOOtheme | MIT License */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("uikit-util")):"function"==typeof define&&define.amd?define("uikitupload",["uikit-util"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).UIkitUpload=t(e.UIkit.util)}(this,function(a){"use strict";var e={props:{allow:String,clsDragover:String,concurrent:Number,maxSize:Number,method:String,mime:String,msgInvalidMime:String,msgInvalidName:String,msgInvalidSize: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,msgInvalidMime:"Invalid File Type: %s",msgInvalidName:"Invalid File Name: %s",msgInvalidSize:"Invalid File Size: %s Kilobytes Max",multiple:!1,name:"files[]",params:{},type:"",url:"",abort:a.noop,beforeAll:a.noop,beforeSend:a.noop,complete:a.noop,completeAll:a.noop,error:a.noop,fail:a.noop,load:a.noop,loadEnd:a.noop,loadStart:a.noop,progress:a.noop},events:{change:function(e){a.matches(e.target,'input[type="file"]')&&(e.preventDefault(),e.target.files&&this.upload(e.target.files),e.target.value="")},drop:function(e){t(e);e=e.dataTransfer;e&&e.files&&(a.removeClass(this.$el,this.clsDragover),this.upload(e.files))},dragenter:function(e){t(e)},dragover:function(e){t(e),a.addClass(this.$el,this.clsDragover)},dragleave:function(e){t(e),a.removeClass(this.$el,this.clsDragover)}},methods:{upload:function(e){var o=this;if(e.length){a.trigger(this.$el,"upload",[e]);for(var t=0;t<e.length;t++){if(this.maxSize&&1e3*this.maxSize<e[t].size)return void this.fail(this.msgInvalidSize.replace("%s",this.maxSize));if(this.allow&&!i(this.allow,e[t].name))return void this.fail(this.msgInvalidName.replace("%s",this.allow));if(this.mime&&!i(this.mime,e[t].type))return void this.fail(this.msgInvalidMime.replace("%s",this.mime))}this.multiple||(e=[e[0]]),this.beforeAll(this,e);var n=function(e,t){for(var i=[],o=0;o<e.length;o+=t){for(var n=[],r=0;r<t;r++)n.push(e[o+r]);i.push(n)}return i}(e,this.concurrent),r=function(e){var t,i=new FormData;for(t in e.forEach(function(e){return i.append(o.name,e)}),o.params)i.append(t,o.params[t]);a.ajax(o.url,{data:i,method:o.method,responseType:o.type,beforeSend:function(e){var t=e.xhr;return t.upload&&a.on(t.upload,"progress",o.progress),["loadStart","load","loadEnd","abort"].forEach(function(e){return a.on(t,e.toLowerCase(),o[e])}),o.beforeSend(e)}}).then(function(e){o.complete(e),n.length?r(n.shift()):o.completeAll(e)},function(e){return o.error(e)})};r(n.shift())}}}};function i(e,t){return t.match(new RegExp("^"+e.replace(/\//g,"\\/").replace(/\*\*/g,"(\\/[^\\/]+)*").replace(/\*/g,"[^\\/]+").replace(/((?!\\))\?/g,"$1.")+"$","i"))}function t(e){e.preventDefault(),e.stopPropagation()}return"undefined"!=typeof window&&window.UIkit&&window.UIkit.component("upload",e),e});