PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/sites/all/modules/contrib/ctools/help/modal.html

https://github.com/aakb/sport-aarhus-events
HTML | 219 lines | 186 code | 33 blank | 0 comment | 0 complexity | 1ff937f35d10c522532f1b2d8cecc2b8 MD5 | raw file
  1. 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.
  2. <h3>Invoking the modal</h3>
  3. 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:
  4. <pre>
  5. ctools_include('modal');
  6. ctools_modal_add_js();
  7. </pre>
  8. 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:
  9. <pre>
  10. /**
  11. * Render an image as a button link. This will automatically apply an AJAX class
  12. * to the link and add the appropriate javascript to make this happen.
  13. *
  14. * @param $image
  15. * The path to an image to use that will be sent to theme('image') for rendering.
  16. * @param $dest
  17. * The destination of the link.
  18. * @param $alt
  19. * The alt text of the link.
  20. * @param $class
  21. * Any class to apply to the link. @todo this should be a options array.
  22. */
  23. function ctools_modal_image_button($image, $dest, $alt, $class = '') {
  24. return ctools_ajax_text_button(theme('image', $image), $dest, $alt, $class, 'ctools-use-modal');
  25. }
  26. /**
  27. * Render text as a link. This will automatically apply an AJAX class
  28. * to the link and add the appropriate javascript to make this happen.
  29. *
  30. * Note: 'html' => true so be sure any text is vetted! Chances are these kinds of buttons will
  31. * not use user input so this is a very minor concern.
  32. *
  33. * @param $image
  34. * The path to an image to use that will be sent to theme('image') for rendering.
  35. * @param $dest
  36. * The destination of the link.
  37. * @param $alt
  38. * The alt text of the link.
  39. * @param $class
  40. * Any class to apply to the link. @todo this should be a options array.
  41. */
  42. function ctools_modal_text_button($text, $dest, $alt, $class = '') {
  43. return ctools_ajax_text_button($text, $dest, $alt, $class, 'ctools-use-modal');
  44. }
  45. </pre>
  46. Like with all CTools' AJAX functionality, the href of the link will be the destination, with any appearance of /nojs/ converted to /ajax/.
  47. 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.
  48. 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.
  49. <h3>Customizing the modal</h3>
  50. 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.
  51. First, you need to create a settings array. You can do this most easily with a bit of PHP:
  52. <pre>
  53. drupal_add_js(array(
  54. 'my-modal-style' => array(
  55. 'modalSize' => array(
  56. 'type' => 'fixed',
  57. 'width' => 250,
  58. 'height' => 250,
  59. ),
  60. ),
  61. ), 'setting');
  62. </pre>
  63. 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.
  64. 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).
  65. 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.
  66. You can set:
  67. <ul>
  68. <li> <b>modalSize</b>: an array of data to control the sizing of the modal. It can contain:
  69. <ul>
  70. <li> <b>type</b>: Either <i>fixed</i> or <i>scale</i>. If fixed, the modal will always be a fixed size. If <i>scale</i> the modal will scale to a percentage of the browser window. <i>Default: scale</i>.
  71. <li> <b>width</b>: If <i>fixed</i> the width in pixels. If <i>scale</i> the percentage of the screen expressed as a number less than zero. (For 80 percent, use .8, for example). <i>Default: .8</i></li>
  72. <li> <b>height</b>: If <i>fixed</i> the height in pixels. If <i>scale</i> the percentage of the screen expressed as a number less than zero. (For 80 percent, use .8, for example). <i>Default: .8</i></li>
  73. <li> <b>addWidth</b>: Any additional width to add to the modal in pixels. Only useful if the type is scale. <i>Default: 0</i></li>
  74. <li> <b>addHeight</b>: Any additional height to add to the modal in pixels. Only useful if the type is scale. <i>Default: 0</i></li>
  75. <li> <b>contentRight</b>: The number of pixels to remove from the content inside the modal to make room for scroll bar and decorations. <i>Default: 25</i></li>
  76. <li> <b>contentBottom</b>: The number of pixels to remove from the content inside the modal to make room for scroll bar and decorations. <i>Default: 45</i></li>
  77. </ul>
  78. </li>
  79. <li> <b>modalTheme</b>: The Drupal javascript themable function which controls how the modal will be rendered. This function must be in the <i>Drupal.theme.prototype</i> 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. <i>Default: CToolsModalDialog</i>
  80. <pre>
  81. Drupal.theme.prototype.CToolsModalDialog = function () {
  82. var html = ''
  83. html += ' &lt;div id="ctools-modal"&gt;'
  84. html += ' &lt;div class="ctools-modal-content"&gt;' // panels-modal-content
  85. html += ' &lt;div class="modal-header"&gt;';
  86. html += ' &lt;a class="close" href="#"&gt;';
  87. html += Drupal.CTools.Modal.currentSettings.closeText + Drupal.CTools.Modal.currentSettings.closeImage;
  88. html += ' &lt;/a&gt;';
  89. html += ' &lt;span id="modal-title" class="modal-title"&gt;&nbsp;&lt;/span&gt;';
  90. html += ' &lt;/div&gt;';
  91. html += ' &lt;div id="modal-content" class="modal-content"&gt;';
  92. html += ' &lt;/div&gt;';
  93. html += ' &lt;/div&gt;';
  94. html += ' &lt;/div&gt;';
  95. return html;
  96. }
  97. </pre></li>
  98. <li> <b>throbberTheme</b>: The Drupal javascript themable function which controls how the modal throbber will be rendered. This function must be in the <i>Drupal.theme.prototype</i> 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. <i>Default: CToolsModalThrobber</i>
  99. <pre>
  100. Drupal.theme.prototype.CToolsModalThrobber = function () {
  101. var html = '';
  102. html += ' &lt;div id="modal-throbber"&gt;';
  103. html += ' &lt;div class="modal-throbber-wrapper"&gt;';
  104. html += Drupal.CTools.Modal.currentSettings.throbber;
  105. html += ' &lt;/div&gt;';
  106. html += ' &lt;/div&gt;';
  107. return html;
  108. };
  109. </pre>
  110. </li>
  111. <li> <b>modalOptions</b>: 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 <b>opacity</b> and <b>background</b>. <i>Default: array('opacity' => .55, 'background' => '#fff');</i></li>
  112. <li> <b>animation</b>: Controls how the modal is animated when it is first drawn. Either <b>show</b>, <b>fadeIn</b> or <b>slideDown</b>. <i>Default: show</i></li>
  113. <li> <b>animationSpeed</b>: 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. <i>Defaults: fast</i></li>
  114. <li><b>closeText</b>: The text to display for the close button. Be sure to wrap this in t() for translatability. <i>Default: t('Close window')</i></li>
  115. <li><b>closeImage</b>: The image to use for the close button of the modal. <i>Default: theme('image', ctools_image_path('icon-close-window.png'), t('Close window'), t('Close window'))</i></li>
  116. <li><b>loadingText</b>: The text to display for the modal title during loading, along with the throbber. Be sure to wrap this in t() for translatability. <i>Default: t('Close window')</i></li>
  117. <li><b>throbber</b>: 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. <i>Default: theme('image', ctools_image_path('throbber.gif'), t('Loading...'), t('Loading'))</i></li>
  118. <li>
  119. </ul>
  120. <h3>Rendering within the modal</h3>
  121. 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.
  122. 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.
  123. In your hook_menu, you can use the a CTools' AJAX convenience loader to help:
  124. <pre>
  125. $items['ctools_ajax_sample/%ctools_js/login'] = array(
  126. 'title' => 'Login',
  127. 'page callback' => 'ctools_ajax_sample_login',
  128. 'page arguments' => array(1),
  129. 'access callback' => TRUE,
  130. 'type' => MENU_CALLBACK,
  131. );
  132. </pre>
  133. 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:
  134. <pre>
  135. function ctools_ajax_sample_login($js) {
  136. if ($js) {
  137. // react with the modal
  138. }
  139. else {
  140. // react without the modal
  141. }
  142. }
  143. </pre>
  144. 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:
  145. <pre>
  146. function ctools_ajax_hello_world($js) {
  147. $title = t('Greetings');
  148. $output = '&lt;p&gt;' . t('Hello world') . ''&lt;/p&gt;';
  149. if ($js) {
  150. ctools_modal_render($title, $output);
  151. }
  152. else {
  153. drupal_set_title($title);
  154. return $output;
  155. }
  156. }
  157. </pre>
  158. 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.
  159. 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:
  160. <pre>
  161. ctools_include('modal');
  162. ctools_include('ajax');
  163. $form_state = array(
  164. 'title' => t('Title of my form'),
  165. 'ajax' => $js,
  166. );
  167. $output = ctools_modal_form_wrapper('my_form', $form_state);
  168. // There are some possible states after calling the form wrapper:
  169. // 1) We are not using $js and there is form output to be rendered.
  170. // 2) We are using $js and the form was successfully submitted and
  171. // we need to dismiss the modal.
  172. // Most other states are handled automatically unless you set flags in
  173. // $form_state to avoid handling them, so we only deal with those two
  174. // states.
  175. if (empty($output) && $js) {
  176. $commands = array();
  177. $commands[] = ctools_modal_command_dismiss(t('Login Success');
  178. // In typical usage you will do something else here, such as update a
  179. // div with HTML or redirect the page based upon the results of the modal
  180. // form.
  181. ctools_ajax_render($commands);
  182. }
  183. // Otherwise, just return the output.
  184. return $output;
  185. </pre>
  186. 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.