Index: css/ctools.css =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/ctools/css/ctools.css,v retrieving revision 1.3 diff -u -r1.3 ctools.css --- css/ctools.css 22 Apr 2009 22:31:28 -0000 1.3 +++ css/ctools.css 13 Jul 2009 14:29:49 -0000 @@ -12,7 +12,8 @@ } a.ctools-ajaxing, -input.ctools-ajaxing { +input.ctools-ajaxing, +select.ctools-ajaxing { padding-right: 18px !important; background: url(../images/status-active.gif) right center no-repeat; } Index: includes/ajax.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/ctools/includes/ajax.inc,v retrieving revision 1.9 diff -u -r1.9 ajax.inc --- includes/ajax.inc 9 Jul 2009 00:07:04 -0000 1.9 +++ includes/ajax.inc 13 Jul 2009 14:29:49 -0000 @@ -363,3 +363,61 @@ ctools_ajax_render($commands); } +/** + * Modify a form element of #type select to take ajax actions. + * + * This is a helper function to easily populate a select form element + * which can be used for on select change ajax functionality. By assigning + * a destination url to a select form element the selected value will be + * sent to the url via ajax when the element's onchange event fires. + * + * You would call this function on a select form element in the form + * function like this: + * + * @code + * $form['example'] = array( + * '#title' => t('Example'), + * '#type' => 'select', + * '#options' => array(1 => 'One', 2 => 'Two', 3 => 'Three'), + * '#default_value' => 1, + * ); + * ctools_ajax_select_change_element($form, 'example', 'example/ajax/urlpath'); + * @endcode + * + * @param &$form + * Reference to the form element. This is required to have the #id and #attribute + * elements populated and to create the hidden form element for each select. + * @param $element_key + * The array key of the select box we are going to take action on. + * @param $dest + * The base URL that will be called with ajax when a select is changed. The call will + * be in the form of $dest/{$select_value} + * @param $element_id + * The ID to use for your select box and the hidden field. + * @param $type + * A type to use, in case a different behavior should be attached. Defaults + * to ctools-use-ajax. + */ +function ctools_ajax_select_change_element(&$form, $element_key, $dest, $element_id = NULL, $type = 'ctools-use-ajax') { + //Add ID to the form element + if ($element_id === NULL) { + //use element name as id if one wasnt specified + $element_id = $element_key; + } + $form[$element_key]['#id'] = $element_id; + + //Add ctools-use-ajax to the class + if (!isset($form[$element_key]['#attributes']) || !array($form[$element_key]['#attributes'])) { + $form[$element_key]['#attributes'] = array(); + } + $class = $form[$element_key]['#attributes']['class'] ? $form[$element_key]['#attributes']['class'] : ''; + $classes = is_array($class) ? implode(' ', $class) : $class; + $form[$element_key]['#attributes']['class'] = "$classes $type"; + + //Add hidden form element to hold base URL + $form["$element_id-url"] = array( + '#type' => 'hidden', + '#value' => $dest, + '#attributes' => array('class' => "$element_id-url"), + ); +} Index: js/ajax-responder.js =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/ctools/js/ajax-responder.js,v retrieving revision 1.10 diff -u -r1.10 ajax-responder.js --- js/ajax-responder.js 11 Jul 2009 18:11:36 -0000 1.10 +++ js/ajax-responder.js 13 Jul 2009 14:29:49 -0000 @@ -1,4 +1,5 @@ // $Id: ajax-responder.js,v 1.10 2009/07/11 18:11:36 merlinofchaos Exp $ + /** * @file * @@ -229,6 +230,47 @@ }; /** + * Generic replacement for drop down select change handler to execute ajax method. + */ +Drupal.CTools.AJAX.changeAJAXSelect = function () { + if ($(this).hasClass('ctools-ajaxing')) { + return false; + } + + var url = Drupal.CTools.AJAX.findURL(this) + "/" + $(this).find('option:selected').attr('value'); + $(this).addClass('ctools-ajaxing'); + var object = $(this); + try { + if (url) { + url.replace('/nojs/', '/ajax/'); + $.ajax({ + type: "POST", + url: url, + data: '', + global: true, + success: Drupal.CTools.AJAX.respond, + error: function() { + alert("An error occurred while attempting to process " + url); + }, + complete: function() { + object.removeClass('ctools-ajaxing'); + }, + dataType: 'json' + }); + } + else { + return false; + } + } + catch (err) { + alert("An error occurred while attempting to process " + url); + $(this).removeClass('ctools-ajaxing'); + return false; + } + return false; +}; + +/** * Bind links that will open modals to the appropriate function. */ Drupal.behaviors.CToolsAJAX = function(context) { @@ -241,4 +283,9 @@ $('input.ctools-use-ajax:not(.ctools-use-ajax-processed)', context) .addClass('ctools-use-ajax-processed') .click(Drupal.CTools.AJAX.clickAJAXButton); + + // Bind select + $('select.ctools-use-ajax:not(.ctools-use-ajax-processed)', context) + .addClass('ctools-use-ajax-processed') + .change(Drupal.CTools.AJAX.changeAJAXSelect); };