? drupal_ahah.js ? drupal_ahah.patch Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.656 diff -u -d -p -r1.656 common.inc --- includes/common.inc 18 Jun 2007 06:59:11 -0000 1.656 +++ includes/common.inc 25 Jun 2007 14:39:05 -0000 @@ -2844,6 +2844,9 @@ function drupal_common_themes() { 'form_element' => array( 'arguments' => array('element' => NULL, 'value' => NULL), ), + 'form_ahah' => array( + 'arguments' => array('element' => NULL), + ), ); } Index: includes/form.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/form.inc,v retrieving revision 1.206 diff -u -d -p -r1.206 form.inc --- includes/form.inc 17 Jun 2007 12:07:51 -0000 1.206 +++ includes/form.inc 25 Jun 2007 14:39:07 -0000 @@ -1471,7 +1471,13 @@ function theme_button($element) { $element['#attributes']['class'] = 'form-'. $element['#button_type']; } - return '\n"; + $output = '\n"; + + if (!empty($element['#ahah_path'])) { + $output .= theme('form_ahah', $element); + } + + return $output; } /** @@ -1674,6 +1680,10 @@ function theme_form_element($element, $v } } + if (!empty($element['#ahah_path'])) { + $output .= theme('form_ahah', $element); + } + $output .= " $value\n"; if (!empty($element['#description'])) { @@ -1685,6 +1695,38 @@ function theme_form_element($element, $v return $output; } + +/** + * Add AHAH information about a form element to the page to communicate with + * javascript. + * + * @param $element + * An associative array containing the properties of the element. + * Properties used: ahah_event, ahah_path, ahah_wrapper, ahah_parameters, + * ahah_effect. + * @return + * None. Additional code is added to the header of the page using + * drupal_add_js. + */ +function theme_form_ahah($element) { + drupal_add_js('misc/ahah.js'); + drupal_add_js('misc/progress.js'); + + $ahah_binding = array( + 'id' => $element['#id'], + 'uri' => url($element['#ahah_path']), + 'event' => $element['#ahah_event'], + 'effect' => empty($element['#ahah_effect']) ? 'none' : $element['#ahah_effect'], + 'method' => empty($element['#ahah_method']) ? 'replace' : $element['#ahah_method'], + ); + + if (!empty($element['#ahah_wrapper'])) { + $ahah_binding['wrapper'] = $element['#ahah_wrapper']; + } + + drupal_add_js(array('ahah' => array($element['#id'] => $ahah_binding)), 'setting'); +} + /** * Sets a form element's class attribute. * Index: misc/ahah.js =================================================================== RCS file: misc/ahah.js diff -N misc/ahah.js --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ misc/ahah.js 25 Jun 2007 14:39:07 -0000 @@ -0,0 +1,102 @@ +// $Id: $ + +/** + * Attaches the ahah behaviour to each ahah form element. + */ +Drupal.ahahAutoAttach = function() { + for (var base in Drupal.settings.ahah) { + var element = Drupal.settings.ahah[base]; + var ahah = new Drupal.ahah(base, element); + } +}; + +/** + * AHAH object. + */ +Drupal.ahah = function(base, element) { + // Set the properties for this object. + this.id = '#' + base; + this.event = element.event; + this.uri = element.uri; + this.wrapper = '#'+ element.wrapper; + this.effect = element.effect; + this.method = element.method; + if (this.effect == 'none') { + this.showEffect = 'show'; + this.hideEffect = 'hide'; + } + else if (this.effect == 'fade') { + this.showEffect = 'fadeIn'; + this.hideEffect = 'fadeOut'; + } + else { + this.showEffect = this.effect + 'Toggle'; + this.hideEffect = this.effect + 'Toggle'; + } + Drupal.redirectFormButton(this.uri, $(this.id).get(0), this); +}; + +/** + * Handler for the form redirection submission. + */ +Drupal.ahah.prototype.onsubmit = function () { + // Insert progressbar and stretch to take the same space. + this.progress = new Drupal.progressBar('ahah_progress'); + this.progress.setProgress(-1, Drupal.t('Please wait...')); + + var $wrapper = $(this.wrapper); + var $button = $(this.id); + var $progress_element = $(this.progress.element); + + $progress_element.css({ + display: 'none', + width: '10em', + margin: '0 0 0 20px', + float: 'left' + }); + $button.css('float', 'left').attr('disabled', true).after($progress_element); + $progress_element.eval(this.showEffect + "()"); +}; + +/** + * Handler for the form redirection completion. + */ +Drupal.ahah.prototype.oncomplete = function (data) { + var $wrapper = $(this.wrapper); + var $button = $(this.id); + var $progress_element = $(this.progress.element); + + Drupal.freezeHeight(); + + // Remove the progress element. + $progress_element.remove(); + + // Add the form and re-attach behavior. + if (this.method == 'replace') { + $wrapper.empty(); + this.method = 'append'; + } + $wrapper.eval(this.method + '(data)').eval(this.showEffect + '()'); + $button.css('float', 'none').attr('disabled', false); + + Drupal.ahahAutoAttach(); + Drupal.unfreezeHeight(); +}; + +/** + * Handler for the form redirection error. + */ +Drupal.ahah.prototype.onerror = function (error) { + alert(Drupal.t('An error occurred:\n\n@error', { '@error': error })); + // Remove progressbar + $(this.progress.element).remove(); + this.progress = null; + // Undo hide + $(this.wrapper).show(); +}; + + +// Global killswitch +if (Drupal.jsEnabled) { + $(document).ready(Drupal.ahahAutoAttach); +} Index: misc/upload.js =================================================================== RCS file: misc/upload.js diff -N misc/upload.js --- misc/upload.js 8 Jun 2007 12:51:59 -0000 1.13 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,116 +0,0 @@ -// $Id: upload.js,v 1.13 2007/06/08 12:51:59 goba Exp $ - -/** - * Attaches the upload behaviour to the upload form. - */ -Drupal.uploadAutoAttach = function() { - $('input.upload').each(function () { - var uri = this.value; - // Extract the base name from the id (edit-attach-url -> attach). - var base = this.id.substring(5, this.id.length - 4); - var button = base + '-button'; - var wrapper = base + '-wrapper'; - var hide = base + '-hide'; - var upload = new Drupal.jsUpload(uri, button, wrapper, hide); - }); -}; - -/** - * JS upload object. - */ -Drupal.jsUpload = function(uri, button, wrapper, hide) { - // Note: these elements are replaced after an upload, so we re-select them - // everytime they are needed. - this.button = '#'+ button; - this.wrapper = '#'+ wrapper; - this.hide = '#'+ hide; - Drupal.redirectFormButton(uri, $(this.button).get(0), this); -}; - -/** - * Handler for the form redirection submission. - */ -Drupal.jsUpload.prototype.onsubmit = function () { - // Insert progressbar and stretch to take the same space. - this.progress = new Drupal.progressBar('uploadprogress'); - this.progress.setProgress(-1, Drupal.t('Uploading file')); - - var hide = this.hide; - var el = this.progress.element; - var offset = $(hide).get(0).offsetHeight; - $(el).css({ - width: '28em', - height: offset +'px', - paddingTop: '10px', - display: 'none' - }); - $(hide).css('position', 'absolute'); - - $(hide).after(el); - $(el).fadeIn('slow'); - $(hide).fadeOut('slow'); -}; - -/** - * Handler for the form redirection completion. - */ -Drupal.jsUpload.prototype.oncomplete = function (data) { - // Remove old form - Drupal.freezeHeight(); // Avoid unnecessary scrolling - $(this.wrapper).html(''); - - // Place HTML into temporary div - var div = document.createElement('div'); - $(div).html(data); - - // If uploading the first attachment fade in everything - if ($('tr', div).size() == 2) { - // Replace form and re-attach behaviour - $(div).hide(); - $(this.wrapper).append(div); - $(div).fadeIn('slow'); - Drupal.uploadAutoAttach(); - } - // Else fade in only the last table row - else { - // Hide form and last table row - $('table tr:last-of-type td', div).hide(); - - // Note: workaround because jQuery's #id selector does not work outside of 'document' - // Should be: $(this.hide, div).hide(); - var hide = this.hide; - $('div', div).each(function() { - if (('#'+ this.id) == hide) { - this.style.display = 'none'; - } - }); - - // Replace form, fade in items and re-attach behaviour - $(this.wrapper).append(div); - $('table tr:last-of-type td', div).fadeIn('slow'); - $(this.hide, div).fadeIn('slow'); - Drupal.uploadAutoAttach(); - } - Drupal.unfreezeHeight(); -}; - -/** - * Handler for the form redirection error. - */ -Drupal.jsUpload.prototype.onerror = function (error) { - alert(Drupal.t('An error occurred:\n\n@error', { '@error': error })); - // Remove progressbar - $(this.progress.element).remove(); - this.progress = null; - // Undo hide - $(this.hide).css({ - position: 'static', - left: '0px' - }); -}; - - -// Global killswitch -if (Drupal.jsEnabled) { - $(document).ready(Drupal.uploadAutoAttach); -} Index: modules/system/system.module =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.module,v retrieving revision 1.493 diff -u -d -p -r1.493 system.module --- modules/system/system.module 17 Jun 2007 10:49:49 -0000 1.493 +++ modules/system/system.module 25 Jun 2007 14:39:07 -0000 @@ -98,9 +98,8 @@ function system_elements() { $type['form'] = array('#method' => 'post', '#action' => request_uri()); // Inputs - $type['checkbox'] = array('#input' => TRUE, '#return_value' => 1); - $type['submit'] = array('#input' => TRUE, '#name' => 'op', '#button_type' => 'submit', '#executes_submit_callback' => TRUE); - $type['button'] = array('#input' => TRUE, '#name' => 'op', '#button_type' => 'submit', '#executes_submit_callback' => FALSE); + $type['submit'] = array('#input' => TRUE, '#name' => 'op', '#button_type' => 'submit', '#executes_submit_callback' => TRUE, '#ahah_event' => 'submit'); + $type['button'] = array('#input' => TRUE, '#name' => 'op', '#button_type' => 'submit', '#executes_submit_callback' => FALSE, '#ahah_event' => 'submit'); $type['textfield'] = array('#input' => TRUE, '#size' => 60, '#maxlength' => 128, '#autocomplete_path' => FALSE); $type['password'] = array('#input' => TRUE, '#size' => 60, '#maxlength' => 128); $type['password_confirm'] = array('#input' => TRUE, '#process' => array('expand_password_confirm')); @@ -108,6 +107,7 @@ function system_elements() { $type['radios'] = array('#input' => TRUE, '#process' => array('expand_radios')); $type['radio'] = array('#input' => TRUE, '#default_value' => NULL); $type['checkboxes'] = array('#input' => TRUE, '#process' => array('expand_checkboxes'), '#tree' => TRUE); + $type['checkbox'] = array('#input' => TRUE, '#return_value' => 1); $type['select'] = array('#input' => TRUE, '#size' => 0, '#multiple' => FALSE); $type['weight'] = array('#input' => TRUE, '#delta' => 10, '#default_value' => 0, '#process' => array('process_weight')); $type['date'] = array('#input' => TRUE, '#process' => array('expand_date' => array()), '#element_validate' => array('date_validate')); @@ -360,6 +360,9 @@ function system_init() { // Add the CSS for this module. drupal_add_css(drupal_get_path('module', 'system') .'/defaults.css', 'module'); drupal_add_css(drupal_get_path('module', 'system') .'/system.css', 'module'); + + // Add the base path for Javascript. + drupal_add_js(array('system' => array('basePath' => base_path())), 'setting'); } /** Index: modules/upload/upload.module =================================================================== RCS file: /cvs/drupal/drupal/modules/upload/upload.module,v retrieving revision 1.168 diff -u -d -p -r1.168 upload.module --- modules/upload/upload.module 22 Jun 2007 08:46:15 -0000 1.168 +++ modules/upload/upload.module 25 Jun 2007 14:39:07 -0000 @@ -368,9 +368,6 @@ function upload_form_alter(&$form, $form if (isset($form['type']) && isset($form['#node'])) { $node = $form['#node']; if ($form['type']['#value'] .'_node_form' == $form_id && variable_get("upload_$node->type", TRUE)) { - drupal_add_js('misc/progress.js'); - drupal_add_js('misc/upload.js'); - // Attachments fieldset $form['attachments'] = array( '#type' => 'fieldset', @@ -384,7 +381,7 @@ function upload_form_alter(&$form, $form '#weight' => 30, ); - // Wrapper for fieldset contents (used by upload JS). + // Wrapper for fieldset contents (used by ahah.hs). $form['attachments']['wrapper'] = array( '#prefix' => '