From 41376338db2fa547e75ec9d11cfeac88adf58778 Mon Sep 17 00:00:00 2001 From: Asaf David Date: Tue, 4 Feb 2014 15:32:37 +0200 Subject: [PATCH] Added notify support to the uploader --- build/ng-s3upload.js | 3 +++ build/ng-s3upload.min.js | 4 ++-- src/ng-s3upload/services/s3-uploader.js | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/build/ng-s3upload.js b/build/ng-s3upload.js index 1bd6d75..0a1ec13 100644 --- a/build/ng-s3upload.js +++ b/build/ng-s3upload.js @@ -79,6 +79,9 @@ angular.module('ngS3upload.services', []). } else { scope.progress = 'unable to compute'; } + if (typeof deferred.notify === 'function') { + deferred.notify({type: 'progress', value: scope.progress}); + } }); } function uploadComplete(e) { diff --git a/build/ng-s3upload.min.js b/build/ng-s3upload.min.js index ed307d4..77a4185 100644 --- a/build/ng-s3upload.min.js +++ b/build/ng-s3upload.min.js @@ -1,2 +1,2 @@ -/*! ngs3upload 27-01-2014 */ -!function(){angular.module("ngS3upload.config",[]).value("ngS3upload.config",{debug:!0}).config(["$compileProvider",function(a){angular.isDefined(a.urlSanitizationWhitelist)?a.urlSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/):a.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/)}]),angular.module("ngS3upload.directives",[]),angular.module("ngS3upload",["ngS3upload.config","ngS3upload.directives","ngS3upload.services","ngSanitize"]),angular.module("ngS3upload.services",[]).service("S3Uploader",["$http","$q","$window",function(a,b){this.uploads=0;var c=this;this.getUploadOptions=function(c){var d=b.defer();return a.get(c).success(function(a){d.resolve(a)}).error(function(a){d.reject(a)}),d.promise},this.randomString=function(a){for(var b="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",c="",d=a;d>0;--d)c+=b[Math.round(Math.random()*(b.length-1))];return c},this.upload=function(a,d,e,f,g,h,i,j,k){function l(b){a.$apply(function(){a.progress=b.lengthComputable?Math.round(100*b.loaded/b.total):"unable to compute"})}function m(){a.$apply(function(){c.uploads--,a.uploading=!1,a.success=!0,p.resolve()})}function n(){a.$apply(function(){c.uploads--,a.uploading=!1,a.success=!1,p.reject()})}function o(){a.$apply(function(){c.uploads--,a.uploading=!1,a.success=!1,p.reject()})}var p=b.defer();a.attempt=!0;var q=new FormData;q.append("key",e),q.append("acl",f),q.append("Content-Type",k.type),q.append("AWSAccessKeyId",h),q.append("policy",i),q.append("signature",j),q.append("file",k);var r=new XMLHttpRequest;return r.upload.addEventListener("progress",l,!1),r.addEventListener("load",m,!1),r.addEventListener("error",n,!1),r.addEventListener("abort",o,!1),a.uploading=!0,this.uploads++,r.open("POST",d,!0),r.send(q),p.promise},this.isUploading=function(){return this.uploads>0}}]),angular.module("ngS3upload.directives",[]).directive("s3Upload",["$parse","S3Uploader",function(a,b){return{restrict:"AC",require:"?ngModel",replace:!0,transclude:!1,scope:!0,controller:["$scope","$element","$attrs","$transclude",function(a){a.attempt=!1,a.success=!1,a.uploading=!1,a.barClass=function(){return{"bar-success":a.attempt&&!a.uploading&&a.success}}}],compile:function(){return{pre:function(a,b,c){if(angular.isUndefined(c.bucket))throw Error("bucket is a mandatory attribute")},post:function(a,c,d,e){var f=angular.extend({},a.$eval(d.s3UploadOptions||d.options));f=angular.extend({submitOnChange:!0,getOptionsUri:"/getS3Options",acl:"public-read",uploadingKey:"uploading",folder:""},f);var g=a.$eval(d.bucket),h=angular.element(c.children()[0]),i=angular.element(c.find("input")[0]);h.bind("click",function(){i[0].click()}),e.$render=function(){a.filename=e.$viewValue};var j=function(){var c=i[0].files[0],d=c.name,h=d.split(".").pop();a.$apply(function(){b.getUploadOptions(f.getOptionsUri).then(function(d){e.$setValidity("uploading",!1);var i="https://"+g+".s3.amazonaws.com/",j=f.folder+(new Date).getTime()+"-"+b.randomString(16)+"."+h;b.upload(a,i,j,f.acl,c.type,d.key,d.policy,d.signature,c).then(function(){e.$setViewValue(i+j),a.filename=e.$viewValue,e.$setValidity("uploading",!0),e.$setValidity("succeeded",!0)},function(){a.filename=e.$viewValue,e.$setValidity("uploading",!0),e.$setValidity("succeeded",!1)})},function(a){throw Error("Can't receive the needed options for S3 "+a)})})};c.bind("change",function(){f.submitOnChange&&j()})}}},template:'
Stored file
'}}])}(window,document); \ No newline at end of file +/*! ngs3upload 04-02-2014 */ +!function(){angular.module("ngS3upload.config",[]).value("ngS3upload.config",{debug:!0}).config(["$compileProvider",function(a){angular.isDefined(a.urlSanitizationWhitelist)?a.urlSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/):a.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|data):/)}]),angular.module("ngS3upload.directives",[]),angular.module("ngS3upload",["ngS3upload.config","ngS3upload.directives","ngS3upload.services","ngSanitize"]),angular.module("ngS3upload.services",[]).service("S3Uploader",["$http","$q","$window",function(a,b){this.uploads=0;var c=this;this.getUploadOptions=function(c){var d=b.defer();return a.get(c).success(function(a){d.resolve(a)}).error(function(a){d.reject(a)}),d.promise},this.randomString=function(a){for(var b="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",c="",d=a;d>0;--d)c+=b[Math.round(Math.random()*(b.length-1))];return c},this.upload=function(a,d,e,f,g,h,i,j,k){function l(b){a.$apply(function(){a.progress=b.lengthComputable?Math.round(100*b.loaded/b.total):"unable to compute","function"==typeof p.notify&&p.notify({type:"progress",value:a.progress})})}function m(){a.$apply(function(){c.uploads--,a.uploading=!1,a.success=!0,p.resolve()})}function n(){a.$apply(function(){c.uploads--,a.uploading=!1,a.success=!1,p.reject()})}function o(){a.$apply(function(){c.uploads--,a.uploading=!1,a.success=!1,p.reject()})}var p=b.defer();a.attempt=!0;var q=new FormData;q.append("key",e),q.append("acl",f),q.append("Content-Type",k.type),q.append("AWSAccessKeyId",h),q.append("policy",i),q.append("signature",j),q.append("file",k);var r=new XMLHttpRequest;return r.upload.addEventListener("progress",l,!1),r.addEventListener("load",m,!1),r.addEventListener("error",n,!1),r.addEventListener("abort",o,!1),a.uploading=!0,this.uploads++,r.open("POST",d,!0),r.send(q),p.promise},this.isUploading=function(){return this.uploads>0}}]),angular.module("ngS3upload.directives",[]).directive("s3Upload",["$parse","S3Uploader",function(a,b){return{restrict:"AC",require:"?ngModel",replace:!0,transclude:!1,scope:!0,controller:["$scope","$element","$attrs","$transclude",function(a){a.attempt=!1,a.success=!1,a.uploading=!1,a.barClass=function(){return{"bar-success":a.attempt&&!a.uploading&&a.success}}}],compile:function(){return{pre:function(a,b,c){if(angular.isUndefined(c.bucket))throw Error("bucket is a mandatory attribute")},post:function(a,c,d,e){var f=angular.extend({},a.$eval(d.s3UploadOptions||d.options));f=angular.extend({submitOnChange:!0,getOptionsUri:"/getS3Options",acl:"public-read",uploadingKey:"uploading",folder:""},f);var g=a.$eval(d.bucket),h=angular.element(c.children()[0]),i=angular.element(c.find("input")[0]);h.bind("click",function(){i[0].click()}),e.$render=function(){a.filename=e.$viewValue};var j=function(){var c=i[0].files[0],d=c.name,h=d.split(".").pop();a.$apply(function(){b.getUploadOptions(f.getOptionsUri).then(function(d){e.$setValidity("uploading",!1);var i="https://"+g+".s3.amazonaws.com/",j=f.folder+(new Date).getTime()+"-"+b.randomString(16)+"."+h;b.upload(a,i,j,f.acl,c.type,d.key,d.policy,d.signature,c).then(function(){e.$setViewValue(i+j),a.filename=e.$viewValue,e.$setValidity("uploading",!0),e.$setValidity("succeeded",!0)},function(){a.filename=e.$viewValue,e.$setValidity("uploading",!0),e.$setValidity("succeeded",!1)})},function(a){throw Error("Can't receive the needed options for S3 "+a)})})};c.bind("change",function(){f.submitOnChange&&j()})}}},template:'
Stored file
'}}])}(window,document); \ No newline at end of file diff --git a/src/ng-s3upload/services/s3-uploader.js b/src/ng-s3upload/services/s3-uploader.js index 10192af..bac887f 100644 --- a/src/ng-s3upload/services/s3-uploader.js +++ b/src/ng-s3upload/services/s3-uploader.js @@ -51,6 +51,9 @@ angular.module('ngS3upload.services', []). } else { scope.progress = 'unable to compute'; } + if (typeof deferred.notify === 'function') { + deferred.notify({type: 'progress', value: scope.progress}); + } }); } function uploadComplete(e) {