function form_expand_ahah

6.x form.inc form_expand_ahah($element)

Add AHAH information about a form element to the page to communicate with javascript. If #ahah[path] is set on an element, this additional javascript is added to the page header to attach the AHAH behaviors. See ahah.js for more information.

Parameters

$element: An associative array containing the properties of the element. Properties used: ahah_event, ahah_path, ahah_wrapper, ahah_parameters, ahah_effect.

Return value

None. Additional code is added to the header of the page using drupal_add_js.

Related topics

1 string reference to 'form_expand_ahah'
system_elements in drupal-6.x/modules/system/system.module
Implementation of hook_elements().

File

drupal-6.x/includes/form.inc, line 1800

Code

function form_expand_ahah($element) {
  static $js_added = array();
  // Add a reasonable default event handler if none specified.
  if (isset($element['#ahah']['path']) && !isset($element['#ahah']['event'])) {
    switch ($element['#type']) {
      case 'submit':
      case 'button':
      case 'image_button':
        // Use the mousedown instead of the click event because form
        // submission via pressing the enter key triggers a click event on
        // submit inputs, inappropriately triggering AHAH behaviors.
        $element['#ahah']['event'] = 'mousedown';
        // Attach an additional event handler so that AHAH behaviours
        // can be triggered still via keyboard input.
        $element['#ahah']['keypress'] = TRUE;
        break;
      case 'password':
      case 'textfield':
      case 'textarea':
        $element['#ahah']['event'] = 'blur';
        break;
      case 'radio':
      case 'checkbox':
      case 'select':
        $element['#ahah']['event'] = 'change';
        break;
    }
  }

  // Adding the same javascript settings twice will cause a recursion error,
  // we avoid the problem by checking if the javascript has already been added.
  if (isset($element['#ahah']['path']) && isset($element['#ahah']['event']) && !isset($js_added[$element['#id']])) {
    drupal_add_js('misc/jquery.form.js');
    drupal_add_js('misc/ahah.js');

    $ahah_binding = array(
      'url' => url($element['#ahah']['path']),
      'event' => $element['#ahah']['event'],
      'keypress' => empty($element['#ahah']['keypress']) ? NULL : $element['#ahah']['keypress'],
      'wrapper' => empty($element['#ahah']['wrapper']) ? NULL : $element['#ahah']['wrapper'],
      'selector' => empty($element['#ahah']['selector']) ? '#' . $element['#id'] : $element['#ahah']['selector'],
      'effect' => empty($element['#ahah']['effect']) ? 'none' : $element['#ahah']['effect'],
      'method' => empty($element['#ahah']['method']) ? 'replace' : $element['#ahah']['method'],
      'progress' => empty($element['#ahah']['progress']) ? array('type' => 'throbber') : $element['#ahah']['progress'],
      'button' => isset($element['#executes_submit_callback']) ? array($element['#name'] => $element['#value']) : FALSE,
    );

    // Convert a simple #ahah[progress] type string into an array.
    if (is_string($ahah_binding['progress'])) {
      $ahah_binding['progress'] = array('type' => $ahah_binding['progress']);
    }
    // Change progress path to a full URL.
    if (isset($ahah_binding['progress']['path'])) {
      $ahah_binding['progress']['url'] = url($ahah_binding['progress']['path']);
    }

    // Add progress.js if we're doing a bar display.
    if ($ahah_binding['progress']['type'] == 'bar') {
      drupal_add_js('misc/progress.js');
    }

    drupal_add_js(array('ahah' => array($element['#id'] => $ahah_binding)), 'setting');

    $js_added[$element['#id']] = TRUE;
    $element['#cache'] = TRUE;
  }
  return $element;
}