Skip to content

Commit

Permalink
Merge pull request #23 from bluntelk/master
Browse files Browse the repository at this point in the history
Javascript Events
  • Loading branch information
jbouzekri authored Feb 16, 2017
2 parents 6741ab6 + 3eca15f commit 3acca0d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
20 changes: 20 additions & 0 deletions Resources/doc/advanced/javascript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Javascript Events
=================

There are a few javascript events that get fired that can help you integrate your workflow. These Are:

* jb.FileUpload.success
* jb.FileUpload.error
* jb.FileUpload.remove

The Success event provide an extra object parameter that contains:

{
filename: "some-image-name.jpg"
filepath: "http://the.path.to.the/uploaded/image.ext"
originalname: "original-image-name.ext"
}

The remove event provides a similar object, but with empty values.

The error event provides the string error message as its additional parameter.
1 change: 1 addition & 0 deletions Resources/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Advanced

* [Using amazon S3 for storage](advanced/amazons3.md)
* [Validate file ownership](advanced/fileowner.md)
* [Javascript Events](advanced/javascript.md)

Troubleshooting
---------------
Expand Down
37 changes: 26 additions & 11 deletions Resources/public/js/jbfileupload.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
/**
* Fill preview and form hidden field
*
* @param {Object} date
* @param {Object} data
*
* @returns {undefined}
*/
Expand Down Expand Up @@ -122,7 +122,9 @@
$resultError.hide();
if (typeof data.result.files !== "undefined" && typeof data.result.files[0] !== "undefined" && typeof data.result.files[0].error !== "undefined") {
$resultError.show();
$resultError.text(translateMessage(data.result.files[0].error));
var message = translateMessage(data.result.files[0].error);
$resultError.text(message);
$parentTag.trigger('jb.FileUpload.error', [message]);
return;
}

Expand All @@ -134,6 +136,7 @@
}

fillResult(data.result);
$parentTag.trigger('jb.FileUpload.success', [data.result]);
}

/**
Expand All @@ -146,13 +149,21 @@
*/
function fileUploadError(e, data) {
loadingToggle({});
if (typeof e.responseJSON.error !== "undefined" && typeof e.responseJSON.error.message !== "undefined") {
$resultError.show();
$resultError.text(e.responseJSON.error.message);
} else if (typeof e.responseJSON[0] !== "undefined" && typeof e.responseJSON[0].message !== "undefined") {
$resultError.show();
$resultError.text(e.responseJSON[0].message);
var message = "An Unknown Error Occurred";
if (typeof e.responseJSON !== "undefined") {
if (typeof e.responseJSON.error !== "undefined" && typeof e.responseJSON.error.message !== "undefined") {
message = e.responseJSON.error.message;
} else if (typeof e.responseJSON[0] !== "undefined" && typeof e.responseJSON[0].message !== "undefined") {
message = e.responseJSON[0].message;
}
} else {
if (typeof e.statusText !== "undefined") {
message = e.statusText;
}
}
$resultError.show();
$resultError.text(message);
$parentTag.trigger('jb.FileUpload.error', message);
}

/**
Expand Down Expand Up @@ -199,13 +210,16 @@
$resultError.hide();
// Fill preview and hidden field
fillResult(data);
$parentTag.trigger('jb.FileUpload.success', [data]);
// Destroy and hide crop
$cropImg.data('Jcrop').destroy();
toggleCropingTool();
}, 'json').fail(function(data) {
if (typeof data.responseJSON !== "undefined" && typeof data.responseJSON.error !== "undefined") {
$resultError.show();
$resultError.text(translateMessage(data.responseJSON.error));
var message = translateMessage(data.responseJSON.error);
$resultError.text(message);
$parentTag.trigger('jb.FileUpload.error', [message]);
}
});
});
Expand All @@ -220,8 +234,9 @@
filename: '',
originalname: '',
filepath: previewData
}
fillResult(clearResultData)
};
fillResult(clearResultData);
$parentTag.trigger('jb.FileUpload.remove', [clearResultData]);
});

// Load jquery file upload
Expand Down

0 comments on commit 3acca0d

Please sign in to comment.