ajax.inc 4.7 KB

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