ajax.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. if (!$element.closest('.file-widget,.form-item').length) {
  50. $element.before(this.progress.element);
  51. }
  52. else {
  53. $element.closest('.file-widget,.form-item').after(this.progress.element);
  54. }
  55. }
  56. else if (this.progress.type == 'throbber') {
  57. this.progress.element = $('<div class="ajax-progress ajax-progress-throbber"><i class="glyphicon glyphicon-refresh glyphicon-spin"></i></div>');
  58. if (this.progress.message) {
  59. $('.throbber', this.progress.element).after('<div class="message">' + this.progress.message + '</div>');
  60. }
  61. // If element is an input type, append after.
  62. if ($element.is('input')) {
  63. $element.after(this.progress.element);
  64. }
  65. else if ($element.is('select')) {
  66. var $inputGroup = $element.closest('.form-item').find('.input-group-addon, .input-group-btn');
  67. if (!$inputGroup.length) {
  68. $element.wrap('<div class="input-group">');
  69. $inputGroup = $('<span class="input-group-addon">');
  70. $element.after($inputGroup);
  71. }
  72. $inputGroup.append(this.progress.element);
  73. }
  74. // Otherwise append the throbber inside the element.
  75. else {
  76. $element.append(this.progress.element);
  77. }
  78. }
  79. };
  80. })(jQuery);