js_example.module 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @file
  4. * Examples showing how to use some of the new JavaScript features in Drupal 7.
  5. */
  6. /**
  7. * @defgroup js_example Example: JavaScript
  8. * @ingroup examples
  9. * @{
  10. * Examples using Drupal 7's built-in JavaScript.
  11. */
  12. /**
  13. * Implements hook_theme().
  14. */
  15. function js_example_theme() {
  16. return array(
  17. 'my_accordion' => array(
  18. 'template' => 'accordion',
  19. 'variables' => array('title' => NULL),
  20. ),
  21. );
  22. }
  23. /**
  24. * Implements hook_menu().
  25. */
  26. function js_example_menu() {
  27. $items = array();
  28. $items['js_example/weights'] = array(
  29. 'title' => 'JS Example: see weighting in action',
  30. 'page callback' => 'js_example_js_weights',
  31. 'access callback' => TRUE,
  32. );
  33. $items['js_example/accordion'] = array(
  34. 'title' => 'JS Example: jQuery UI accordion',
  35. 'page callback' => 'js_example_accordion',
  36. 'access callback' => TRUE,
  37. );
  38. return $items;
  39. }
  40. /**
  41. * Weights demonstration.
  42. *
  43. * Here we demonstrate attaching a number of scripts to the render array.
  44. * These scripts generate content according to 'weight' and color.
  45. *
  46. * On the Drupal side, we do three main things:
  47. * - Create a container DIV, with an ID all the scripts can recognize.
  48. * - Attach some scripts which generate color-coded content. We use the
  49. * 'weight' attribute to set the order in which the scripts are included.
  50. * - Add the color->weight array to the settings variable in each *color*.js
  51. * file. This is where Drupal passes data out to JavaScript.
  52. *
  53. * Each of the color scripts (red.js, blue.js, etc) uses jQuery to find our
  54. * DIV, and then add some content to it. The order in which the color scripts
  55. * execute will end up being the order of the content.
  56. *
  57. * The 'weight' form atttribute determines the order in which a script is
  58. * output to the page. To see this in action:
  59. * - Uncheck the 'Aggregate Javascript files' setting at:
  60. * admin/config/development/performance.
  61. * - Load the page: js_example/weights. Examine the page source.
  62. * You will see that the color js scripts have been added in the <head>
  63. * element in weight order.
  64. *
  65. * To test further, change a weight in the $weights array below, then save
  66. * this file and reload js_example/weights. Examine the new source to see the
  67. * reordering.
  68. *
  69. * @return array
  70. * A renderable array.
  71. */
  72. function js_example_js_weights() {
  73. // Add some css to show which line is output by which script.
  74. drupal_add_css(drupal_get_path('module', 'js_example') . '/css/jsweights.css');
  75. // Create an array of items with random-ish weight values.
  76. $weights = array(
  77. 'red' => 100,
  78. 'blue' => 23,
  79. 'green' => 3,
  80. 'brown' => 45,
  81. 'black' => 5,
  82. 'purple' => 60,
  83. );
  84. // Attach the weights array to our JavaScript settings. This allows the
  85. // color scripts to discover their weight values, by accessing
  86. // settings.jsWeights.*color*. The color scripts only use this information for
  87. // display to the user.
  88. drupal_add_js(array('jsWeights' => $weights), array('type' => 'setting'));
  89. // Add our individual scripts. We add them in an arbitrary order, but the
  90. // 'weight' attribute will cause Drupal to render (and thus load and execute)
  91. // them in the weighted order.
  92. drupal_add_js(drupal_get_path('module', 'js_example') . '/js/red.js', array('weight' => $weights['red']));
  93. drupal_add_js(drupal_get_path('module', 'js_example') . '/js/blue.js', array('weight' => $weights['blue']));
  94. drupal_add_js(drupal_get_path('module', 'js_example') . '/js/green.js', array('weight' => $weights['green']));
  95. drupal_add_js(drupal_get_path('module', 'js_example') . '/js/brown.js', array('weight' => $weights['brown']));
  96. drupal_add_js(drupal_get_path('module', 'js_example') . '/js/black.js', array('weight' => $weights['black']));
  97. drupal_add_js(drupal_get_path('module', 'js_example') . '/js/purple.js', array('weight' => $weights['purple']));
  98. // Main container DIV. We give it a unique ID so that the JavaScript can
  99. // find it using jQuery.
  100. $output = '<div id="js-weights"></div>';
  101. return $output;
  102. }
  103. /**
  104. * Demonstrate accordion effect.
  105. */
  106. function js_example_accordion() {
  107. $title = t('Click sections to expand or collapse:');
  108. $build['myelement'] = array(
  109. '#theme' => 'my_accordion',
  110. '#title' => $title,
  111. );
  112. $build['myelement']['#attached']['library'][] = array('system', 'ui.accordion');
  113. $build['myelement']['#attached']['js'][] = array('data' => '(function($){$(function() { $("#accordion").accordion(); })})(jQuery);', 'type' => 'inline');
  114. $output = drupal_render($build);
  115. return $output;
  116. }