/*! formstone v1.2.1 [upload.js] 2016-08-02 | GPL-3.0 License | formstone.it */

!function(a){"function"==typeof define&&define.amd?define(["jquery","./core"],a):a(jQuery,Formstone)}(function(a,b){"use strict";function c(a){if(b.support.file){var c="";a.label!==!1&&(c+='<div class="'+w.target+'">',c+=a.label,c+="</div>"),c+='<input class="'+w.input+'" type="file"',a.multiple&&(c+=" multiple"),a.accept&&(c+=' accept="'+a.accept+'"'),c+=">",a.baseClasses=[w.base,a.theme,a.customClass].join(" "),this.addClass(a.baseClasses).append(c),a.$input=this.find(v.input),a.queue=[],a.total=0,a.uploading=!1,a.disabled=!0,a.aborting=!1,this.on(x.click,v.target,a,i).on(x.dragEnter,a,m).on(x.dragOver,a,n).on(x.dragLeave,a,o).on(x.drop,a,p),a.$input.on(x.focus,a,j).on(x.blur,a,k).on(x.change,a,l),h.call(this,a)}}function d(a){b.support.file&&(a.$input.off(x.namespace),this.off(x.namespace).removeClass(a.baseClasses).html(""))}function e(b,c){var d;b.aborting=!0;for(var e in b.queue)b.queue.hasOwnProperty(e)&&(d=b.queue[e],("undefined"===a.type(c)||c>=0&&d.index===c)&&(d.started&&!d.complete?d.transfer.abort():f(b,d,"abort")));b.aborting=!1,s(b)}function f(a,b,c){b.error=!0,a.$el.trigger(x.fileError,[b,c]),a.aborting||s(a)}function g(a){a.disabled||(this.addClass(w.disabled),a.$input.prop("disabled",!0),a.disabled=!0)}function h(a){a.disabled&&(this.removeClass(w.disabled),a.$input.prop("disabled",!1),a.disabled=!1)}function i(a){y.killEvent(a);var b=a.data;b.disabled||b.$input.trigger(x.click)}function j(a){a.data.$el.addClass(w.focus)}function k(a){a.data.$el.removeClass(w.focus)}function l(a){y.killEvent(a);var b=a.data,c=b.$input[0].files;!b.disabled&&c.length&&q(b,c)}function m(a){y.killEvent(a);var b=a.data;b.$el.addClass(w.dropping).trigger(x.fileDragEnter)}function n(a){y.killEvent(a);var b=a.data;b.$el.addClass(w.dropping).trigger(x.fileDragOver)}function o(a){y.killEvent(a);var b=a.data;b.$el.removeClass(w.dropping).trigger(x.fileDragLeave)}function p(a){y.killEvent(a);var b=a.data,c=a.originalEvent.dataTransfer.files;b.$el.removeClass(w.dropping),b.disabled||q(b,c)}function q(a,b){for(var c=[],d=0;d<b.length;d++){var e={index:a.total++,file:b[d],name:b[d].name,size:b[d].size,started:!1,complete:!1,error:!1,transfer:null};c.push(e),a.queue.push(e)}a.$el.trigger(x.queued,[c]),a.autoUpload&&r(a),a.$input.val("")}function r(a){a.uploading||(z.on(x.beforeUnload,function(){return a.leave}),a.uploading=!0,a.$el.trigger(x.start,[a.queue])),s(a)}function s(a){var b=0,c=[];for(var d in a.queue)!a.queue.hasOwnProperty(d)||a.queue[d].complete||a.queue[d].error||c.push(a.queue[d]);a.queue=c;for(var e in a.queue)if(a.queue.hasOwnProperty(e)){if(!a.queue[e].started){var f=new FormData;f.append(a.postKey,a.queue[e].file);for(var g in a.postData)a.postData.hasOwnProperty(g)&&f.append(g,a.postData[g]);t(a,f,a.queue[e])}if(b++,b>=a.maxQueue)return;d++}0===b&&(z.off(x.beforeUnload),a.uploading=!1,a.$el.trigger(x.complete))}function t(b,c,d){c=b.beforeSend.call(b.$el,c,d),d.size>=b.maxSize||c===!1||d.error===!0?f(b,d,c?"size":"abort"):(d.started=!0,d.transfer=a.ajax({url:b.action,data:c,dataType:b.dataType,type:"POST",contentType:!1,processData:!1,cache:!1,xhr:function(){var c=a.ajaxSettings.xhr();return c.upload&&c.upload.addEventListener("progress",function(a){var c=0,e=a.loaded||a.position,f=a.total;a.lengthComputable&&(c=Math.ceil(e/f*100)),b.$el.trigger(x.fileProgress,[d,c])},!1),c},beforeSend:function(a,c){b.$el.trigger(x.fileStart,[d])},success:function(a,c,e){d.complete=!0,b.$el.trigger(x.fileComplete,[d,a]),s(b)},error:function(a,c,e){f(b,d,e)}}))}var u=b.Plugin("upload",{widget:!0,defaults:{accept:!1,action:"",autoUpload:!0,beforeSend:function(a){return a},customClass:"",dataType:"html",label:"Drag and drop files or click to select",leave:"You have uploads pending, are you sure you want to leave this page?",maxQueue:2,maxSize:5242880,multiple:!0,postData:{},postKey:"file",theme:"fs-light"},classes:["input","target","multiple","dropping","disabled","focus"],methods:{_construct:c,_destruct:d,disable:g,enable:h,abort:e,start:r}}),v=u.classes,w=v.raw,x=u.events,y=u.functions,z=(b.window,b.$window);x.complete="complete",x.fileComplete="filecomplete",x.fileDragEnter="filedragenter",x.fileDragLeave="filedragleave",x.fileDragOver="filedragover",x.fileError="fileerror",x.fileProgress="fileprogress",x.fileStart="filestart",x.start="start",x.queued="queued"});