diff --git a/core/misc/ajax.js b/core/misc/ajax.js index af8a29c..d26c47d 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -15,6 +15,7 @@ */ Drupal.ajax = Drupal.ajax || {}; + Drupal.ajaxInstances = Drupal.ajaxInstances || {}; /** * Attaches the Ajax behavior to each Ajax form element. @@ -29,7 +30,7 @@ } $(element_settings.selector).once('drupal-ajax', function () { element_settings.element = this; - Drupal.ajax[element_settings.selector] = new Drupal.ajax(base, this, element_settings); + Drupal.ajaxInstances[element_settings.selector] = new Drupal.ajax(base, this, element_settings); }); } @@ -55,7 +56,7 @@ element_settings.accepts = $(this).data('accepts'); element_settings.dialog = $(this).data('dialog-options'); var baseUseAjax = $(this).attr('id'); - Drupal.ajax[baseUseAjax] = new Drupal.ajax(baseUseAjax, this, element_settings); + Drupal.ajaxInstances[baseUseAjax] = new Drupal.ajax(baseUseAjax, this, element_settings); }); // This class means to submit the form to the action using Ajax. @@ -74,7 +75,7 @@ element_settings.progress = {'type': 'throbber'}; var baseUseAjaxSubmit = $(this).attr('id'); - Drupal.ajax[baseUseAjaxSubmit] = new Drupal.ajax(baseUseAjaxSubmit, this, element_settings); + Drupal.ajaxInstances[baseUseAjaxSubmit] = new Drupal.ajax(baseUseAjaxSubmit, this, element_settings); }); } }; @@ -127,9 +128,10 @@ /** * Ajax object. * - * All Ajax objects on a page are accessible through the global Drupal.ajax - * object and are keyed by the submit button's ID. You can access them from - * your module's JavaScript file to override properties or functions. + * All Ajax objects on a page are accessible through the global + * Drupal.ajaxInstances object and are keyed by the submit button's ID. You + * can access them from your module's JavaScript file to override properties + * or functions. * * For example, if your Ajax enabled button has the ID 'edit-submit', you can * redefine the function that is called to insert the new content like this @@ -137,7 +139,7 @@ * @code * Drupal.behaviors.myCustomAJAXStuff = { * attach: function (context, settings) { - * Drupal.ajax['edit-submit'].commands.insert = function (ajax, response, status) { + * Drupal.ajaxInstances['edit-submit'].commands.insert = function (ajax, response, status) { * new_content = $(response.data); * $('#my-wrapper').append(new_content); * alert('New content was appended to #my-wrapper'); @@ -148,9 +150,9 @@ */ Drupal.ajax = function (base, element, element_settings) { var defaults = { - event: 'mousedown', + event: element ? 'mousedown' : null, keypress: true, - selector: '#' + base, + selector: base ? '#' + base : null, effect: 'none', speed: 'none', method: 'replaceWith', @@ -180,16 +182,17 @@ // If there isn't a form, jQuery.ajax() will be used instead, allowing us to // bind Ajax to links as well. - if (this.element.form) { + if (this.element && this.element.form) { this.$form = $(this.element.form); } // If no Ajax callback URL was given, use the link href or form action. if (!this.url) { - if ($(element).is('a')) { - this.url = $(element).attr('href'); + var $element = $(this.element); + if ($element.is('a')) { + this.url = $element.attr('href'); } - else if (element.form) { + else if (this.element && element.form) { this.url = this.$form.attr('action'); // @todo If there's a file input on this form, then jQuery will submit the @@ -281,6 +284,31 @@ }; /** + * Execute the ajax request. + * + * Allows developers to execute an AJAX request manually without specifying + * an event to respond to. + */ + Drupal.ajax.prototype.execute = function() { + var ajax = this; + // Do not perform another ajax command if one is already in progress. + if (ajax.ajaxing) { + return; + } + + try { + ajax.beforeSerialize(ajax.element, ajax.options); + $.ajax(ajax.options); + } + catch (e) { + // Unset the ajax.ajaxing flag here because it won't be unset during + // the complete response. + ajax.ajaxing = false; + window.alert("An error occurred while attempting to process " + ajax.options.url + ": " + e.message); + } + }; + + /** * Handle a key press. * * The Ajax object will, if instructed, bind to a key press response. This @@ -438,30 +466,47 @@ // from changing its value. $(this.element).addClass('progress-disabled').prop('disabled', true); - // Insert progressbar or throbber. - if (this.progress.type === 'bar') { - var progressBar = new Drupal.ProgressBar('ajax-progress-' + this.element.id, $.noop, this.progress.method, $.noop); - if (this.progress.message) { - progressBar.setProgress(-1, this.progress.message); - } - if (this.progress.url) { - progressBar.startMonitoring(this.progress.url, this.progress.interval || 1500); - } - this.progress.element = $(progressBar.element).addClass('ajax-progress ajax-progress-bar'); - this.progress.object = progressBar; + // Insert progress indicator + var progressIndicatorMethod = 'setProgressIndicator' + this.progress.type.slice(0, 1).toUpperCase() + this.progress.type.slice(1).toLowerCase(); + if (progressIndicatorMethod in this && typeof this[progressIndicatorMethod] === 'function') { + this[progressIndicatorMethod].call(this); $(this.element).after(this.progress.element); } - else if (this.progress.type === 'throbber') { - this.progress.element = $('
 
'); - if (this.progress.message) { - this.progress.element.find('.throbber').after('
' + this.progress.message + '
'); - } - $(this.element).after(this.progress.element); + }; + + /** + * Sets the progress bar progress indicator. + */ + Drupal.ajax.prototype.setProgressIndicatorBar = function() { + var progressBar = new Drupal.ProgressBar('ajax-progress-' + this.element.id, $.noop, this.progress.method, $.noop); + if (this.progress.message) { + progressBar.setProgress(-1, this.progress.message); } - else if (this.progress.type === 'fullscreen') { - this.progress.element = $('
 
'); - $('body').after(this.progress.element); + if (this.progress.url) { + progressBar.startMonitoring(this.progress.url, this.progress.interval || 1500); } + this.progress.element = $(progressBar.element).addClass('ajax-progress ajax-progress-bar'); + this.progress.object = progressBar; + $(this.element).after(this.progress.element); + }; + + /** + * Sets the throbber progress indicator. + */ + Drupal.ajax.prototype.setProgressIndicatorThrobber = function() { + this.progress.element = $('
 
'); + if (this.progress.message) { + this.progress.element.find('.throbber').after('
' + this.progress.message + '
'); + } + $(this.element).after(this.progress.element); + }; + + /** + * Sets the fullscreen progress indicator. + */ + Drupal.ajax.prototype.setProgressIndicatorFullscreen = function() { + this.progress.element = $('
 
'); + $('body').after(this.progress.element); }; /** diff --git a/core/modules/views_ui/js/ajax.js b/core/modules/views_ui/js/ajax.js index e693493..f1780ed 100644 --- a/core/modules/views_ui/js/ajax.js +++ b/core/modules/views_ui/js/ajax.js @@ -89,7 +89,7 @@ element_settings.url = $(this).attr('href'); } var base = $(this).attr('id'); - Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); + Drupal.ajaxInstances[base] = new Drupal.ajax(base, this, element_settings); }); $('div#views-live-preview a') @@ -109,7 +109,7 @@ element_settings.wrapper = 'views-preview-wrapper'; element_settings.method = 'replaceWith'; var base = $(this).attr('id'); - Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); + Drupal.ajaxInstances[base] = new Drupal.ajax(base, this, element_settings); }); // Within a live preview, make exposed widget form buttons re-trigger the @@ -134,7 +134,7 @@ element_settings.event = 'click'; var base = $(this).attr('id'); - Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); + Drupal.ajaxInstances[base] = new Drupal.ajax(base, this, element_settings); }); }