Projet

Général

Profil

Paste
Télécharger (13,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / help / modal.html @ ba09eb79

1
<p>CTools provides a simple modal that can be used as a popup to place forms. It differs from the normal modal frameworks in that it does not do its work via an iframe. This is both an advantage and a disadvantage. The iframe simply renders normal pages in a sub-browser and they can do their thing. That makes it much easier to put arbitrary pages and forms in a modal. However, the iframe is not very good at actually communicating changes to the main page, so you cannot open the modal, have it do some work, and then modify the page. </p>
2

    
3
<h2>Invoking the modal</h2>
4

    
5
<p>The basic form of the modal can be set up just by including the javascript and adding the proper class to a link or form that will open the modal. To include the proper javascript, simply include the library and call the add_js function:</p>
6

    
7
<code>ctools_include('modal');
8
ctools_modal_add_js();
9
</code>
10

    
11
<p>You can have links and buttons bound to use the modal by adding the class ctools-use-modal. For convenience, there is a helper function to try and do this, though it's not great at doing all links so using this is optional:</p>
12

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

    
30
/**
31
 * Render text as a link. This will automatically apply an AJAX class
32
 * to the link and add the appropriate javascript to make this happen.
33
 *
34
 * Note: 'html' => true so be sure any text is vetted! Chances are these kinds of buttons will
35
 * not use user input so this is a very minor concern.
36
 *
37
 * @param $text
38
 *   The text to display as the link.
39
 * @param $dest
40
 *   The destination of the link.
41
 * @param $alt
42
 *   The alt text of the link.
43
 * @param $class
44
 *   Any class to apply to the link. @todo this should be a options array.
45
 */
46
function ctools_modal_text_button($text, $dest, $alt, $class = '') {
47
  return ctools_ajax_text_button($text, $dest, $alt, $class, 'ctools-use-modal');
48
}
49
</code>
50

    
51
<p>Like with all CTools' AJAX functionality, the href of the link will be the destination, with any appearance of /nojs/ converted to /ajax/.</p>
52

    
53
<p>For submit buttons, however, the URL may be found a different, slightly more complex way. If you do not wish to simply submit the form to the modal, you can create a URL using hidden form fields. The ID of the item is taken and -url is appended to it to derive a class name. Then, all form elements that contain that class name are founded and their values put together to form a URL.</p>
54

    
55
<p>For example, let's say you have an 'add' button, and it has a select form item that tells your system what widget it is adding. If the id of the add button is edit-add, you would place a hidden input with the base of your URL in the form and give it a class of 'edit-add-url'. You would then add 'edit-add-url' as a class to the select widget allowing you to convert this value to the form without posting. If no URL is found, the action of the form will be used and the entire form posted to it.</p>
56

    
57
<h2>Customizing the modal</h2>
58

    
59
<p>If you do not wish to use the default modal, the modal can be customized by creating an array of data to define a customized modal. To do this, you add an array of info to the javascript settings to define the customizations for the modal and add an additional class to your modal link or button to tell it which modal to use.</p>
60

    
61
<p>First, you need to create a settings array. You can do this most easily with a bit of PHP:</p>
62

    
63
<pre>drupal_add_js(array(
64
    'my-modal-style' => array(
65
      'modalSize' => array(
66
        'type' => 'fixed',
67
        'width' => 250,
68
        'height' => 250,
69
      ),
70
    ),
71
  ), 'setting');
72
</pre>
73

    
74
<p>The key to the array above (in this case, my-modal-style) is the identifier to your modal theme. You can have multiple modal themes on a page, so be sure to use an ID that will not collide with some other module's use. Using your module or theme as a prefix is a good idea.</p>
75

    
76
<p>Then, when adding the ctools-use-modal class to your link or button, also add the following class: ctools-modal-ID (in the example case, that would be ctools-modal-my-modal-style).</p>
77

    
78
<p>modalSize can be 'fixed' or 'scale'. If fixed it will be a raw pixel value; if 'scale' it will be a percentage of the screen.</p>
79

    
80
<p>You can set:</p>
81
  <ul>
82
    <li> <strong>modalSize</strong>: an array of data to control the sizing of the modal. It can contain:
83
  <ul>
84
  <li> <strong>type</strong>: Either <em>fixed</em> or <em>scale</em>. If fixed, the modal will always be a fixed size. If <em>scale</em> the modal will scale to a percentage of the browser window. <em>Default: scale</em>.
85
<li> <strong>width</strong>: If </em>fixed</em> the width in pixels. If <em>scale</em> the percentage of the screen expressed as a number less than zero. (For 80 percent, use .8, for example). <em>Default: .8</em></li>
86
<li> <strong>height</strong>: If <em>fixed</em> the height in pixels. If <em>scale</em> the percentage of the screen expressed as a number less than zero. (For 80 percent, use .8, for example). <em>Default: .8</em></li>
87
<li> <strong>addWidth</strong>: Any additional width to add to the modal in pixels. Only useful if the type is scale. <em>Default: 0</em></li>
88
<li> <strong>addHeight</strong>: Any additional height to add to the modal in pixels. Only useful if the type is scale. <em>Default: 0</em></li>
89
<li> <strong>contentRight</strong>: The number of pixels to remove from the content inside the modal to make room for scroll bar and decorations. <em>Default: 25</em></li>
90
<li> <strong>contentBottom</strong>: The number of pixels to remove from the content inside the modal to make room for scroll bar and decorations. <em>Default: 45</em></li>
91
</ul>
92
</li>
93
<li> <strong>modalTheme</strong>: The Drupal javascript themable function which controls how the modal will be rendered. This function must be in the <em>Drupal.theme.prototype</em> namespace. If you set this value, you must include a corresponding function in a javascript file and use drupal_add_js() to add that file. <em>Default: CToolsModalDialog</em>
94
<pre>
95
  Drupal.theme.prototype.CToolsModalDialog = function () {
96
    var html = ''
97
    html += '  &lt;div id="ctools-modal"&gt;'
98
    html += '    &lt;div class="ctools-modal-content"&gt;' // panels-modal-content
99
    html += '      &lt;div class="modal-header"&gt;';
100
    html += '        &lt;a class="close" href="#"&gt;';
101
    html +=            Drupal.CTools.Modal.currentSettings.closeText + Drupal.CTools.Modal.currentSettings.closeImage;
102
    html += '        &lt;/a&gt;';
103
    html += '        &lt;span id="modal-title" class="modal-title"&gt;&nbsp;&lt;/span&gt;';
104
    html += '      &lt;/div&gt;';
105
    html += '      &lt;div id="modal-content" class="modal-content"&gt;';
106
    html += '      &lt;/div&gt;';
107
    html += '    &lt;/div&gt;';
108
    html += '  &lt;/div&gt;';
109

    
110
    return html;
111
  }
112
</pre></li>
113
<li> <strong>throbberTheme</strong>: The Drupal javascript themable function which controls how the modal throbber will be rendered. This function must be in the <em>Drupal.theme.prototype</em> namespace. If you set this value, you must include a corresponding function in a javascript file and use drupal_add_js() to add that file. <em>Default: CToolsModalThrobber</em>
114
<pre>
115
  Drupal.theme.prototype.CToolsModalThrobber = function () {
116
    var html = '';
117
    html += '  &lt;div id="modal-throbber"&gt;';
118
    html += '    &lt;div class="modal-throbber-wrapper"&gt;';
119
    html +=        Drupal.CTools.Modal.currentSettings.throbber;
120
    html += '    &lt;/div&gt;';
121
    html += '  &lt;/div&gt;';
122

    
123
    return html;
124
  };
125
</pre>
126
</li>
127
<li> <strong>modalOptions</strong>: The options object that's sent to Drupal.CTools.Modal.modalContent. Can contain any CSS settings that will be applied to the modal backdrop, in particular settings such as <strong>opacity</strong> and <strong>background</strong>. <em>Default: array('opacity' => .55, 'background' => '#fff');</em></li>
128
<li> <strong>animation</strong>: Controls how the modal is animated when it is first drawn. Either <strong>show</strong>, <strong>fadeIn</strong> or <strong>slideDown</strong>. <em>Default: show</em></li>
129
<li> <strong>animationSpeed</strong>: The speed of the animation, expressed either as a word jQuery understands (slow, medium or fast) or a number of milliseconds for the animation to run. <em>Defaults: fast</em></li>
130
<li><strong>closeText</strong>: The text to display for the close button. Be sure to wrap this in t() for translatability. <em>Default: t('Close window')</em></li>
131
<li><strong>closeImage</strong>: The image to use for the close button of the modal. <em>Default: theme('image', array('path' => ctools_image_path('icon-close-window.png'), 'alt' => t('Close window'), 'title' => t('Close window')))</em></li>
132
<li><strong>loadingText</strong>: The text to display for the modal title during loading, along with the throbber. Be sure to wrap this in t() for translatability. <em>Default: t('Close window')</em></li>
133
<li><strong>throbber</strong>: The HTML to display for the throbber image. You can use this instead of CToolsModalThrobber theme if you just want to change the image but not the throbber HTML. <em>Default: theme('image', array('path' => ctools_image_path('throbber.gif'), 'alt' => t('Loading...'), 'title' => t('Loading')))</em></li>
134
</ul>
135

    
136
<h2>Rendering within the modal</h2>
137
<p>To render your data inside the modal, you need to provide a page callback in your module that responds more or less like a normal page.</p>
138

    
139
<p>In order to handle degradability, it's nice to allow your page to work both inside and outside of the modal so that users whose javascript is turned off can still use your page. There are two ways to accomplish this. First, you can embed 'nojs' as a portion of the URL and then watch to see if that turns into 'ajax' when the link is clicked. Second, if you do not wish to modify the URLs, you can check $_POST['js'] or $_POST['ctools_js'] to see if that flag was set. The URL method is the most flexible because it is easy to send the two links along completely different paths if necessary, and it is also much easier to manually test your module's output by manually visiting the nojs URL. It's actually quite difficult to do this if you have to set $_POST['js'] to test.</p>
140

    
141
<p>In your hook_menu, you can use the a CTools' AJAX convenience loader to help:</p>
142

    
143
<pre>
144
  $items['ctools_ajax_sample/%ctools_js/login'] = array(
145
      'title' => 'Login',
146
      'page callback' => 'ctools_ajax_sample_login',
147
      'page arguments' => array(1),
148
      'access callback' => TRUE,
149
      'type' => MENU_CALLBACK,
150
  );
151
</pre>
152

    
153
<p>The first argument to the page callback will be the result of ctools_js_load() which will return TRUE if 'ajax' was the string, and FALSE if anything else (i.e, nojs) is the string. Which means you can then declare your function like this:</p>
154

    
155
<pre>
156
function ctools_ajax_sample_login($js) {
157
  if ($js) {
158
    // react with the modal
159
  }
160
  else {
161
    // react without the modal
162
  }
163
}
164
</pre>
165

    
166
<p>If your modal does not include a form, rendering the output you wish to give the user is just a matter of calling the modal renderer with your data:</p>
167

    
168
<pre>
169
function ctools_ajax_hello_world($js) {
170
  $title = t('Greetings');
171
  $output = '&lt;p&gt;' . t('Hello world') . ''&lt;/p&gt;';
172
  if ($js) {
173
    ctools_modal_render($title, $output);
174
  }
175
  else {
176
    drupal_set_title($title);
177
    return $output;
178
  }
179
}
180
</pre>
181

    
182
<p>If you need to do more than just render your modal, you can use a CTools $commands array. See the function ctools_modal_render() for more information on what you need to do here.</p>
183

    
184
<p>If you are displaying a form -- and the vast majority of modals display forms -- then you need to do just slightly more. Fortunately there is the ctools_modal_form_wrapper() function:</p>
185

    
186
<pre>
187
  ctools_include('modal');
188
  ctools_include('ajax');
189
  $form_state = array(
190
    'title' => t('Title of my form'),
191
    'ajax' => $js,
192
  );
193
  $output = ctools_modal_form_wrapper('my_form', $form_state);
194
  // There are some possible states after calling the form wrapper:
195
  // 1) We are not using $js and the form has been executed.
196
  // 2) We are using $js and the form was successfully submitted and
197
  //    we need to dismiss the modal.
198
  // Most other states are handled automatically unless you set flags in
199
  // $form_state to avoid handling them, so we only deal with those two
200
  // states.
201
  if ($form_state['executed'] && $js) {
202
    $commands = array();
203
    $commands[] = ctools_modal_command_dismiss(t('Login Success'));
204
    // In typical usage you will do something else here, such as update a
205
    // div with HTML or redirect the page based upon the results of the modal
206
    // form.
207
    print ajax_render($commands);
208
    exit;
209
  }
210

    
211
  // Otherwise, just return the output.
212
  return $output;
213
</pre>
214

    
215
<p>You can also use CTools' form wizard inside the modal. You do not need to do much special beyond setting $form_state['modal'] = TRUE in the wizard form; it already knows how to handle the rest.</p>