ajax.inc 4.7 KB

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