annotate sites/all/modules/ctools/includes/ajax.inc @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
rev   line source
danielebarchiesi@0 1 <?php
danielebarchiesi@0 2
danielebarchiesi@0 3 // Set this so we can tell that the file has been included at some point.
danielebarchiesi@0 4 define('CTOOLS_AJAX_INCLUDED', 1);
danielebarchiesi@0 5
danielebarchiesi@0 6 /**
danielebarchiesi@0 7 * @file
danielebarchiesi@0 8 * Extend core AJAX with some of our own stuff.
danielebarchiesi@0 9 */
danielebarchiesi@0 10
danielebarchiesi@0 11 /**
danielebarchiesi@0 12 * Render an image as a button link. This will automatically apply an AJAX class
danielebarchiesi@0 13 * to the link and add the appropriate javascript to make this happen.
danielebarchiesi@0 14 *
danielebarchiesi@0 15 * @param $image
danielebarchiesi@0 16 * The path to an image to use that will be sent to theme('image') for rendering.
danielebarchiesi@0 17 * @param $dest
danielebarchiesi@0 18 * The destination of the link.
danielebarchiesi@0 19 * @param $alt
danielebarchiesi@0 20 * The alt text of the link.
danielebarchiesi@0 21 * @param $class
danielebarchiesi@0 22 * Any class to apply to the link. @todo this should be a options array.
danielebarchiesi@0 23 */
danielebarchiesi@0 24 function ctools_ajax_image_button($image, $dest, $alt, $class = '') {
danielebarchiesi@0 25 return ctools_ajax_text_button(theme('image', array('path' => $image)), $dest, $alt, $class);
danielebarchiesi@0 26 }
danielebarchiesi@0 27
danielebarchiesi@0 28 /**
danielebarchiesi@0 29 * Render text as a link. This will automatically apply an AJAX class
danielebarchiesi@0 30 * to the link and add the appropriate javascript to make this happen.
danielebarchiesi@0 31 *
danielebarchiesi@0 32 * Note: 'html' => true so be sure any text is vetted! Chances are these kinds of buttons will
danielebarchiesi@0 33 * not use user input so this is a very minor concern.
danielebarchiesi@0 34 *
danielebarchiesi@0 35 * @param $text
danielebarchiesi@0 36 * The text that will be displayed as the link.
danielebarchiesi@0 37 * @param $dest
danielebarchiesi@0 38 * The destination of the link.
danielebarchiesi@0 39 * @param $alt
danielebarchiesi@0 40 * The alt text of the link.
danielebarchiesi@0 41 * @param $class
danielebarchiesi@0 42 * Any class to apply to the link. @todo this should be a options array.
danielebarchiesi@0 43 * @param $type
danielebarchiesi@0 44 * A type to use, in case a different behavior should be attached. Defaults
danielebarchiesi@0 45 * to ctools-use-ajax.
danielebarchiesi@0 46 */
danielebarchiesi@0 47 function ctools_ajax_text_button($text, $dest, $alt, $class = '', $type = 'use-ajax') {
danielebarchiesi@0 48 drupal_add_library('system', 'drupal.ajax');
danielebarchiesi@0 49 return l($text, $dest, array('html' => TRUE, 'attributes' => array('class' => array($type, $class), 'title' => $alt)));
danielebarchiesi@0 50 }
danielebarchiesi@0 51
danielebarchiesi@0 52 /**
danielebarchiesi@0 53 * Set a single property to a value, on all matched elements.
danielebarchiesi@0 54 *
danielebarchiesi@0 55 * @param $selector
danielebarchiesi@0 56 * The CSS selector. This can be any selector jquery uses in $().
danielebarchiesi@0 57 * @param $name
danielebarchiesi@0 58 * The name or key: of the data attached to this selector.
danielebarchiesi@0 59 * @param $value
danielebarchiesi@0 60 * The value of the data.
danielebarchiesi@0 61 */
danielebarchiesi@0 62 function ctools_ajax_command_attr($selector, $name, $value) {
danielebarchiesi@0 63 ctools_add_js('ajax-responder');
danielebarchiesi@0 64 return array(
danielebarchiesi@0 65 'command' => 'attr',
danielebarchiesi@0 66 'selector' => $selector,
danielebarchiesi@0 67 'name' => $name,
danielebarchiesi@0 68 'value' => $value,
danielebarchiesi@0 69 );
danielebarchiesi@0 70 }
danielebarchiesi@0 71
danielebarchiesi@0 72 /**
danielebarchiesi@0 73 * Force a client-side redirect.
danielebarchiesi@0 74 *
danielebarchiesi@0 75 * @param $url
danielebarchiesi@0 76 * The url to be redirected to. This can be an absolute URL or a
danielebarchiesi@0 77 * Drupal path.
danielebarchiesi@0 78 * @param $delay
danielebarchiesi@0 79 * A delay before applying the redirection, in milliseconds.
danielebarchiesi@0 80 * @param $options
danielebarchiesi@0 81 * An array of options to pass to the url() function.
danielebarchiesi@0 82 */
danielebarchiesi@0 83 function ctools_ajax_command_redirect($url, $delay = 0, $options = array()) {
danielebarchiesi@0 84 ctools_add_js('ajax-responder');
danielebarchiesi@0 85 return array(
danielebarchiesi@0 86 'command' => 'redirect',
danielebarchiesi@0 87 'url' => url($url, $options),
danielebarchiesi@0 88 'delay' => $delay,
danielebarchiesi@0 89 );
danielebarchiesi@0 90 }
danielebarchiesi@0 91
danielebarchiesi@0 92 /**
danielebarchiesi@0 93 * Force a reload of the current page.
danielebarchiesi@0 94 */
danielebarchiesi@0 95 function ctools_ajax_command_reload() {
danielebarchiesi@0 96 ctools_add_js('ajax-responder');
danielebarchiesi@0 97 return array(
danielebarchiesi@0 98 'command' => 'reload',
danielebarchiesi@0 99 );
danielebarchiesi@0 100 }
danielebarchiesi@0 101
danielebarchiesi@0 102 /**
danielebarchiesi@0 103 * Submit a form.
danielebarchiesi@0 104 *
danielebarchiesi@0 105 * This is useful for submitting a parent form after a child form has finished
danielebarchiesi@0 106 * processing in a modal overlay.
danielebarchiesi@0 107 *
danielebarchiesi@0 108 * @param $selector
danielebarchiesi@0 109 * The CSS selector to identify the form for submission. This can be any
danielebarchiesi@0 110 * selector jquery uses in $().
danielebarchiesi@0 111 */
danielebarchiesi@0 112 function ctools_ajax_command_submit($selector) {
danielebarchiesi@0 113 ctools_add_js('ajax-responder');
danielebarchiesi@0 114 return array(
danielebarchiesi@0 115 'command' => 'submit',
danielebarchiesi@0 116 'selector' => $selector,
danielebarchiesi@0 117 );
danielebarchiesi@0 118 }
danielebarchiesi@0 119
danielebarchiesi@0 120 /**
danielebarchiesi@0 121 * Send an error response back via AJAX and immediately exit.
danielebarchiesi@0 122 */
danielebarchiesi@0 123 function ctools_ajax_render_error($error = '') {
danielebarchiesi@0 124 $commands = array();
danielebarchiesi@0 125 $commands[] = ajax_command_alert($error);
danielebarchiesi@0 126 print ajax_render($commands);
danielebarchiesi@0 127 exit;
danielebarchiesi@0 128 }
danielebarchiesi@0 129