summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/ajax.inc33
-rw-r--r--misc/ajax.js8
-rw-r--r--modules/simpletest/tests/ajax.test9
-rw-r--r--modules/simpletest/tests/ajax_forms_test.module19
4 files changed, 68 insertions, 1 deletions
diff --git a/includes/ajax.inc b/includes/ajax.inc
index 2fd7f6941..24824a63f 100644
--- a/includes/ajax.inc
+++ b/includes/ajax.inc
@@ -1002,7 +1002,7 @@ function ajax_command_changed($selector, $asterisk = '') {
* The 'css' command will instruct the client to use the jQuery css() method
* to apply the CSS arguments to elements matched by the given selector.
*
- * This command is implemented by Drupal.ajax.prototype.commands.insert()
+ * This command is implemented by Drupal.ajax.prototype.commands.css()
* defined in misc/ajax.js.
*
* @param $selector
@@ -1088,6 +1088,37 @@ function ajax_command_data($selector, $name, $value) {
}
/**
+ * Creates a Drupal AJAX 'invoke' command.
+ *
+ * The 'invoke' command will instruct the client to invoke the given jQuery
+ * method with the supplied arguments on the elements matched by the given
+ * selector. Intended for simple jQuery commands, such as attr(), addClass(),
+ * removeClass(), toggleClass(), etc.
+ *
+ * This command is implemented by Drupal.ajax.prototype.commands.invoke()
+ * defined in misc/ajax.js.
+ *
+ * @param $selector
+ * A jQuery selector string. If the command is a response to a request from
+ * an #ajax form element then this value can be NULL.
+ * @param $method
+ * The jQuery method to invoke.
+ * @param $arguments
+ * (optional) A list of arguments to the jQuery $method, if any.
+ *
+ * @return
+ * An array suitable for use with the ajax_render() function.
+ */
+function ajax_command_invoke($selector, $method, array $arguments = array()) {
+ return array(
+ 'command' => 'invoke',
+ 'selector' => $selector,
+ 'method' => $method,
+ 'arguments' => $arguments,
+ );
+}
+
+/**
* Creates a Drupal AJAX 'restripe' command.
*
* The 'restripe' command instructs the client to restripe a table. This is
diff --git a/misc/ajax.js b/misc/ajax.js
index 127dd07ae..088d46df7 100644
--- a/misc/ajax.js
+++ b/misc/ajax.js
@@ -544,6 +544,14 @@ Drupal.ajax.prototype.commands = {
},
/**
+ * Command to apply a jQuery method.
+ */
+ invoke: function (ajax, response, status) {
+ var $element = $(response.selector);
+ $element[response.method].apply($element, response.arguments);
+ },
+
+ /**
* Command to restripe a table.
*/
restripe: function (ajax, response, status) {
diff --git a/modules/simpletest/tests/ajax.test b/modules/simpletest/tests/ajax.test
index 2788d4f90..2a2469334 100644
--- a/modules/simpletest/tests/ajax.test
+++ b/modules/simpletest/tests/ajax.test
@@ -211,6 +211,15 @@ class AJAXCommandsTestCase extends AJAXTestCase {
);
$this->assertCommand($commands, $expected, "'data' AJAX command issued with correct key and value");
+ // Tests the 'invoke' command.
+ $commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX invoke command: Invoke addClass() method.")));
+ $expected = array(
+ 'command' => 'invoke',
+ 'method' => 'addClass',
+ 'arguments' => array('error'),
+ );
+ $this->assertCommand($commands, $expected, "'invoke' AJAX command issued with correct method and argument");
+
// Tests the 'html' command.
$commands = $this->drupalPostAJAX($form_path, $edit, array('op' => t("AJAX html: Replace the HTML in a selector.")));
$expected = array(
diff --git a/modules/simpletest/tests/ajax_forms_test.module b/modules/simpletest/tests/ajax_forms_test.module
index 531c703fd..21784dfa3 100644
--- a/modules/simpletest/tests/ajax_forms_test.module
+++ b/modules/simpletest/tests/ajax_forms_test.module
@@ -176,6 +176,16 @@ function ajax_forms_test_ajax_commands_form($form, &$form_state) {
'#suffix' => '<div id="data_div">Data attached to this div.</div>',
);
+ // Shows the AJAX 'invoke' command.
+ $form['invoke_command_example'] = array(
+ '#value' => t("AJAX invoke command: Invoke addClass() method."),
+ '#type' => 'submit',
+ '#ajax' => array(
+ 'callback' => 'ajax_forms_test_advanced_commands_invoke_callback',
+ ),
+ '#suffix' => '<div id="invoke_div">Original contents</div>',
+ );
+
// Shows the AJAX 'html' command.
$form['html_command_example'] = array(
'#value' => t("AJAX html: Replace the HTML in a selector."),
@@ -332,6 +342,15 @@ function ajax_forms_test_advanced_commands_data_callback($form, $form_state) {
}
/**
+ * AJAX callback for 'invoke'.
+ */
+function ajax_forms_test_advanced_commands_invoke_callback($form, $form_state) {
+ $commands = array();
+ $commands[] = ajax_command_invoke('#invoke_div', 'addClass', array('error'));
+ return array('#type' => 'ajax', '#commands' => $commands);
+}
+
+/**
* AJAX callback for 'html'.
*/
function ajax_forms_test_advanced_commands_html_callback($form, $form_state) {