forked from twilson63/ngUpload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-upload.min.js
1 lines (1 loc) · 2.25 KB
/
ng-upload.min.js
1
angular.module("ngUpload",[]).directive("uploadSubmit",["$parse",function(){function n(t,a){t=angular.element(t);var e=t.parent();return a=a.toLowerCase(),e&&e[0].tagName.toLowerCase()===a?e:e?n(e,a):null}return{restrict:"AC",link:function(t,a){a.bind("click",function(t){if(t&&(t.preventDefault(),t.stopPropagation()),!a.attr("disabled")){var e=n(a,"form");e.triggerHandler("submit"),e[0].submit()}})}}}]).directive("ngUpload",["$log","$parse","$document",function(n,t,a){function e(n){var t,e=a.find("head");return angular.forEach(e.find("meta"),function(a){a.getAttribute("name")===n&&(t=a)}),angular.element(t)}var r=1;return{restrict:"AC",link:function(a,o,i){function l(n){a.$isUploading=n}function d(){f.unbind("load"),a.$$phase?l(!1):a.$apply(function(){l(!1)});var t,e=(f[0].contentDocument||f[0].contentWindow.document).body;try{t=angular.fromJson(e.innerText||e.textContent)}catch(r){t=e.innerHTML,n.warn("Response is not valid JSON")}a.$$phase?p(a,{content:t}):a.$apply(function(){p(a,{content:t})})}r++;var u={},p=i.ngUpload?t(i.ngUpload):angular.noop,s=i.ngUploadLoading?t(i.ngUploadLoading):null;i.hasOwnProperty("uploadOptionsConvertHidden")&&(u.convertHidden="false"!=i.uploadOptionsConvertHidden),i.hasOwnProperty("uploadOptionsEnableRailsCsrf")&&(u.enableRailsCsrf="false"!=i.uploadOptionsEnableRailsCsrf),i.hasOwnProperty("uploadOptionsBeforeSubmit")&&(u.beforeSubmit=t(i.uploadOptionsBeforeSubmit)),o.attr({target:"upload-iframe-"+r,method:"post",enctype:"multipart/form-data",encoding:"multipart/form-data"});var f=angular.element('<iframe name="upload-iframe-'+r+'" '+'border="0" width="0" height="0" '+'style="width:0px;height:0px;border:none;display:none">');if(u.enableRailsCsrf){var c=angular.element("<input />");c.attr("class","upload-csrf-token"),c.attr("type","hidden"),c.attr("name",e("csrf-param").attr("content")),c.val(e("csrf-token").attr("content")),o.append(c)}o.after(f),l(!1),o.bind("submit",function(){var n=a[i.name];return n&&n.$invalid?!1:u.beforeSubmit?u.beforeSubmit():(f.bind("load",d),u.convertHidden&&angular.forEach(o.find("input"),function(n){var t=angular.element(n);t.attr("ng-model")&&t.attr("type")&&"hidden"==t.attr("type")&&t.attr("value",a.$eval(t.attr("ng-model")))}),a.$$phase?(s&&s(a),l(!0)):a.$apply(function(){s&&s(a),l(!0)}),void 0)})}}}]);