Skip to content

Commit

Permalink
Added new events for the UI version (added started sent completed fai…
Browse files Browse the repository at this point in the history
…led stopped destroyed).
  • Loading branch information
blueimp committed Feb 9, 2012
1 parent 5d58eb6 commit 02c8595
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 17 deletions.
70 changes: 55 additions & 15 deletions js/jquery.fileupload-ui.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* jQuery File Upload User Interface Plugin 6.2
* jQuery File Upload User Interface Plugin 6.3
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
Expand Down Expand Up @@ -75,16 +75,21 @@
.data('data', data);
// Force reflow:
that._reflow = $.support.transition && data.context[0].offsetWidth;
data.context.addClass('in');
if ((that.options.autoUpload || data.autoUpload) &&
data.isValidated) {
data.submit();
}
that._transitionCallback(
data.context.addClass('in'),
function (node) {
if ((that._trigger('added', e, data) !== false) &&
(that.options.autoUpload || data.autoUpload) &&
data.isValidated) {
data.submit();
}
}
);
},
// Callback for the start of each file upload request:
send: function (e, data) {
var that = $(this).data('fileupload');
if (!data.isValidated) {
var that = $(this).data('fileupload');
if (!data.isAdjusted) {
that._adjustMaxNumberOfFiles(-data.files.length);
}
Expand All @@ -106,6 +111,7 @@
parseInt(100, 10) + '%'
);
}
return that._trigger('sent', e, data);
},
// Callback for successful uploads:
done: function (e, data) {
Expand All @@ -128,7 +134,13 @@
// Force reflow:
that._reflow = $.support.transition &&
template[0].offsetWidth;
template.addClass('in');
that._transitionCallback(
template.addClass('in'),
function (node) {
data.context = node;
that._trigger('completed', e, data);
}
);
}
);
});
Expand All @@ -137,7 +149,13 @@
.appendTo(that._files);
// Force reflow:
that._reflow = $.support.transition && template[0].offsetWidth;
template.addClass('in');
that._transitionCallback(
template.addClass('in'),
function (node) {
data.context = node;
that._trigger('completed', e, data);
}
);
}
},
// Callback for failed (abort or error) uploads:
Expand All @@ -159,14 +177,21 @@
// Force reflow:
that._reflow = $.support.transition &&
template[0].offsetWidth;
template.addClass('in');
that._transitionCallback(
template.addClass('in'),
function (node) {
data.context = node;
that._trigger('failed', e, data);
}
);
}
);
} else {
that._transitionCallback(
$(this).removeClass('in'),
function (node) {
node.remove();
that._trigger('failed', e, data);
}
);
}
Expand All @@ -178,7 +203,15 @@
.data('data', data);
// Force reflow:
that._reflow = $.support.transition && data.context[0].offsetWidth;
data.context.addClass('in');
that._transitionCallback(
data.context.addClass('in'),
function (node) {
data.context = node;
that._trigger('failed', e, data);
}
);
} else {
that._trigger('failed', e, data);
}
},
// Callback for upload progress events:
Expand All @@ -198,17 +231,23 @@
);
},
// Callback for uploads start, equivalent to the global ajaxStart event:
start: function () {
$(this).find('.fileupload-buttonbar .progress')
.addClass('in');
start: function (e) {
var that = $(this).data('fileupload');
that._transitionCallback(
$(this).find('.fileupload-buttonbar .progress').addClass('in'),
function (node) {
that._trigger('started', e);
}
);
},
// Callback for uploads stop, equivalent to the global ajaxStop event:
stop: function () {
stop: function (e) {
var that = $(this).data('fileupload');
that._transitionCallback(
$(this).find('.fileupload-buttonbar .progress').removeClass('in'),
function (node) {
node.find('.bar').css('width', '0%');
that._trigger('stopped', e);
}
);
},
Expand All @@ -223,6 +262,7 @@
data.context.removeClass('in'),
function (node) {
node.remove();
that._trigger('destroyed', e, data);
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blueimp-file-upload",
"version": "6.2.0",
"version": "6.3.0",
"title": "jQuery File Upload",
"description": "File Upload widget with multiple file selection, drag&drop support, progress bar and preview images for jQuery. Supports cross-domain, chunked and resumable file uploads. Works with any server-side platform (Google App Engine, PHP, Python, Ruby on Rails, Java, etc.) that supports standard HTML form file uploads.",
"keywords": [
Expand Down
104 changes: 103 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* jQuery File Upload Plugin Test 6.0
* jQuery File Upload Plugin Test 6.3
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2010, Sebastian Tschan
Expand Down Expand Up @@ -923,6 +923,108 @@ $(function () {
$('#fileupload .fileupload-buttonbar .delete').click();
});

asyncTest('added', function () {
expect(1);
var param = {files: [{name: 'test'}]};
$('#fileupload').fileupload({
added: function (e, data) {
start();
strictEqual(
data.files[0].name,
param.files[0].name,
'Triggers added callback'
);
},
send: function () {
return false;
}
}).fileupload('add', param)
});

asyncTest('started', function () {
expect(1);
var param = {files: [{name: 'test'}]};
$('#fileupload').fileupload({
started: function (e) {
start();
ok('Triggers started callback');
return false;
},
sent: function (e, data) {
return false;
}
}).fileupload('send', param)
});

asyncTest('sent', function () {
expect(1);
var param = {files: [{name: 'test'}]};
$('#fileupload').fileupload({
sent: function (e, data) {
start();
strictEqual(
data.files[0].name,
param.files[0].name,
'Triggers sent callback'
);
return false;
}
}).fileupload('send', param)
});

asyncTest('completed', function () {
expect(1);
var param = {files: [{name: 'test'}]};
$('#fileupload').fileupload({
completed: function (e, data) {
start();
ok('Triggers completed callback');
return false;
}
}).fileupload('send', param)
});

asyncTest('failed', function () {
expect(1);
var param = {files: [{name: 'test'}]};
$('#fileupload').fileupload({
failed: function (e, data) {
start();
ok('Triggers failed callback');
return false;
}
}).fileupload('send', param).abort();
});

asyncTest('stopped', function () {
expect(1);
var param = {files: [{name: 'test'}]};
$('#fileupload').fileupload({
stopped: function (e, data) {
start();
ok('Triggers stopped callback');
return false;
}
}).fileupload('send', param)
});

asyncTest('destroy', function () {
expect(1);
$('#fileupload').fileupload({
destroyed: function (e, data) {
start();
ok(true, 'Triggers destroyed callback');
}
});
$('#fileupload').data('fileupload')._renderDownload([{
name: 'test',
delete_url: 'test',
delete_type: 'DELETE'
}]).appendTo($('#fileupload .files')).show()
.find('.delete input').click();
$('#fileupload .fileupload-buttonbar .delete').click();
});

module('UI Options', lifecycleUI);

test('autoUpload', function () {
Expand Down

0 comments on commit 02c8595

Please sign in to comment.