Last active
May 22, 2024 11:45
-
-
Save hedleysmith/260a427f1e48f456bb56 to your computer and use it in GitHub Desktop.
Ever wondered how to override the default Ajax progress throbber? We can change the GIF easily with CSS but if you want to do things like change the position of the actualy HTML this is probably the cleanest way to do so.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file | |
* | |
* Overrides the default Ajax progress indicator to do things like change the | |
* styling and positioning. | |
*/ | |
(function ($) { | |
// Drupal's core beforeSend function | |
var beforeSend = Drupal.ajax.prototype.beforeSend; | |
// Add a trigger when beforeSend fires. | |
Drupal.ajax.prototype.beforeSend = function (xmlhttprequest, options) { | |
// Only apply our override on specific fields. | |
if (this.element.name == "field_name") { | |
// Copied and modified from Drupal.ajax.prototype.beforeSend in ajax.js | |
$(this.element).addClass('progress-disabled').attr('disabled', true); | |
// Modify the actualy progress throbber HTML. | |
this.progress.element = $('<div class="ajax-progress ajax-progress-throbber"><div class="throbber"> </div><div class="message">Loading</div></div>'); | |
// Change the position of the throbber. | |
$(this.element).parent().parent().after(this.progress.element); | |
} else { | |
// Send to the default Drupal Ajax function if we're not looking at our specific field. | |
beforeSend.call(this, xmlhttprequest, options); | |
$(document).trigger('beforeSend'); | |
} | |
}; | |
} (jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great, thanks!