Browse Source

This changes the spinner element such that the another widget can prevent the

submission of the form if need be.
pull/571/head
Nigel Banks 10 years ago
parent
commit
d4343e265f
  1. 34
      js/spinner.js

34
js/spinner.js

@ -1,9 +1,11 @@
/*jshint browser: true, devel:true*/
/*global jQuery, Drupal, Spinner*/
/** /**
* @file * @file
* Triggers the display of a spinning icon when the form is submitted. * Triggers the display of a spinning icon when the form is submitted.
*/ */
(function ($) { (function ($) {
'use strict';
Drupal.behaviors.spinner = { Drupal.behaviors.spinner = {
attach: function(context, settings) { attach: function(context, settings) {
// Store what triggered the submit. // Store what triggered the submit.
@ -15,35 +17,41 @@
// On enter the first submit button is assumed as is most often the // On enter the first submit button is assumed as is most often the
// case and this is part of the HTML 5 specification, although some // case and this is part of the HTML 5 specification, although some
// Browsers may choose the button with the lowest tab-index. // Browsers may choose the button with the lowest tab-index.
if (event.which == 13) { if (event.which === 13) {
$(this).data('clicked', $(':submit', this).first()); $(this).data('clicked', $(':submit', this).first());
} }
}); });
}); });
for (var base in settings.spinner) { $.each(settings.spinner, function (base, value) {
var id = '#' + base; var id = '#' + base,
// Don't add spinner to the hidden next/ingest button. message = $('<div/>').text(settings.spinner[base].message);
if (id != '#edit-hidden-next') { if (id !== '#edit-hidden-next') {
$(id, context).once('spinner', function () { $(id, context).once('spinner', function () {
var spinner = new Spinner(settings.spinner[base].opts); var spinner = new Spinner(settings.spinner[base].opts);
$(id).parents('form').one('submit', function (event) { $(id).parents('form').submit(function (event) {
if ($(this).data('clicked').is(id)) { // If some other widget is preventing form submission we should
// not attempt to submit at this time.
if (event.isDefaultPrevented()) {
return;
}
if ($(this).data('clicked').is(id) && $(this).data('submitted') === undefined) {
event.preventDefault(); event.preventDefault();
// Prevent this from being entered a second time.
$(this).data('submitted', true);
// Add Message. // Add Message.
var message = $('<div/>').text(settings.spinner[base].message);
$(id).after(message); $(id).after(message);
// Make UI changes. // Make UI changes.
spinner.spin(this); spinner.spin(this);
$('#edit-next').hide(); $('#edit-next').hide();
$('#edit-prev').hide(); $('#edit-prev').hide();
// Submit the form after a set timeout, this handles problems with // Submit the form after a set timeout, this handles problems with
// safari, in that safari submit's immediately.. // safari, in that safari submits immediately..
if (navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1) { if (navigator.userAgent.indexOf('Safari') !== -1 && navigator.userAgent.indexOf('Chrome') === -1) {
$(':submit').attr('disabled', 'disabled'); $(':submit').attr('disabled', 'disabled');
} }
setTimeout(function () { setTimeout(function () {
// Allow for the button to be clicked, then click it then // Allow for the button to be clicked, then click it then
// prevent the default behavoir. // prevent the default behaviour.
$(id).removeAttr('disabled') $(id).removeAttr('disabled')
.click() .click()
.click(function (event) { .click(function (event) {
@ -55,7 +63,7 @@
}); });
}); });
} }
} });
} }
}; };
})(jQuery); })(jQuery);

Loading…
Cancel
Save