Skip to content

Commit

Permalink
Merge pull request #9 from IMIO/PM-3355_fix_js_submitFormHelperOnsucc…
Browse files Browse the repository at this point in the history
…essDefault_to_handle_binary_response

Pm 3355 fix js submit form helper onsuccess default to handle binary response
  • Loading branch information
sgeulette authored May 31, 2021
2 parents 9ba14b3 + c55e78d commit 12131e7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Changelog

- Lowercased email address after validation.
[sgeulette]
- Fixed `submitFormHelperOnsuccessDefault` JS function to handle binary response
so it is possible to download the result of the ajax query.
- Added `xhtml.imagesToData` that turns the src of images used in a xhtml
content from an `http` or equivalent URL to a data base64 value.
[gbastien]
Expand Down
82 changes: 70 additions & 12 deletions src/imio/helpers/browser/static/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,48 @@ function setoddeven() {
.filter(':even').addClass('odd');
}

// make jQuery ajax support 'binary' dataType
$.ajaxTransport("+binary", function (options, originalOptions, jqXHR) {
// check for conditions and support for blob / arraybuffer response type
if (window.FormData && ((options.dataType && (options.dataType == 'binary')) || (options.data && ((window.ArrayBuffer && options.data instanceof ArrayBuffer) || (window.Blob && options.data instanceof Blob))))) {
return {
// create new XMLHttpRequest
send: function (headers, callback) {
// setup all variables
var xhr = new XMLHttpRequest(),
url = options.url,
type = options.type,
async = options.async || true,
// blob or arraybuffer. Default is blob
dataType = options.responseType || "blob",
data = options.data || null,
username = options.username || null,
password = options.password || null;

xhr.addEventListener('load', function () {
var data = {};
data[options.dataType] = xhr.response;
// make callback and send data
callback(xhr.status, xhr.statusText, data, xhr.getAllResponseHeaders());
});

xhr.open(type, url, async, username, password);

// setup custom headers
for (var i in headers) {
xhr.setRequestHeader(i, headers[i]);
}

xhr.responseType = dataType;
xhr.send(data);
},
abort: function () {
jqXHR.abort();
}
};
}
});

function submitFormHelper(form, onsuccess=submitFormHelperOnsuccessDefault, onerror=null) {
$('input#form-buttons-apply').click(function(event) {
event.preventDefault();
Expand All @@ -123,10 +165,15 @@ function submitFormHelper(form, onsuccess=submitFormHelperOnsuccessDefault, oner
type: 'POST',
url: this.form.action,
data: data,
dataType: 'binary',
processData: 'false',
responseType: 'arraybuffer',
cache: false,
async: false,
success: function(data) {
if (onsuccess) {return onsuccess(data);}
success: function(data, textStatus, request) {
if (onsuccess) {
return onsuccess(data, textStatus, request);
}
},
error: function(jqXHR, textStatus, errorThrown) {
if (onerror) {
Expand All @@ -140,17 +187,28 @@ function submitFormHelper(form, onsuccess=submitFormHelperOnsuccessDefault, oner
});
}

function submitFormHelperOnsuccessDefault(data) {
// close the overlay
function submitFormHelperOnsuccessDefault(data, textStatus, request) {
cancel_button = $('input#form-buttons-cancel');
if (cancel_button) {
// download file if 'content-disposition' header found
if (request.getResponseHeader('content-disposition')) {
data = new Uint8Array(data);
contentType = request.getResponseHeader('content-type');
var blob = new Blob([data], {type: contentType});
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
cancel_button.click();
}
// reload faceted
if (has_faceted()) {
Faceted.URLHandler.hash_changed();
}
else {
window.location.reload();
URL.revokeObjectURL(objectUrl);
} else {
// close the overlay
if (cancel_button) {
cancel_button.click();
}
// reload faceted
if (has_faceted()) {
Faceted.URLHandler.hash_changed();
} else {
// window.location.reload(); will keep old values of selected checkboxes
window.location.href = window.location.href;
}
}
}

0 comments on commit 12131e7

Please sign in to comment.