Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / includes / ajax.inc @ 96a203dd

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

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

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

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

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

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