33 * progress, resize, thumbnail, preview, validation and CORS
44 * FileAPI Flash shim for old browsers not supporting FormData
55 * @author Danial <danial.farid@gmail.com>
6- * @version 12.2.11
6+ * @version 12.2.12
77 */
88
99( function ( ) {
@@ -424,7 +424,7 @@ if (!window.FileReader) {
424424 * AngularJS file upload directives and services. Supoorts: file upload/drop/paste, resume, cancel/abort,
425425 * progress, resize, thumbnail, preview, validation and CORS
426426 * @author Danial <danial.farid@gmail.com>
427- * @version 12.2.11
427+ * @version 12.2.12
428428 */
429429
430430if ( window . XMLHttpRequest && ! ( window . FileAPI && FileAPI . shouldLoad ) ) {
@@ -445,7 +445,7 @@ if (window.XMLHttpRequest && !(window.FileAPI && FileAPI.shouldLoad)) {
445445
446446var ngFileUpload = angular . module ( 'ngFileUpload' , [ ] ) ;
447447
448- ngFileUpload . version = '12.2.11 ' ;
448+ ngFileUpload . version = '12.2.12 ' ;
449449
450450ngFileUpload . service ( 'UploadBase' , [ '$http' , '$q' , '$timeout' , function ( $http , $q , $timeout ) {
451451 var upload = this ;
@@ -902,13 +902,13 @@ ngFileUpload.service('Upload', ['$parse', '$timeout', '$compile', '$q', 'UploadE
902902 return $q . all ( promises ) ;
903903 }
904904
905- function resize ( files , attr , scope ) {
905+ function resizeFile ( files , attr , scope , ngModel ) {
906906 var resizeVal = upload . attrGetter ( 'ngfResize' , attr , scope ) ;
907907 if ( ! resizeVal || ! upload . isResizeSupported ( ) || ! files . length ) return upload . emptyPromise ( ) ;
908908 if ( resizeVal instanceof Function ) {
909909 var defer = $q . defer ( ) ;
910910 return resizeVal ( files ) . then ( function ( p ) {
911- resizeWithParams ( p , files , attr , scope ) . then ( function ( r ) {
911+ resizeWithParams ( p , files , attr , scope , ngModel ) . then ( function ( r ) {
912912 defer . resolve ( r ) ;
913913 } , function ( e ) {
914914 defer . reject ( e ) ;
@@ -917,11 +917,11 @@ ngFileUpload.service('Upload', ['$parse', '$timeout', '$compile', '$q', 'UploadE
917917 defer . reject ( e ) ;
918918 } ) ;
919919 } else {
920- return resizeWithParams ( resizeVal , files , attr , scope ) ;
920+ return resizeWithParams ( resizeVal , files , attr , scope , ngModel ) ;
921921 }
922922 }
923923
924- function resizeWithParams ( params , files , attr , scope ) {
924+ function resizeWithParams ( params , files , attr , scope , ngModel ) {
925925 var promises = [ upload . emptyPromise ( ) ] ;
926926
927927 function handleFile ( f , i ) {
@@ -937,7 +937,10 @@ ngFileUpload.service('Upload', ['$parse', '$timeout', '$compile', '$q', 'UploadE
937937 files . splice ( i , 1 , resizedFile ) ;
938938 } , function ( e ) {
939939 f . $error = 'resize' ;
940+ ( f . $errorMessages = ( f . $errorMessages || { } ) ) . resize = true ;
940941 f . $errorParam = ( e ? ( e . message ? e . message : e ) + ': ' : '' ) + ( f && f . name ) ;
942+ ngModel . $ngfValidations . push ( { name : 'resize' , valid : false } ) ;
943+ upload . applyModelValidation ( ngModel , files ) ;
941944 } ) ;
942945 }
943946 }
@@ -1033,7 +1036,8 @@ ngFileUpload.service('Upload', ['$parse', '$timeout', '$compile', '$q', 'UploadE
10331036 } , options && options . debounce ? options . debounce . change || options . debounce : 0 ) ;
10341037 }
10351038
1036- resize ( validateAfterResize ? allNewFiles : valids , attr , scope ) . then ( function ( ) {
1039+ var resizingFiles = validateAfterResize ? allNewFiles : valids ;
1040+ resizeFile ( resizingFiles , attr , scope , ngModel ) . then ( function ( ) {
10371041 if ( validateAfterResize ) {
10381042 upload . validate ( allNewFiles , keep ? prevValidFiles . length : 0 , ngModel , attr , scope )
10391043 . then ( function ( validationResult ) {
@@ -1044,8 +1048,18 @@ ngFileUpload.service('Upload', ['$parse', '$timeout', '$compile', '$q', 'UploadE
10441048 } else {
10451049 updateModel ( ) ;
10461050 }
1047- } , function ( e ) {
1048- throw 'Could not resize files ' + e ;
1051+ } , function ( ) {
1052+ for ( var i = 0 ; i < resizingFiles . length ; i ++ ) {
1053+ var f = resizingFiles [ i ] ;
1054+ if ( f . $error === 'resize' ) {
1055+ var index = valids . indexOf ( f ) ;
1056+ if ( index > - 1 ) {
1057+ valids . splice ( index , 1 ) ;
1058+ invalids . push ( f ) ;
1059+ }
1060+ updateModel ( ) ;
1061+ }
1062+ }
10491063 } ) ;
10501064 }
10511065
@@ -1168,21 +1182,25 @@ ngFileUpload.directive('ngfSelect', ['$parse', '$timeout', '$compile', 'Upload',
11681182 fileElem . attr ( 'accept' , attrGetter ( 'ngfAccept' , scope ) ) ;
11691183 } ) ) ;
11701184 }
1171- attr . $observe ( 'accept' , function ( ) {
1185+ unwatches . push ( attr . $observe ( 'accept' , function ( ) {
11721186 fileElem . attr ( 'accept' , attrGetter ( 'accept' ) ) ;
1173- } ) ;
1174- unwatches . push ( function ( ) {
1175- if ( attr . $$observers ) delete attr . $$observers . accept ;
1176- } ) ;
1177- function bindAttrToFileInput ( fileElem ) {
1187+ } ) ) ;
1188+ function bindAttrToFileInput ( fileElem , label ) {
1189+ function updateId ( val ) {
1190+ fileElem . attr ( 'id' , 'ngf-' + val ) ;
1191+ label . attr ( 'id' , 'ngf-label-' + val ) ;
1192+ }
1193+
11781194 for ( var i = 0 ; i < elem [ 0 ] . attributes . length ; i ++ ) {
11791195 var attribute = elem [ 0 ] . attributes [ i ] ;
11801196 if ( attribute . name !== 'type' && attribute . name !== 'class' && attribute . name !== 'style' ) {
1181- if ( attribute . value == null || attribute . value === '' ) {
1182- if ( attribute . name === 'required' ) attribute . value = 'required' ;
1183- if ( attribute . name === 'multiple' ) attribute . value = 'multiple' ;
1197+ if ( attribute . name === 'id' ) {
1198+ updateId ( attribute . value ) ;
1199+ unwatches . push ( attr . $observe ( 'id' , updateId ) ) ;
1200+ } else {
1201+ fileElem . attr ( attribute . name , ( ! attribute . value && ( attribute . name === 'required' ||
1202+ attribute . name === 'multiple' ) ) ? attribute . name : attribute . value ) ;
11841203 }
1185- fileElem . attr ( attribute . name , attribute . name === 'id' ? 'ngf-' + attribute . value : attribute . value ) ;
11861204 }
11871205 }
11881206 }
@@ -1194,15 +1212,12 @@ ngFileUpload.directive('ngfSelect', ['$parse', '$timeout', '$compile', 'Upload',
11941212
11951213 var fileElem = angular . element ( '<input type="file">' ) ;
11961214
1197- bindAttrToFileInput ( fileElem ) ;
1198-
11991215 var label = angular . element ( '<label>upload</label>' ) ;
12001216 label . css ( 'visibility' , 'hidden' ) . css ( 'position' , 'absolute' ) . css ( 'overflow' , 'hidden' )
12011217 . css ( 'width' , '0px' ) . css ( 'height' , '0px' ) . css ( 'border' , 'none' )
12021218 . css ( 'margin' , '0px' ) . css ( 'padding' , '0px' ) . attr ( 'tabindex' , '-1' ) ;
1203- if ( elem . attr ( 'id' ) ) {
1204- label . attr ( 'id' , 'ngf-label-' + elem . attr ( 'id' ) ) ;
1205- }
1219+ bindAttrToFileInput ( fileElem , label ) ;
1220+
12061221 generatedElems . push ( { el : elem , ref : label } ) ;
12071222
12081223 document . body . appendChild ( label . append ( fileElem ) [ 0 ] ) ;
@@ -1227,7 +1242,8 @@ ngFileUpload.directive('ngfSelect', ['$parse', '$timeout', '$compile', 'Upload',
12271242 document . body . appendChild ( fileElem . parent ( ) [ 0 ] ) ;
12281243 fileElem . bind ( 'change' , changeFn ) ;
12291244 }
1230- } catch ( e ) { /*ignore*/ }
1245+ } catch ( e ) { /*ignore*/
1246+ }
12311247
12321248 if ( isDelayedClickSupported ( navigator . userAgent ) ) {
12331249 setTimeout ( function ( ) {
@@ -1257,7 +1273,7 @@ ngFileUpload.directive('ngfSelect', ['$parse', '$timeout', '$compile', 'Upload',
12571273 var currentX = touches [ 0 ] . clientX ;
12581274 var currentY = touches [ 0 ] . clientY ;
12591275 if ( ( Math . abs ( currentX - initialTouchStartX ) > 20 ) ||
1260- ( Math . abs ( currentY - initialTouchStartY ) > 20 ) ) {
1276+ ( Math . abs ( currentY - initialTouchStartY ) > 20 ) ) {
12611277 evt . stopPropagation ( ) ;
12621278 evt . preventDefault ( ) ;
12631279 return false ;
0 commit comments