2007-07-04 15:42:38 +00:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides AJAX-like page updating via AHAH (Asynchronous HTML and HTTP).
|
|
|
|
*
|
|
|
|
* AHAH is a method of making a request via Javascript while viewing an HTML
|
|
|
|
* page. The request returns a small chunk of HTML, which is then directly
|
|
|
|
* injected into the page.
|
|
|
|
*
|
2007-10-05 09:35:09 +00:00
|
|
|
* Drupal uses this file to enhance form elements with #ahah[path] and
|
|
|
|
* #ahah[wrapper] properties. If set, this file will automatically be included
|
2007-07-04 15:42:38 +00:00
|
|
|
* to provide AHAH capabilities.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2007-10-21 18:59:02 +00:00
|
|
|
* Attaches the ahah behavior to each ahah form element.
|
2007-07-04 15:42:38 +00:00
|
|
|
*/
|
2008-10-29 10:01:28 +00:00
|
|
|
Drupal.behaviors.ahah = {
|
|
|
|
attach: function(context) {
|
|
|
|
for (var base in Drupal.settings.ahah) {
|
|
|
|
if (!$('#'+ base + '.ahah-processed').size()) {
|
|
|
|
var element_settings = Drupal.settings.ahah[base];
|
2007-10-05 09:35:09 +00:00
|
|
|
|
2008-10-29 10:01:28 +00:00
|
|
|
$(element_settings.selector).each(function() {
|
|
|
|
element_settings.element = this;
|
|
|
|
var ahah = new Drupal.ahah(base, element_settings);
|
|
|
|
});
|
2007-10-05 09:35:09 +00:00
|
|
|
|
2008-10-29 10:01:28 +00:00
|
|
|
$('#'+ base).addClass('ahah-processed');
|
|
|
|
}
|
2007-07-04 15:42:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AHAH object.
|
|
|
|
*/
|
2007-10-05 09:35:09 +00:00
|
|
|
Drupal.ahah = function(base, element_settings) {
|
2007-07-04 15:42:38 +00:00
|
|
|
// Set the properties for this object.
|
2007-10-05 09:35:09 +00:00
|
|
|
this.element = element_settings.element;
|
|
|
|
this.selector = element_settings.selector;
|
|
|
|
this.event = element_settings.event;
|
2008-02-12 13:52:33 +00:00
|
|
|
this.keypress = element_settings.keypress;
|
2007-10-05 09:35:09 +00:00
|
|
|
this.url = element_settings.url;
|
|
|
|
this.wrapper = '#'+ element_settings.wrapper;
|
|
|
|
this.effect = element_settings.effect;
|
|
|
|
this.method = element_settings.method;
|
2007-10-10 10:24:25 +00:00
|
|
|
this.progress = element_settings.progress;
|
2007-11-19 10:05:48 +00:00
|
|
|
this.button = element_settings.button || { };
|
|
|
|
|
2007-07-04 15:42:38 +00:00
|
|
|
if (this.effect == 'none') {
|
|
|
|
this.showEffect = 'show';
|
|
|
|
this.hideEffect = 'hide';
|
2007-10-05 09:35:09 +00:00
|
|
|
this.showSpeed = '';
|
2007-07-04 15:42:38 +00:00
|
|
|
}
|
|
|
|
else if (this.effect == 'fade') {
|
|
|
|
this.showEffect = 'fadeIn';
|
|
|
|
this.hideEffect = 'fadeOut';
|
2007-10-05 09:35:09 +00:00
|
|
|
this.showSpeed = 'slow';
|
2007-07-04 15:42:38 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.showEffect = this.effect + 'Toggle';
|
|
|
|
this.hideEffect = this.effect + 'Toggle';
|
2007-10-05 09:35:09 +00:00
|
|
|
this.showSpeed = 'slow';
|
2007-07-04 15:42:38 +00:00
|
|
|
}
|
2007-10-05 09:35:09 +00:00
|
|
|
|
|
|
|
// Record the form action and target, needed for iFrame file uploads.
|
|
|
|
var form = $(this.element).parents('form');
|
|
|
|
this.form_action = form.attr('action');
|
|
|
|
this.form_target = form.attr('target');
|
|
|
|
this.form_encattr = form.attr('encattr');
|
|
|
|
|
|
|
|
// Set the options for the ajaxSubmit function.
|
|
|
|
// The 'this' variable will not persist inside of the options object.
|
|
|
|
var ahah = this;
|
|
|
|
var options = {
|
|
|
|
url: ahah.url,
|
2007-11-19 10:05:48 +00:00
|
|
|
data: ahah.button,
|
2007-10-05 09:35:09 +00:00
|
|
|
beforeSubmit: function(form_values, element_settings, options) {
|
|
|
|
return ahah.beforeSubmit(form_values, element_settings, options);
|
|
|
|
},
|
|
|
|
success: function(response, status) {
|
|
|
|
// Sanity check for browser support (object expected).
|
|
|
|
// When using iFrame uploads, responses must be returned as a string.
|
|
|
|
if (typeof(response) == 'string') {
|
|
|
|
response = Drupal.parseJson(response);
|
|
|
|
}
|
|
|
|
return ahah.success(response, status);
|
|
|
|
},
|
|
|
|
complete: function(response, status) {
|
2008-01-04 11:53:21 +00:00
|
|
|
if (status == 'error' || status == 'parsererror') {
|
|
|
|
return ahah.error(response, ahah.url);
|
2007-10-05 09:35:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
dataType: 'json',
|
|
|
|
type: 'POST'
|
|
|
|
};
|
|
|
|
|
|
|
|
// Bind the ajaxSubmit function to the element event.
|
|
|
|
$(element_settings.element).bind(element_settings.event, function() {
|
|
|
|
$(element_settings.element).parents('form').ajaxSubmit(options);
|
|
|
|
return false;
|
|
|
|
});
|
2008-02-12 13:52:33 +00:00
|
|
|
// If necessary, enable keyboard submission so that AHAH behaviors
|
|
|
|
// can be triggered through keyboard input as well as e.g. a mousedown
|
|
|
|
// action.
|
|
|
|
if (element_settings.keypress) {
|
|
|
|
$(element_settings.element).keypress(function(event) {
|
|
|
|
// Detect enter key.
|
|
|
|
if (event.keyCode == 13) {
|
|
|
|
$(element_settings.element).trigger(element_settings.event);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2007-07-04 15:42:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the form redirection submission.
|
|
|
|
*/
|
2007-10-05 09:35:09 +00:00
|
|
|
Drupal.ahah.prototype.beforeSubmit = function (form_values, element, options) {
|
2007-10-10 10:24:25 +00:00
|
|
|
// Disable the element that received the change.
|
|
|
|
$(this.element).addClass('progress-disabled').attr('disabled', true);
|
|
|
|
|
|
|
|
// Insert progressbar or throbber.
|
|
|
|
if (this.progress.type == 'bar') {
|
|
|
|
var progressBar = new Drupal.progressBar('ahah-progress-' + this.element.id, eval(this.progress.update_callback), this.progress.method, eval(this.progress.error_callback));
|
|
|
|
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('ahah-progress ahah-progress-bar');
|
|
|
|
this.progress.object = progressBar;
|
|
|
|
$(this.element).after(this.progress.element);
|
|
|
|
}
|
|
|
|
else if (this.progress.type == 'throbber') {
|
|
|
|
this.progress.element = $('<div class="ahah-progress ahah-progress-throbber"><div class="throbber"> </div></div>');
|
|
|
|
if (this.progress.message) {
|
|
|
|
$('.throbber', this.progress.element).after('<div class="message">' + this.progress.message + '</div>')
|
|
|
|
}
|
|
|
|
$(this.element).after(this.progress.element);
|
|
|
|
}
|
2007-07-04 15:42:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the form redirection completion.
|
|
|
|
*/
|
2007-10-05 09:35:09 +00:00
|
|
|
Drupal.ahah.prototype.success = function (response, status) {
|
2007-07-04 15:42:38 +00:00
|
|
|
var wrapper = $(this.wrapper);
|
2007-10-05 09:35:09 +00:00
|
|
|
var form = $(this.element).parents('form');
|
|
|
|
// Manually insert HTML into the jQuery object, using $() directly crashes
|
|
|
|
// Safari with long string lengths. http://dev.jquery.com/ticket/1152
|
|
|
|
var new_content = $('<div></div>').html(response.data);
|
2007-07-04 15:42:38 +00:00
|
|
|
|
2007-10-05 09:35:09 +00:00
|
|
|
// Restore the previous action and target to the form.
|
|
|
|
form.attr('action', this.form_action);
|
|
|
|
this.form_target ? form.attr('target', this.form_target) : form.removeAttr('target');
|
|
|
|
this.form_encattr ? form.attr('target', this.form_encattr) : form.removeAttr('encattr');
|
2007-07-04 15:42:38 +00:00
|
|
|
|
|
|
|
// Remove the progress element.
|
2007-10-10 10:24:25 +00:00
|
|
|
if (this.progress.element) {
|
|
|
|
$(this.progress.element).remove();
|
2007-10-05 09:35:09 +00:00
|
|
|
}
|
2007-10-10 10:24:25 +00:00
|
|
|
if (this.progress.object) {
|
|
|
|
this.progress.object.stopMonitoring();
|
|
|
|
}
|
|
|
|
$(this.element).removeClass('progress-disabled').attr('disabled', false);
|
2007-07-04 15:42:38 +00:00
|
|
|
|
2007-10-05 09:35:09 +00:00
|
|
|
// Add the new content to the page.
|
|
|
|
Drupal.freezeHeight();
|
2007-07-04 15:42:38 +00:00
|
|
|
if (this.method == 'replace') {
|
|
|
|
wrapper.empty().append(new_content);
|
|
|
|
}
|
2007-10-05 09:35:09 +00:00
|
|
|
else {
|
2007-09-12 18:29:32 +00:00
|
|
|
wrapper[this.method](new_content);
|
|
|
|
}
|
2007-10-05 09:35:09 +00:00
|
|
|
|
2007-10-10 10:24:25 +00:00
|
|
|
// Immediately hide the new content if we're using any effects.
|
|
|
|
if (this.showEffect != 'show') {
|
|
|
|
new_content.hide();
|
|
|
|
}
|
|
|
|
|
2007-10-05 09:35:09 +00:00
|
|
|
// Determine what effect use and what content will receive the effect, then
|
|
|
|
// show the new content. For browser compatibility, Safari is excluded from
|
|
|
|
// using effects on table rows.
|
2007-10-10 10:24:25 +00:00
|
|
|
if (($.browser.safari && $("tr.ahah-new-content", new_content).size() > 0)) {
|
|
|
|
new_content.show();
|
|
|
|
}
|
|
|
|
else if ($('.ahah-new-content', new_content).size() > 0) {
|
2007-10-05 09:35:09 +00:00
|
|
|
$('.ahah-new-content', new_content).hide();
|
|
|
|
new_content.show();
|
|
|
|
$(".ahah-new-content", new_content)[this.showEffect](this.showSpeed);
|
|
|
|
}
|
|
|
|
else if (this.showEffect != 'show') {
|
|
|
|
new_content[this.showEffect](this.showSpeed);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach all javascript behaviors to the new content, if it was successfully
|
|
|
|
// added to the page, this if statement allows #ahah[wrapper] to be optional.
|
|
|
|
if (new_content.parents('html').length > 0) {
|
|
|
|
Drupal.attachBehaviors(new_content);
|
2007-07-04 15:42:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Drupal.unfreezeHeight();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the form redirection error.
|
|
|
|
*/
|
2008-01-04 11:53:21 +00:00
|
|
|
Drupal.ahah.prototype.error = function (response, uri) {
|
|
|
|
alert(Drupal.ahahError(response, uri));
|
2007-10-05 09:35:09 +00:00
|
|
|
// Resore the previous action and target to the form.
|
2008-01-04 11:53:21 +00:00
|
|
|
$(this.element).parent('form').attr( { action: this.form_action, target: this.form_target} );
|
2007-10-10 10:24:25 +00:00
|
|
|
// Remove the progress element.
|
|
|
|
if (this.progress.element) {
|
|
|
|
$(this.progress.element).remove();
|
|
|
|
}
|
|
|
|
if (this.progress.object) {
|
|
|
|
this.progress.object.stopMonitoring();
|
|
|
|
}
|
2007-07-04 15:42:38 +00:00
|
|
|
// Undo hide.
|
|
|
|
$(this.wrapper).show();
|
|
|
|
// Re-enable the element.
|
2007-10-05 09:35:09 +00:00
|
|
|
$(this.element).removeClass('progess-disabled').attr('disabled', false);
|
2007-07-04 15:42:38 +00:00
|
|
|
};
|