ajax.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. (function ($) {
  2. /**
  3. * Override Drupal's AJAX prototype beforeSend function so it can append the
  4. * throbber inside the pager links.
  5. */
  6. Drupal.ajax.prototype.beforeSend = function (xmlhttprequest, options) {
  7. // For forms without file inputs, the jQuery Form plugin serializes the form
  8. // values, and then calls jQuery's $.ajax() function, which invokes this
  9. // handler. In this circumstance, options.extraData is never used. For forms
  10. // with file inputs, the jQuery Form plugin uses the browser's normal form
  11. // submission mechanism, but captures the response in a hidden IFRAME. In this
  12. // circumstance, it calls this handler first, and then appends hidden fields
  13. // to the form to submit the values in options.extraData. There is no simple
  14. // way to know which submission mechanism will be used, so we add to extraData
  15. // regardless, and allow it to be ignored in the former case.
  16. if (this.form) {
  17. options.extraData = options.extraData || {};
  18. // Let the server know when the IFRAME submission mechanism is used. The
  19. // server can use this information to wrap the JSON response in a TEXTAREA,
  20. // as per http://jquery.malsup.com/form/#file-upload.
  21. options.extraData.ajax_iframe_upload = '1';
  22. // The triggering element is about to be disabled (see below), but if it
  23. // contains a value (e.g., a checkbox, textfield, select, etc.), ensure that
  24. // value is included in the submission. As per above, submissions that use
  25. // $.ajax() are already serialized prior to the element being disabled, so
  26. // this is only needed for IFRAME submissions.
  27. var v = $.fieldValue(this.element);
  28. if (v !== null) {
  29. options.extraData[this.element.name] = v;
  30. }
  31. }
  32. var $element = $(this.element);
  33. // Disable the element that received the change to prevent user interface
  34. // interaction while the Ajax request is in progress. ajax.ajaxing prevents
  35. // the element from triggering a new request, but does not prevent the user
  36. // from changing its value.
  37. $element.addClass('progress-disabled').attr('disabled', true);
  38. // Insert progressbar or throbber.
  39. if (this.progress.type == 'bar') {
  40. var progressBar = new Drupal.progressBar('ajax-progress-' + this.element.id, eval(this.progress.update_callback), this.progress.method, eval(this.progress.error_callback));
  41. if (this.progress.message) {
  42. progressBar.setProgress(-1, this.progress.message);
  43. }
  44. if (this.progress.url) {
  45. progressBar.startMonitoring(this.progress.url, this.progress.interval || 500);
  46. }
  47. this.progress.element = $(progressBar.element).addClass('ajax-progress ajax-progress-bar');
  48. this.progress.object = progressBar;
  49. $element.closest('.file-widget,.form-item').after(this.progress.element);
  50. }
  51. else if (this.progress.type == 'throbber') {
  52. this.progress.element = $('<div class="ajax-progress ajax-progress-throbber"><i class="glyphicon glyphicon-refresh glyphicon-spin"></i></div>');
  53. if (this.progress.message) {
  54. $('.throbber', this.progress.element).after('<div class="message">' + this.progress.message + '</div>');
  55. }
  56. // If element is an input type, append after.
  57. if ($element.is('input')) {
  58. $element.after(this.progress.element);
  59. }
  60. else if ($element.is('select')) {
  61. var $inputGroup = $element.closest('.form-item').find('.input-group-addon, .input-group-btn');
  62. if (!$inputGroup.length) {
  63. $element.wrap('<div class="input-group">');
  64. $inputGroup = $('<span class="input-group-addon">');
  65. $element.after($inputGroup);
  66. }
  67. $inputGroup.append(this.progress.element);
  68. }
  69. // Otherwise append the throbber inside the element.
  70. else {
  71. $element.append(this.progress.element);
  72. }
  73. }
  74. };
  75. })(jQuery);