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.75 KB
/
ng-upload.min.js
1
angular.module("ngUpload",[]).directive("uploadSubmit",["$parse",function(t){function e(t,a){t=angular.element(t);var n=t.parent();return a=a.toLowerCase(),n&&n[0].tagName.toLowerCase()===a?n:n?e(n,a):null}return{restrict:"AC",link:function(a,n,o){var r={};r.enableControls=o.uploadOptionsEnableControls,o.hasOwnProperty("uploadOptionsConvertHidden")&&(r.convertHidden="false"!=o.uploadOptionsConvertHidden),o.hasOwnProperty("uploadOptionBeforeSubmit")&&(r.beforeSubmitCallback=o.uploadOptionBeforeSubmit);var i=e(n,"form"),l=t(o.uploadSubmit);if(!angular.isFunction(l)){var d="The expression on the ngUpload directive does not point to a valid function.";throw d+"\n"}n.bind("click",function(t){if(t&&t.preventDefault(),!n.attr("disabled")){if(void 0!==r.beforeSubmitCallback){var e=a.$apply(function(){return a[r.beforeSubmitCallback](a,t)});if(e===!1)return!1}var o=angular.element("<iframe id='upload_iframe' name='upload_iframe' border='0' width='0' height='0' style='width: 0px; height: 0px; border: none; display: none' />");i.parent().append(o),o.bind("load",function(){var t=o[0],e=t.contentDocument||t.contentWindow.document,r=e.body.innerText||e.body.textContent;try{r=JSON.parse(r)}catch(i){console&&console.log("WARN: XHR response is not valid json")}a.$$phase?l(a,{content:r,completed:!0}):a.$apply(function(){l(a,{content:r,completed:!0})}),""!==r&&setTimeout(function(){o.remove()},250),n.attr("disabled",null),n.attr("title","Click to start upload.")}),a.$$phase?l(a,{content:"Please wait...",completed:!1}):a.$apply(function(){l(a,{content:"Please wait...",completed:!1})});var d=!0;r.enableControls||(n.attr("disabled","disabled"),d=!1),n.attr("title",(d?"[ENABLED]: ":"[DISABLED]: ")+"Uploading, please wait..."),r.convertHidden&&angular.forEach(i.find("input"),function(t){t=angular.element(t),t.attr("ng-model")&&t.attr("type")&&"hidden"==t.attr("type")&&t.attr("value",a.$eval(t.attr("ng-model")))}),i[0].submit()}}).attr("title","Click to start upload.")}}}]).directive("ngUpload",["$parse","$document",function(t,e){function a(t){var a,n=e.find("head");return angular.forEach(n.find("meta"),function(e){e.getAttribute("name")===t&&(a=e)}),angular.element(a)}return{restrict:"AC",link:function(t,e,n){var o={};n.hasOwnProperty("uploadOptionsEnableRailsCsrf")&&(o.enableRailsCsrf="false"!=n.uploadOptionsEnableRailsCsrf),e.attr("target","upload_iframe"),e.attr("method","post");var r=-1==e.attr("action").indexOf("?")?"?":"&";if(e.attr("action",e.attr("action")+r+"_t="+(new Date).getTime()),e.attr("enctype","multipart/form-data"),e.attr("encoding","multipart/form-data"),o.enableRailsCsrf){var i=angular.element("<input />");i.attr("class","upload-csrf-token"),i.attr("type","hidden"),i.attr("name",a("csrf-param").attr("content")),i.val(a("csrf-token").attr("content")),e.append(i)}}}}]);