Skip to content
Snippets Groups Projects
Commit 22656630 authored by Ere Maijala's avatar Ere Maijala
Browse files

Lightbox: Added error handling in ajax call and button handling also for...

Lightbox: Added error handling in ajax call and button handling also for lightbox forms outside lightbox. Added also the possibility to ignore a form inside lightbox or a button inside lightbox form.
parent f07e10b0
No related merge requests found
...@@ -10,7 +10,7 @@ VuFind.lightbox = (function() { ...@@ -10,7 +10,7 @@ VuFind.lightbox = (function() {
var _html = function(html) { var _html = function(html) {
_modalBody.html(html); _modalBody.html(html);
_modal.modal('handleUpdate'); _modal.modal('handleUpdate');
} };
// Public: Present an alert // Public: Present an alert
var showAlert = function(message, type) { var showAlert = function(message, type) {
if ('undefined' == typeof type) { if ('undefined' == typeof type) {
...@@ -26,7 +26,13 @@ VuFind.lightbox = (function() { ...@@ -26,7 +26,13 @@ VuFind.lightbox = (function() {
.after('<div class="alert alert-'+type+'">'+message+'</div>'); .after('<div class="alert alert-'+type+'">'+message+'</div>');
}; };
// Update content /**
* Update content
*
* Form data options:
*
* data-lightbox-ignore = do not submit this form in lightbox
*/
var _update = function(html) { var _update = function(html) {
if (!html.match) return; if (!html.match) return;
// Isolate successes // Isolate successes
...@@ -44,17 +50,13 @@ VuFind.lightbox = (function() { ...@@ -44,17 +50,13 @@ VuFind.lightbox = (function() {
_html(html); _html(html);
_modal.modal('show'); _modal.modal('show');
// Attach capturing events // Attach capturing events
_modalBody.find('a').on('click', _constrainLink); _modalBody.find('a').click(_constrainLink);
// Handle submit buttons attached to a form as well as those in a form. Store // Handle submit buttons attached to a form as well as those in a form. Store
// information about which button was clicked here as checking focused button // information about which button was clicked here as checking focused button
// doesn't work on all browsers and platforms. // doesn't work on all browsers and platforms.
_modalBody.find('[type=submit]').click(function() { _modalBody.find('[type=submit]').click(_storeClickedStatus);
var form = $(this).prop('form') || $(this).closest('form')[0];
$(form.elements).filter('[type=submit]').removeAttr('clicked');
$(this).attr('clicked', true);
});
var forms = _modalBody.find('form'); var forms = _modalBody.find('form:not([data-lightbox-ignore])');
for(var i=0;i<forms.length;i++) { for(var i=0;i<forms.length;i++) {
$(forms[i]).on('submit', _formSubmit); $(forms[i]).on('submit', _formSubmit);
} }
...@@ -65,8 +67,14 @@ VuFind.lightbox = (function() { ...@@ -65,8 +67,14 @@ VuFind.lightbox = (function() {
$('#modal').find('.checkbox-select-item').change(function() { $('#modal').find('.checkbox-select-item').change(function() {
$(this).closest('.modal-body').find('.checkbox-select-all').prop('checked', false); $(this).closest('.modal-body').find('.checkbox-select-all').prop('checked', false);
}); });
} };
var _storeClickedStatus = function() {
var form = $(this).prop('form') || $(this).closest('form')[0];
$(form.elements).filter('[type=submit]').removeAttr('clicked');
$(this).attr('clicked', true);
};
var _xhr = false; var _xhr = false;
// Public: Handle AJAX in the Lightbox // Public: Handle AJAX in the Lightbox
var ajax = function(obj) { var ajax = function(obj) {
...@@ -147,13 +155,19 @@ VuFind.lightbox = (function() { ...@@ -147,13 +155,19 @@ VuFind.lightbox = (function() {
VuFind.modal('show'); VuFind.modal('show');
return false; return false;
} }
} };
/** /**
* Form data options * Handle form submission.
*
* Form data options:
* *
* data-lightbox-onsubmit = on submit, run named function * data-lightbox-onsubmit = on submit, run named function
* data-lightbox-onclose = on close, run named function * data-lightbox-onclose = on close, run named function
*
* Submit button data options:
*
* data-lightbox-ignore = do not handle clicking this button in lightbox
*/ */
var _formSubmit = function(event) { var _formSubmit = function(event) {
// Gather data // Gather data
...@@ -164,7 +178,13 @@ VuFind.lightbox = (function() { ...@@ -164,7 +178,13 @@ VuFind.lightbox = (function() {
// Add submit button information // Add submit button information
var submit = $(form).find('[type=submit][clicked]'); var submit = $(form).find('[type=submit][clicked]');
if (submit.length > 0) { if (submit.length > 0) {
submit.attr('disabled', 'disabled'); if (typeof submit.data('lightbox-ignore') !== 'undefined') {
return true;
}
// Prevent multiple submission of submit button in lightbox
if (submit.closest(_modal).length > 0) {
submit.attr('disabled', 'disabled');
}
var name = submit.attr('name') ? submit.attr('name') : 'submit'; var name = submit.attr('name') ? submit.attr('name') : 'submit';
data.push({'name':name, 'value':submit.attr('value') || 1}); data.push({'name':name, 'value':submit.attr('value') || 1});
} }
...@@ -208,7 +228,7 @@ VuFind.lightbox = (function() { ...@@ -208,7 +228,7 @@ VuFind.lightbox = (function() {
VuFind.modal('show'); VuFind.modal('show');
return false; return false;
} };
/** /**
* Reload the page without causing trouble with POST parameters while keeping hash * Reload the page without causing trouble with POST parameters while keeping hash
...@@ -224,7 +244,7 @@ VuFind.lightbox = (function() { ...@@ -224,7 +244,7 @@ VuFind.lightbox = (function() {
href += new Date().getTime() + '#' + parts[1]; href += new Date().getTime() + '#' + parts[1];
window.location.href = href; window.location.href = href;
} }
} };
// Public: Attach listeners to the page // Public: Attach listeners to the page
var bind = function(target) { var bind = function(target) {
...@@ -237,6 +257,11 @@ VuFind.lightbox = (function() { ...@@ -237,6 +257,11 @@ VuFind.lightbox = (function() {
$(target).find('form[data-lightbox]') $(target).find('form[data-lightbox]')
.unbind('submit', _formSubmit) .unbind('submit', _formSubmit)
.on('submit', _formSubmit); .on('submit', _formSubmit);
// Handle submit buttons attached to a form as well as those in a form. Store
// information about which button was clicked here as checking focused button
// doesn't work on all browsers and platforms.
$('form[data-lightbox] [type=submit]').click(_storeClickedStatus);
}; };
// Reveal // Reveal
...@@ -271,7 +296,7 @@ VuFind.lightbox = (function() { ...@@ -271,7 +296,7 @@ VuFind.lightbox = (function() {
VuFind.lightbox.reset(); VuFind.lightbox.reset();
}); });
VuFind.modal = function(cmd) { _modal.modal(cmd); } VuFind.modal = function(cmd) { _modal.modal(cmd); };
bind(); bind();
} }
}; };
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment