mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-30 04:26:44 +00:00
Improved modal forms
- JSON POST response can include feedback messages - Either displayed immediately or after page is reloaded
This commit is contained in:
parent
e62bf92bd6
commit
a6fdda1c1a
@ -8,6 +8,43 @@ function attachSelect(modal) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function afterForm(response, options) {
|
||||||
|
|
||||||
|
// Should we show alerts immediately or cache them?
|
||||||
|
var cache = (options.follow && response.url) ||
|
||||||
|
options.redirect ||
|
||||||
|
options.reload;
|
||||||
|
|
||||||
|
// Display any messages
|
||||||
|
if (response.success) {
|
||||||
|
showAlertOrCache("alert-success", response.success, cache);
|
||||||
|
}
|
||||||
|
if (response.info) {
|
||||||
|
showAlertOrCache("alert-info", response.info, cache);
|
||||||
|
}
|
||||||
|
if (response.warning) {
|
||||||
|
showAlertOrCache("alert-warning", response.warning, cache);
|
||||||
|
}
|
||||||
|
if (response.danger) {
|
||||||
|
showAlertOrCache("alert-danger", response.danger, cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Was a callback provided?
|
||||||
|
if (options.success) {
|
||||||
|
options.success();
|
||||||
|
}
|
||||||
|
else if (options.follow && response.url) {
|
||||||
|
window.location.href = response.url;
|
||||||
|
}
|
||||||
|
else if (options.redirect) {
|
||||||
|
window.location.href = options.redirect;
|
||||||
|
}
|
||||||
|
else if (options.reload) {
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
function launchDeleteForm(modal, url, options = {}) {
|
function launchDeleteForm(modal, url, options = {}) {
|
||||||
|
|
||||||
$(modal).on('shown.bs.modal', function() {
|
$(modal).on('shown.bs.modal', function() {
|
||||||
@ -50,20 +87,7 @@ function launchDeleteForm(modal, url, options = {}) {
|
|||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
success: function (response) {
|
success: function (response) {
|
||||||
$(modal).modal('hide');
|
$(modal).modal('hide');
|
||||||
|
afterForm(response, options);
|
||||||
if (options.success) {
|
|
||||||
options.success();
|
|
||||||
}
|
|
||||||
// Follow the URL returned by the JSON response
|
|
||||||
else if (options.follow && response.url) {
|
|
||||||
window.location.href = response.url;
|
|
||||||
}
|
|
||||||
else if (options.redirect) {
|
|
||||||
window.location.href = options.redirect;
|
|
||||||
}
|
|
||||||
else if (options.reload) {
|
|
||||||
location.reload();
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
error: function (xhr, ajaxOptions, thrownError) {
|
error: function (xhr, ajaxOptions, thrownError) {
|
||||||
alert('Error deleting item:\n' + thrownError);
|
alert('Error deleting item:\n' + thrownError);
|
||||||
@ -129,36 +153,27 @@ function launchModalForm(modal, url, options = {}) {
|
|||||||
type: form.attr('method'),
|
type: form.attr('method'),
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
success: function (response) {
|
success: function (response) {
|
||||||
if (response.form_valid) {
|
|
||||||
|
|
||||||
$(modal).modal('hide');
|
// Form validation was performed
|
||||||
|
if ('form_valid' in response) {
|
||||||
// Form success callback
|
if (response.form_valid) {
|
||||||
if (options.success) {
|
$(modal).modal('hide');
|
||||||
options.success();
|
afterForm(response, options);
|
||||||
}
|
}
|
||||||
// Follow the URL returned by the JSON response
|
// Form was invalid - try again!
|
||||||
else if (options.follow && response.url) {
|
else {
|
||||||
window.location.href = response.url;
|
if (response.html_form) {
|
||||||
|
$(modal + ' .modal-form-content').html(response.html_form);
|
||||||
|
attachSelect(modal);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
alert('HTML form data missing from AJAX response');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Redirect to a specific URL
|
|
||||||
else if (options.redirect) {
|
|
||||||
window.location.href = options.redirect;
|
|
||||||
}
|
|
||||||
// Reload the current page
|
|
||||||
else if (options.reload) {
|
|
||||||
location.reload();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (response.html_form) {
|
|
||||||
var target = modal + ' .modal-form-content';
|
|
||||||
$(target).html(response.html_form);
|
|
||||||
|
|
||||||
attachSelect(modal);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
alert('JSON response missing form data');
|
|
||||||
$(modal).modal('hide');
|
$(modal).modal('hide');
|
||||||
|
afterForm(response, options);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
error: function (xhr, ajaxOptions, thrownError) {
|
error: function (xhr, ajaxOptions, thrownError) {
|
||||||
|
@ -7,6 +7,15 @@ function showAlert(target, message, timeout=5000) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function showAlertOrCache(alertType, message, cache, timeout=5000) {
|
||||||
|
if (cache) {
|
||||||
|
sessionStorage.setItem(alertType, message);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
showAlert('#' + alertType, message, timeout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function showCachedAlerts() {
|
function showCachedAlerts() {
|
||||||
|
|
||||||
// Success Message
|
// Success Message
|
||||||
@ -32,6 +41,4 @@ function showCachedAlerts() {
|
|||||||
showAlert("#alert-danger", sessionStorage.getItem("alert-danger"));
|
showAlert("#alert-danger", sessionStorage.getItem("alert-danger"));
|
||||||
sessionStorage.removeItem("alert-danger");
|
sessionStorage.removeItem("alert-danger");
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionStorage.setItem("alert-danger", 'test');
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user