Projet

Général

Profil

Paste
Télécharger (3,53 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / ctools / includes / ajax.inc @ a5572547

1
<?php
2

    
3
// Set this so we can tell that the file has been included at some point.
4
define('CTOOLS_AJAX_INCLUDED', 1);
5

    
6
/**
7
 * @file
8
 * Extend core AJAX with some of our own stuff.
9
 */
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
/**
29
 * Render text as a link. This will automatically apply an AJAX class
30
 * to the link and add the appropriate javascript to make this happen.
31
 *
32
 * Note: 'html' => true so be sure any text is vetted! Chances are these kinds of buttons will
33
 * not use user input so this is a very minor concern.
34
 *
35
 * @param $text
36
 *   The text that will be displayed as the link.
37
 * @param $dest
38
 *   The destination of the link.
39
 * @param $alt
40
 *   The alt text of the link.
41
 * @param $class
42
 *   Any class to apply to the link. @todo this should be a options array.
43
 * @param $type
44
 *   A type to use, in case a different behavior should be attached. Defaults
45
 *   to ctools-use-ajax.
46
 */
47
function ctools_ajax_text_button($text, $dest, $alt, $class = '', $type = 'use-ajax') {
48
  drupal_add_library('system', 'drupal.ajax');
49
  return l($text, $dest, array('html' => TRUE, 'attributes' => array('class' => array($type, $class), 'title' => $alt)));
50
}
51

    
52
/**
53
 * Set a single property to a value, on all matched elements.
54
 *
55
 * @param $selector
56
 *   The CSS selector. This can be any selector jquery uses in $().
57
 * @param $name
58
 *   The name or key: of the data attached to this selector.
59
 * @param $value
60
 *  The value of the data.
61
 */
62
function ctools_ajax_command_attr($selector, $name, $value) {
63
  ctools_add_js('ajax-responder');
64
  return array(
65
     'command' => 'attr',
66
     'selector' => $selector,
67
     'name' => $name,
68
     'value' => $value,
69
   );
70
 }
71

    
72
/**
73
 * Force a client-side redirect.
74
 *
75
 * @param $url
76
 *   The url to be redirected to. This can be an absolute URL or a
77
 *   Drupal path.
78
 * @param $delay
79
 *   A delay before applying the redirection, in milliseconds.
80
 * @param $options
81
 *   An array of options to pass to the url() function.
82
 */
83
function ctools_ajax_command_redirect($url, $delay = 0, $options = array()) {
84
  ctools_add_js('ajax-responder');
85
  return array(
86
    'command' => 'redirect',
87
    'url' => url($url, $options),
88
    'delay' => $delay,
89
  );
90
}
91

    
92
/**
93
 * Force a reload of the current page.
94
 */
95
function ctools_ajax_command_reload() {
96
  ctools_add_js('ajax-responder');
97
  return array(
98
    'command' => 'reload',
99
  );
100
}
101

    
102
/**
103
 * Submit a form.
104
 *
105
 * This is useful for submitting a parent form after a child form has finished
106
 * processing in a modal overlay.
107
 *
108
 * @param $selector
109
 *   The CSS selector to identify the form for submission. This can be any
110
 *   selector jquery uses in $().
111
 */
112
function ctools_ajax_command_submit($selector) {
113
  ctools_add_js('ajax-responder');
114
  return array(
115
    'command' => 'submit',
116
    'selector' => $selector,
117
  );
118
}
119

    
120
/**
121
 * Send an error response back via AJAX and immediately exit.
122
 */
123
function ctools_ajax_render_error($error = '') {
124
  $commands = array();
125
  $commands[] = ajax_command_alert($error);
126
  print ajax_render($commands);
127
  exit;
128
}
129