From 3eca15f3da03e1cf60fbd8cd30a1b56df30c1c53 Mon Sep 17 00:00:00 2001 From: Jason Playne Date: Thu, 16 Feb 2017 13:42:59 +0800 Subject: [PATCH] Adding Javascript Events to the uploader for better integration opportunities --- Resources/doc/advanced/javascript.md | 20 +++++++++++++++ Resources/doc/index.md | 1 + Resources/public/js/jbfileupload.js | 37 +++++++++++++++++++--------- 3 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 Resources/doc/advanced/javascript.md diff --git a/Resources/doc/advanced/javascript.md b/Resources/doc/advanced/javascript.md new file mode 100644 index 0000000..f1ebb3e --- /dev/null +++ b/Resources/doc/advanced/javascript.md @@ -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. \ No newline at end of file diff --git a/Resources/doc/index.md b/Resources/doc/index.md index faf8183..5833518 100644 --- a/Resources/doc/index.md +++ b/Resources/doc/index.md @@ -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 --------------- diff --git a/Resources/public/js/jbfileupload.js b/Resources/public/js/jbfileupload.js index 52bcd81..3eeefcc 100644 --- a/Resources/public/js/jbfileupload.js +++ b/Resources/public/js/jbfileupload.js @@ -91,7 +91,7 @@ /** * Fill preview and form hidden field * - * @param {Object} date + * @param {Object} data * * @returns {undefined} */ @@ -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; } @@ -134,6 +136,7 @@ } fillResult(data.result); + $parentTag.trigger('jb.FileUpload.success', [data.result]); } /** @@ -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); } /** @@ -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]); } }); }); @@ -220,8 +234,9 @@ filename: '', originalname: '', filepath: previewData - } - fillResult(clearResultData) + }; + fillResult(clearResultData); + $parentTag.trigger('jb.FileUpload.remove', [clearResultData]); }); // Load jquery file upload