uikit
Version:
UIkit is a lightweight and modular front-end framework for developing fast and powerful web interfaces.
3 lines (2 loc) • 2.89 kB
JavaScript
/*! UIkit 3.2.1 | http://www.getuikit.com | (c) 2014 - 2019 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=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){o(e);var t=e.dataTransfer;t&&t.files&&(a.removeClass(this.$el,this.clsDragover),this.upload(t.files))},dragenter:function(e){o(e)},dragover:function(e){o(e),a.addClass(this.$el,this.clsDragover)},dragleave:function(e){o(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 r=function(e,t){for(var i=[],o=0;o<e.length;o+=t){for(var r=[],n=0;n<t;n++)r.push(e[o+n]);i.push(r)}return i}(e,this.concurrent),n=function(e){var t=new FormData;for(var i in e.forEach(function(e){return t.append(o.name,e)}),o.params)t.append(i,o.params[i]);a.ajax(o.url,{data:t,method:o.method,responseType:o.type,beforeSend:function(e){var t=e.xhr;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),r.length?n(r.shift()):o.completeAll(e)},function(e){return o.error(e)})};n(r.shift())}}}};function i(e,t){return t.match(new RegExp("^"+e.replace(/\//g,"\\/").replace(/\*\*/g,"(\\/[^\\/]+)*").replace(/\*/g,"[^\\/]+").replace(/((?!\\))\?/g,"$1.")+"$","i"))}function o(e){e.preventDefault(),e.stopPropagation()}return"undefined"!=typeof window&&window.UIkit&&window.UIkit.component("upload",e),e});