ajax.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * Set a single property to a value, on all matched elements.
  49. *
  50. * @param $selector
  51. * The CSS selector. This can be any selector jquery uses in $().
  52. * @param $name
  53. * The name or key: of the data attached to this selector.
  54. * @param $value
  55. * The value of the data.
  56. */
  57. function ctools_ajax_command_attr($selector, $name, $value) {
  58. ctools_add_js('ajax-responder');
  59. return array(
  60. 'command' => 'attr',
  61. 'selector' => $selector,
  62. 'name' => $name,
  63. 'value' => $value,
  64. );
  65. }
  66. /**
  67. * Force a client-side redirect.
  68. *
  69. * @param $url
  70. * The url to be redirected to. This can be an absolute URL or a
  71. * Drupal path.
  72. * @param $delay
  73. * A delay before applying the redirection, in milliseconds.
  74. * @param $options
  75. * An array of options to pass to the url() function.
  76. */
  77. function ctools_ajax_command_redirect($url, $delay = 0, $options = array()) {
  78. ctools_add_js('ajax-responder');
  79. return array(
  80. 'command' => 'redirect',
  81. 'url' => url($url, $options),
  82. 'delay' => $delay,
  83. );
  84. }
  85. /**
  86. * Force a reload of the current page.
  87. */
  88. function ctools_ajax_command_reload() {
  89. ctools_add_js('ajax-responder');
  90. return array(
  91. 'command' => 'reload',
  92. );
  93. }
  94. /**
  95. * Submit a form.
  96. *
  97. * This is useful for submitting a parent form after a child form has finished
  98. * processing in a modal overlay.
  99. *
  100. * @param $selector
  101. * The CSS selector to identify the form for submission. This can be any
  102. * selector jquery uses in $().
  103. */
  104. function ctools_ajax_command_submit($selector) {
  105. ctools_add_js('ajax-responder');
  106. return array(
  107. 'command' => 'submit',
  108. 'selector' => $selector,
  109. );
  110. }
  111. /**
  112. * Send an error response back via AJAX and immediately exit.
  113. */
  114. function ctools_ajax_render_error($error = '') {
  115. $commands = array();
  116. $commands[] = ajax_command_alert($error);
  117. print ajax_render($commands);
  118. exit;
  119. }