10
10
/**
11
11
* The angular file upload module
12
12
* @author : nerv
13
- * @version : 0.2.9.7 , 2013-12-31
13
+ * @version : 0.2.9.8 , 2013-12-31
14
14
*/
15
15
var app = angular . module ( 'angularFileUpload' , [ ] ) ;
16
16
17
17
/**
18
18
* The angular file upload module
19
19
* @author : nerv
20
- * @version : 0.2.9.7 , 2013-12-31
20
+ * @version : 0.2.9.8 , 2013-12-31
21
21
*/
22
22
23
23
// It is attached to an element that catches the event drop file
@@ -57,7 +57,7 @@ app.directive('ngFileDrop', [ '$fileUploader', function ($fileUploader) {
57
57
/**
58
58
* The angular file upload module
59
59
* @author : nerv
60
- * @version : 0.2.9.7 , 2013-12-31
60
+ * @version : 0.2.9.8 , 2013-12-31
61
61
*/
62
62
63
63
// It is attached to an element which will be assigned to a class "ng-file-over" or ng-file-over="className"
@@ -78,7 +78,7 @@ app.directive('ngFileOver', function () {
78
78
/**
79
79
* The angular file upload module
80
80
* @author : nerv
81
- * @version : 0.2.9.7 , 2013-12-31
81
+ * @version : 0.2.9.8 , 2013-12-31
82
82
*/
83
83
84
84
// It is attached to <input type="file"> element like <ng-file-select="options">
@@ -99,7 +99,7 @@ app.directive('ngFileSelect', [ '$fileUploader', function ($fileUploader) {
99
99
/**
100
100
* The angular file upload module
101
101
* @author : nerv
102
- * @version : 0.2.9.7 , 2013-12-31
102
+ * @version : 0.2.9.8 , 2013-12-31
103
103
*/
104
104
105
105
app . factory ( '$fileUploader' , [ '$compile' , '$rootScope' , '$http' , '$window' , function ( $compile , $rootScope , $http , $window ) {
@@ -134,6 +134,7 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
134
134
this . bind ( 'beforeupload' , Item . prototype . _beforeupload ) ;
135
135
this . bind ( 'in:progress' , Item . prototype . _progress ) ;
136
136
this . bind ( 'in:success' , Item . prototype . _success ) ;
137
+ this . bind ( 'in:cancel' , Item . prototype . _cancel ) ;
137
138
this . bind ( 'in:error' , Item . prototype . _error ) ;
138
139
this . bind ( 'in:complete' , Item . prototype . _complete ) ;
139
140
this . bind ( 'changedqueue' , this . _changedQueue ) ;
@@ -220,8 +221,10 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
220
221
* @param {Item|Number } value
221
222
*/
222
223
removeFromQueue : function ( value ) {
223
- var index = angular . isObject ( value ) ? this . getIndexOfItem ( value ) : value ;
224
- var item = this . queue . splice ( index , 1 ) [ 0 ] ;
224
+ var index = this . getIndexOfItem ( value ) ;
225
+ var item = this . queue [ index ] ;
226
+ item . isUploading && item . cancel ( ) ;
227
+ this . queue . splice ( index , 1 ) ;
225
228
item . _destroyForm ( ) ;
226
229
this . trigger ( 'changedqueue' , item ) ;
227
230
} ,
@@ -231,6 +234,7 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
231
234
*/
232
235
clearQueue : function ( ) {
233
236
this . queue . forEach ( function ( item ) {
237
+ item . isUploading && item . cancel ( ) ;
234
238
item . _destroyForm ( ) ;
235
239
} , this ) ;
236
240
this . queue . length = 0 ;
@@ -239,11 +243,11 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
239
243
240
244
/**
241
245
* Returns a index of item from the queue
242
- * @param item
246
+ * @param { Item|Number } value
243
247
* @returns {Number }
244
248
*/
245
- getIndexOfItem : function ( item ) {
246
- return this . queue . indexOf ( item ) ;
249
+ getIndexOfItem : function ( value ) {
250
+ return angular . isObject ( value ) ? this . queue . indexOf ( value ) : value ;
247
251
} ,
248
252
249
253
/**
@@ -271,11 +275,11 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
271
275
} ,
272
276
273
277
/**
274
- * Upload a item from the queue
278
+ * Uploads a item from the queue
275
279
* @param {Item|Number } value
276
280
*/
277
281
uploadItem : function ( value ) {
278
- var index = angular . isObject ( value ) ? this . getIndexOfItem ( value ) : value ;
282
+ var index = this . getIndexOfItem ( value ) ;
279
283
var item = this . queue [ index ] ;
280
284
var transport = item . _hasForm ( ) ? '_iframeTransport' : '_xhrTransport' ;
281
285
@@ -290,6 +294,24 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
290
294
this [ transport ] ( item ) ;
291
295
} ,
292
296
297
+
298
+ /**
299
+ * Cancels uploading of item from the queue
300
+ * @param {Item|Number } value
301
+ */
302
+ cancelItem : function ( value ) {
303
+ var index = this . getIndexOfItem ( value ) ;
304
+ var item = this . queue [ index ] ;
305
+
306
+ if ( item . _hasForm ( ) ) {
307
+ // TODO: old browsers
308
+ } else {
309
+ item . _xhr && item . _xhr . abort ( ) ;
310
+ delete item . _xhr ;
311
+ }
312
+ } ,
313
+
314
+
293
315
/**
294
316
* Uploads all not uploaded items of queue
295
317
*/
@@ -305,6 +327,16 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
305
327
} ,
306
328
307
329
330
+ /**
331
+ * Cancels all uploads
332
+ */
333
+ cancelAll : function ( ) {
334
+ this . getNotUploadedItems ( ) . forEach ( function ( item ) {
335
+ item . cancel ( ) ;
336
+ } ) ;
337
+ } ,
338
+
339
+
308
340
/**
309
341
* Returns the total progress
310
342
* @param {Number } [value]
@@ -340,7 +372,7 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
340
372
var item = this . getReadyItems ( ) [ 0 ] ;
341
373
this . isUploading = false ;
342
374
343
- if ( angular . isDefined ( item ) ) {
375
+ if ( angular . isDefined ( item ) ) {
344
376
this . uploadItem ( item ) ;
345
377
return ;
346
378
}
@@ -362,7 +394,7 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
362
394
* The XMLHttpRequest transport
363
395
*/
364
396
_xhrTransport : function ( item ) {
365
- var xhr = new XMLHttpRequest ( ) ;
397
+ var xhr = item . _xhr = new XMLHttpRequest ( ) ;
366
398
var form = new FormData ( ) ;
367
399
var that = this ;
368
400
@@ -394,6 +426,7 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
394
426
} ;
395
427
396
428
xhr . onabort = function ( ) {
429
+ that . trigger ( 'in:cancel' , xhr , item ) ;
397
430
that . trigger ( 'in:complete' , xhr , item ) ;
398
431
} ;
399
432
@@ -499,6 +532,7 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
499
532
isUploading : false ,
500
533
isUploaded : false ,
501
534
isSuccess : false ,
535
+ isCancel : false ,
502
536
isError : false ,
503
537
progress : null ,
504
538
index : null
@@ -512,6 +546,9 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
512
546
upload : function ( ) {
513
547
this . uploader . uploadItem ( this ) ;
514
548
} ,
549
+ cancel : function ( ) {
550
+ this . uploader . cancelItem ( this ) ;
551
+ } ,
515
552
_hasForm : function ( ) {
516
553
return ! ! ( this . file && this . file . _form ) ;
517
554
} ,
@@ -523,6 +560,7 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
523
560
item . isUploading = true ;
524
561
item . isUploaded = false ;
525
562
item . isSuccess = false ;
563
+ item . isCancel = false ;
526
564
item . isError = false ;
527
565
item . progress = null ;
528
566
} ,
@@ -535,30 +573,35 @@ app.factory('$fileUploader', [ '$compile', '$rootScope', '$http', '$window', fun
535
573
item . isUploading = false ;
536
574
item . isUploaded = true ;
537
575
item . isSuccess = true ;
576
+ item . isCancel = false ;
538
577
item . isError = false ;
539
578
item . progress = 100 ;
540
579
item . index = null ;
541
580
item . uploader . trigger ( 'success' , xhr , item , response ) ;
542
581
} ,
582
+ _cancel : function ( event , xhr , item ) {
583
+ item . isReady = false ;
584
+ item . isUploading = false ;
585
+ item . isUploaded = false ;
586
+ item . isSuccess = false ;
587
+ item . isCancel = true ;
588
+ item . isError = false ;
589
+ item . progress = 0 ;
590
+ item . index = null ;
591
+ item . uploader . trigger ( 'cancel' , xhr , item ) ;
592
+ } ,
543
593
_error : function ( event , xhr , item , response ) {
544
594
item . isReady = false ;
545
595
item . isUploading = false ;
546
596
item . isUploaded = true ;
547
597
item . isSuccess = false ;
598
+ item . isCancel = false ;
548
599
item . isError = true ;
549
600
item . progress = 100 ;
550
601
item . index = null ;
551
602
item . uploader . trigger ( 'error' , xhr , item , response ) ;
552
603
} ,
553
604
_complete : function ( event , xhr , item , response ) {
554
- var status = item . uploader . _isSuccessCode ( xhr . status ) ;
555
- item . isReady = false ;
556
- item . isUploading = false ;
557
- item . isUploaded = true ;
558
- item . isSuccess = status ;
559
- item . isError = ! status ;
560
- item . progress = 100 ;
561
- item . index = null ;
562
605
item . uploader . trigger ( 'complete' , xhr , item , response ) ;
563
606
item . removeAfterUpload && item . remove ( ) ;
564
607
}
0 commit comments