forked from danialfarid/ng-file-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-file-upload.min.js
2 lines (2 loc) · 9.08 KB
/
angular-file-upload.min.js
1
2
/*! 3.1.2 */
!function(){function a(a,b){window.XMLHttpRequest.prototype[a]=b(window.XMLHttpRequest.prototype[a])}function b(a,b,c,d,f,g,h){function i(){return"input"===b[0].tagName.toLowerCase()&&b.attr("type")&&"file"===b.attr("type").toLowerCase()}function j(c){g(function(){b.parent().length&&p.push(a.$watch(c,function(a,b){a!=b&&k()}))})}function k(){var c=b.clone();if(b.attr("__afu_gen__")&&angular.element(document.getElementById(b.attr("id").substring(1))).remove(),b.parent().length){for(var d=0;d<p.length;d++)p[d]();b.replaceWith(c),h(c)(a)}return c}function l(d,e){if(d){j(d);var g=f(d)(a);g?(b.attr(e,g),c[e]=g):(b.attr(e,null),delete c[e])}}function m(b){var e;e=b.__files_||b.target&&b.target.files,o(e,c,d,a,b)}function n(a){if(null!=q[0].value&&""!=q[0].value&&(q[0].value=null,-1===navigator.userAgent.indexOf("Trident/7")&&m({target:{files:[]}})),b.attr("__afu_clone__"))b.attr("__afu_clone__",null);else if(-1!==navigator.appVersion.indexOf("MSIE 10")||-1!==navigator.userAgent.indexOf("Trident/7")){var c=k();return c.attr("__afu_clone__",!0),c[0].click(),a.preventDefault(),a.stopPropagation(),!0}}function o(a,b,c,d,h){for(var i=[],j=[],k=f(b.ngAccept)(d),l=angular.isString(k)&&k?new RegExp(e(k),"gi"):null,m=l?null:b.ngAccept,n=0;n<a.length;n++){var o=a.item(n);!(!l||o.type.match(l)||null!=o.name&&o.name.match(l))||m&&!f(m)(d,{$file:o,$event:h})?j.push(o):i.push(o)}g(function(){c&&(f(b.ngModel).assign(d,i),c&&c.$setViewValue(null!=i&&0==i.length?"":i),b.ngModelRejected&&f(b.ngModelRejected).assign(d,j)),b.ngFileChange&&""!=b.ngFileChange&&f(b.ngFileChange)(d,{$files:i,$rejectedFiles:j,$event:h})})}var p=[];l(c.ngMultiple,"multiple"),l(c.ngAccept,"ng-accept"),l(c.ngCapture,"capture"),""!=c.ngFileSelect&&(c.ngFileChange=c.ngFileSelect);var q=b;if(i())b.bind("click",n);else{q=angular.element('<input type="file">'),b.attr("multiple")&&q.attr("multiple",b.attr("multiple")),b.attr("accept")&&q.attr("accept",b.attr("accept")),b.attr("capture")&&q.attr("capture",b.attr("capture"));for(var r in c)if(0==r.indexOf("inputFile")){var s=r.substring("inputFile".length);s=s[0].toLowerCase()+s.substring(1),q.attr(s,c[r])}q.css("width","0px").css("height","0px").css("position","absolute").css("padding",0).css("margin",0).css("overflow","hidden").attr("tabindex","-1").css("opacity",0).attr("__afu_gen__",!0),b.attr("__refElem__",!0),q[0].__refElem__=b[0],b.parent()[0].insertBefore(q[0],b[0]),b.css("overflow","hidden"),b.bind("click",function(a){n(a)||q[0].click()})}q.bind("change",m),b.on("$destroy",function(){for(var a=0;a<p.length;a++)p[a]();b[0]!=q[0]&&q.remove()}),p.push(a.$watch(c.ngModel,function(a,b){a==b||null!=a&&a.length||(-1!==navigator.appVersion.indexOf("MSIE 10")?k():q[0].value=null)}))}function c(a,b,c,f,g,h,i){function j(a,b,c){var d=!0;if(r||s){var e=c.dataTransfer.items;if(null!=e)for(var f=0;f<e.length&&d;f++)d=d&&("file"==e[f].kind||""==e[f].kind)&&(s&&g(s)(a,{$file:e[f],$event:c})||r&&null!=e[f].type&&e[f].type.match(r)||null!=e[f].name&&e[f].name.match(r))}var h=g(b.dragOverClass)(a,{$event:c});return h&&(h.delay&&(p=h.delay),h.accept&&(h=d?h.accept:h.reject)),h||b.dragOverClass||"dragover"}function k(b,c,d,e){function f(c){!(!r||c.type.match(r)||null!=c.name&&c.name.match(r))||s&&!g(s)(a,{$file:c,$event:b})?l.push(c):k.push(c)}function j(a,b,c){if(null!=b)if(b.isDirectory){var d=(c||"")+b.name;f({name:b.name,type:"directory",path:d});var e=b.createReader(),g=[];n++;var h=function(){e.readEntries(function(d){try{if(d.length)g=g.concat(Array.prototype.slice.call(d||[],0)),h();else{for(var e=0;e<g.length;e++)j(a,g[e],(c?c:"")+b.name+"/");n--}}catch(f){n--,console.error(f)}},function(){n--})};h()}else n++,b.file(function(a){try{n--,a.path=(c?c:"")+a.name,f(a)}catch(b){n--,console.error(b)}},function(){n--})}var k=[],l=[],m=b.dataTransfer.items,n=0;if(m&&m.length>0&&"file"!=i.protocol())for(var o=0;o<m.length;o++){if(m[o].webkitGetAsEntry&&m[o].webkitGetAsEntry()&&m[o].webkitGetAsEntry().isDirectory){var p=m[o].webkitGetAsEntry();if(p.isDirectory&&!d)continue;null!=p&&j(k,p)}else{var q=m[o].getAsFile();null!=q&&f(q)}if(!e&&k.length>0)break}else{var t=b.dataTransfer.files;if(null!=t)for(var o=0;o<t.length&&(f(t.item(o)),e||!(k.length>0));o++);}var u=0;!function v(a){h(function(){if(n)10*u++<2e4&&v(10);else{if(!e&&k.length>1){for(var a=0;"directory"==k[a].type;)a++;k=[k[a]]}c(k,l)}},a||0)}()}var l=d();if(c.dropAvailable&&h(function(){a.dropAvailable?a.dropAvailable.value=l:a.dropAvailable=l}),!l)return 0!=g(c.hideOnDropNotAvailable)(a)&&b.css("display","none"),void 0;var m,n=null,o=g(c.stopPropagation)(a),p=1,q=g(c.ngAccept)(a)||c.accept,r=angular.isString(q)&&q?new RegExp(e(q),"gi"):null,s=r?null:c.ngAccept;b[0].addEventListener("dragover",function(d){if(d.preventDefault(),o&&d.stopPropagation(),navigator.userAgent.indexOf("Chrome")>-1){var e=d.dataTransfer.effectAllowed;d.dataTransfer.dropEffect="move"===e||"linkMove"===e?"move":"copy"}h.cancel(n),a.actualDragOverClass||(m=j(a,c,d)),b.addClass(m)},!1),b[0].addEventListener("dragenter",function(a){a.preventDefault(),o&&a.stopPropagation()},!1),b[0].addEventListener("dragleave",function(){n=h(function(){b.removeClass(m),m=null},p||1)},!1),""!=c.ngFileDrop&&(c.ngFileChange=a.ngFileDrop),b[0].addEventListener("drop",function(d){d.preventDefault(),o&&d.stopPropagation(),b.removeClass(m),m=null,k(d,function(b,e){h(function(){f&&(g(c.ngModel).assign(a,b),f&&f.$setViewValue(null!=b&&0==b.length?"":b)),c.ngModelRejected&&a[c.ngModelRejected]&&g(c.ngModelRejected).assign(a,e)}),h(function(){g(c.ngFileChange)(a,{$files:b,$rejectedFiles:e,$event:d})})},0!=g(c.allowDir)(a),c.multiple||g(c.ngMultiple)(a))},!1)}function d(){var a=document.createElement("div");return"draggable"in a&&"ondrop"in a}function e(a){if(a.length>2&&"/"===a[0]&&"/"===a[a.length-1])return a.substring(1,a.length-1);var b=a.split(","),c="";if(b.length>1)for(var d=0;d<b.length;d++)c+="("+e(b[d])+")",d<b.length-1&&(c+="|");else 0==a.indexOf(".")&&(a="*"+a),c="^"+a.replace(new RegExp("[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\-]","g"),"\\$&")+"$",c=c.replace(/\\\*/g,".*").replace(/\\\?/g,".");return c}window.XMLHttpRequest&&!window.XMLHttpRequest.__isFileAPIShim&&a("setRequestHeader",function(a){return function(b,c){if("__setXHR_"===b){var d=c(this);d instanceof Function&&d(this)}else a.apply(this,arguments)}});var f=angular.module("angularFileUpload",[]);f.version="3.1.2",f.service("$upload",["$http","$q","$timeout",function(a,b,c){function d(d){d.method=d.method||"POST",d.headers=d.headers||{},d.transformRequest=d.transformRequest||function(b,c){return window.ArrayBuffer&&b instanceof window.ArrayBuffer?b:a.defaults.transformRequest[0](b,c)};var e=b.defer(),f=e.promise;return d.headers.__setXHR_=function(){return function(a){a&&(d.__XHR=a,d.xhrFn&&d.xhrFn(a),a.upload.addEventListener("progress",function(a){a.config=d,e.notify?e.notify(a):f.progress_fn&&c(function(){f.progress_fn(a)})},!1),a.upload.addEventListener("load",function(a){a.lengthComputable&&(a.config=d,e.notify?e.notify(a):f.progress_fn&&c(function(){f.progress_fn(a)}))},!1))}},a(d).then(function(a){e.resolve(a)},function(a){e.reject(a)},function(a){e.notify(a)}),f.success=function(a){return f.then(function(b){a(b.data,b.status,b.headers,d)}),f},f.error=function(a){return f.then(null,function(b){a(b.data,b.status,b.headers,d)}),f},f.progress=function(a){return f.progress_fn=a,f.then(null,null,function(b){a(b)}),f},f.abort=function(){return d.__XHR&&c(function(){d.__XHR.abort()}),f},f.xhr=function(a){return d.xhrFn=function(b){return function(){b&&b.apply(f,arguments),a.apply(f,arguments)}}(d.xhrFn),f},f}this.upload=function(a){a.headers=a.headers||{},a.headers["Content-Type"]=void 0;a.transformRequest;return a.transformRequest=a.transformRequest?"[object Array]"===Object.prototype.toString.call(a.transformRequest)?a.transformRequest:[a.transformRequest]:[],a.transformRequest.push(function(b){var c=new FormData,d={};for(var e in a.fields)d[e]=a.fields[e];if(b&&(d.data=b),a.formDataAppender)for(var e in d)a.formDataAppender(c,e,d[e]);else for(var e in d){var f=d[e];void 0!==f&&("[object String]"===Object.prototype.toString.call(f)?c.append(e,f):a.sendObjectsAsJsonBlob&&"object"==typeof f?c.append(e,new Blob([f],{type:"application/json"})):c.append(e,JSON.stringify(f)))}if(null!=a.file){var g=a.fileFormDataName||"file";if("[object Array]"===Object.prototype.toString.call(a.file))for(var h="[object String]"===Object.prototype.toString.call(g),i=0;i<a.file.length;i++)c.append(h?g:g[i],a.file[i],a.fileName&&a.fileName[i]||a.file[i].name);else c.append(g,a.file,a.fileName||a.file.name)}return c}),d(a)},this.http=function(a){return d(a)}}]),f.directive("ngFileSelect",["$parse","$timeout","$compile",function(a,c,d){return{restrict:"AEC",require:"?ngModel",link:function(e,f,g,h){b(e,f,g,h,a,c,d)}}}]),f.directive("ngFileDrop",["$parse","$timeout","$location",function(a,b,d){return{restrict:"AEC",require:"?ngModel",link:function(e,f,g,h){c(e,f,g,h,a,b,d)}}}]),f.directive("ngNoFileDrop",function(){return function(a,b){d()&&b.css("display","none")}}),f.directive("ngFileDropAvailable",["$parse","$timeout",function(a,b){return function(c,e,f){if(d()){var g=a(f.ngFileDropAvailable);b(function(){g(c)})}}}]);var g=angular.module("ngFileUpload",[]);for(var h in f)g[h]=f[h]}();