Projet

Général

Profil

Paste
Télécharger (4,68 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / includes / ajax.inc @ 7e72b748

1
<?php
2

    
3
/**
4
 * @file
5
 * Set this so we can tell that the file has been included at some point.
6
 */
7

    
8
define('CTOOLS_AJAX_INCLUDED', 1);
9

    
10
/**
11
 * @file
12
 * Extend core AJAX with some of our own stuff.
13
 */
14

    
15
/**
16
 * Render an image as a button link. This will automatically apply an AJAX class
17
 * to the link and add the appropriate javascript to make this happen.
18
 *
19
 * @param $image
20
 *   The path to an image to use that will be sent to theme('image') for rendering.
21
 * @param $dest
22
 *   The destination of the link.
23
 * @param $alt
24
 *   The alt text of the link.
25
 * @param $class
26
 *   Any class to apply to the link. @todo this should be a options array.
27
 */
28
function ctools_ajax_image_button($image, $dest, $alt, $class = '') {
29
  return ctools_ajax_text_button(theme('image', array('path' => $image)), $dest, $alt, $class);
30
}
31

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

    
56
/**
57
 * Render an icon and related text as a link. This will automatically apply an AJAX class
58
 * to the link and add the appropriate javascript to make this happen.
59
 *
60
 * Note: 'html' => true so be sure any text is vetted! Chances are these kinds of buttons will
61
 * not use user input so this is a very minor concern.
62
 *
63
 * @param $text
64
 *   The text that will be displayed as the link.
65
 * @param $image
66
 *   The icon image to include in the link.
67
 * @param $dest
68
 *   The destination of the link.
69
 * @param $alt
70
 *   The title text of the link.
71
 * @param $class
72
 *   Any class to apply to the link. @todo this should be a options array.
73
 * @param $type
74
 *   A type to use, in case a different behavior should be attached. Defaults
75
 *   to ctools-use-ajax.
76
 */
77
function ctools_ajax_icon_text_button($text, $image, $dest, $alt, $class = '', $type = 'use-ajax') {
78
  drupal_add_library('system', 'drupal.ajax');
79
  $rendered_image = theme('image', array('path' => $image));
80
  $link_content = $rendered_image . "<span>" . $text . "</span>";
81
  return l($link_content, $dest, array('html' => TRUE, 'attributes' => array('class' => array($type, $class), 'title' => $alt)));
82
}
83

    
84
/**
85
 * Set a single property to a value, on all matched elements.
86
 *
87
 * @param $selector
88
 *   The CSS selector. This can be any selector jquery uses in $().
89
 * @param $name
90
 *   The name or key: of the data attached to this selector.
91
 * @param $value
92
 *   The value of the data.
93
 */
94
function ctools_ajax_command_attr($selector, $name, $value) {
95
  ctools_add_js('ajax-responder');
96
  return array(
97
    'command' => 'attr',
98
    'selector' => $selector,
99
    'name' => $name,
100
    'value' => $value,
101
  );
102
}
103

    
104
/**
105
 * Force a client-side redirect.
106
 *
107
 * @param $url
108
 *   The url to be redirected to. This can be an absolute URL or a
109
 *   Drupal path.
110
 * @param $delay
111
 *   A delay before applying the redirection, in milliseconds.
112
 * @param $options
113
 *   An array of options to pass to the url() function.
114
 */
115
function ctools_ajax_command_redirect($url, $delay = 0, $options = array()) {
116
  ctools_add_js('ajax-responder');
117
  return array(
118
    'command' => 'redirect',
119
    'url' => url($url, $options),
120
    'delay' => $delay,
121
  );
122
}
123

    
124
/**
125
 * Force a reload of the current page.
126
 */
127
function ctools_ajax_command_reload() {
128
  ctools_add_js('ajax-responder');
129
  return array(
130
    'command' => 'reload',
131
  );
132
}
133

    
134
/**
135
 * Submit a form.
136
 *
137
 * This is useful for submitting a parent form after a child form has finished
138
 * processing in a modal overlay.
139
 *
140
 * @param $selector
141
 *   The CSS selector to identify the form for submission. This can be any
142
 *   selector jquery uses in $().
143
 */
144
function ctools_ajax_command_submit($selector) {
145
  ctools_add_js('ajax-responder');
146
  return array(
147
    'command' => 'submit',
148
    'selector' => $selector,
149
  );
150
}
151

    
152
/**
153
 * Send an error response back via AJAX and immediately exit.
154
 */
155
function ctools_ajax_render_error($error = '') {
156
  $commands = array();
157
  $commands[] = ajax_command_alert($error);
158
  print ajax_render($commands);
159
  exit;
160
}