Skip to content

Commit

Permalink
proper asynchronous upload finish handling (fixes #19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-novakov committed Dec 2, 2013
1 parent 32f157f commit d765d5b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
17 changes: 17 additions & 0 deletions lib/upload/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Ext.define('Ext.ux.upload.Item', {
STATUS_UPLOADED : 'uploaded',
STATUS_UPLOAD_ERROR : 'uploaderror',

progress : null,
status : null,

config : {
/**
* @cfg {Object} fileApiObject (required)
Expand Down Expand Up @@ -115,6 +118,20 @@ Ext.define('Ext.ux.upload.Item', {
this.fireEvent('changestatus', this, status);
},

hasStatus : function(status) {
var itemStatus = this.getStatus();

if (Ext.isArray(status) && Ext.Array.contains(status, itemStatus)) {
return true;
}

if (itemStatus === status) {
return true;
}

return false;
},

isReady : function() {
return (this.status == this.STATUS_READY);
},
Expand Down
10 changes: 5 additions & 5 deletions lib/upload/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Ext.define('Ext.ux.upload.Manager', {
uploader : null,
uploaderOptions : null,
synchronous : true,
filenameEncoder: null
filenameEncoder : null
},

DEFAULT_UPLOADER_CLASS : 'Ext.ux.upload.uploader.ExtJsUploader',
Expand Down Expand Up @@ -83,7 +83,7 @@ Ext.define('Ext.ux.upload.Manager', {
success : this.onUploadSuccess,
failure : this.onUploadFailure,
progress : this.onUploadProgress,
filenameEncoder: this.getFilenameEncoder()
filenameEncoder : this.getFilenameEncoder()
});

this.uploader = Ext.create(uploaderClass, uploaderOptions);
Expand All @@ -108,8 +108,6 @@ Ext.define('Ext.ux.upload.Manager', {

this.startUpload(queue);

queue.reset();

if (this.synchronous) {
this.uploadQueueSync(queue);
return;
Expand Down Expand Up @@ -146,6 +144,8 @@ Ext.define('Ext.ux.upload.Manager', {
},

startUpload : function(queue) {
queue.reset();

this.uploadActive = true;
this.currentQueue = queue;
this.fireEvent('beforeupload', this, queue);
Expand Down Expand Up @@ -174,7 +174,7 @@ Ext.define('Ext.ux.upload.Manager', {
this.uploadNextItemSync();
}

if (this.currentQueue.isLast(item)) {
if (!this.currentQueue.existUploadingItems()) {
this.finishUpload();
}
},
Expand Down
47 changes: 37 additions & 10 deletions lib/upload/Queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,51 @@ Ext.define('Ext.ux.upload.Queue', {
return this.getRange();
},

/**
* Returns an array of items by the specified status.
*
* @param {String/Array}
* @return {Ext.ux.upload.Item[]}
*/
getItemsByStatus : function(status) {
var itemsByStatus = [];

this.each(function(item, index, items) {
if (item.hasStatus(status)) {
itemsByStatus.push(item);
}
});

return itemsByStatus;
},

/**
* Returns an array of items, that have already been uploaded.
*
* @return {Ext.ux.upload.Item[]}
*/
getUploadedItems : function() {
var uploadedItems = [];
var num = this.getCount();
var i;
return this.getItemsByStatus('uploaded');
},

for (i = 0; i < num; i++) {
var item = this.getAt(i);
if (item.isUploaded()) {
uploadedItems.push(item);
}
}
/**
* Returns an array of items, that have not been uploaded yet.
*
* @return {Ext.ux.upload.Item[]}
*/
getUploadingItems : function() {
return this.getItemsByStatus([
'ready', 'uploading'
]);
},

return uploadedItems;
/**
* Returns true, if there are items, that are currently being uploaded.
*
* @return {Boolean}
*/
existUploadingItems : function() {
return (this.getUploadingItems().length > 0);
},

/**
Expand Down

0 comments on commit d765d5b

Please sign in to comment.