dropbutton.theme.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @file
  4. * Provide a javascript based dropbutton menu.
  5. *
  6. * The dropbutton menu will show up as a button with a clickable twisty pointer
  7. * to the right. When clicked the button will expand, showing the list of links.
  8. *
  9. * The dropbutton will stay open until either the user has moved the mouse
  10. * away from the box for > .5 seconds, or can be immediately closed by
  11. * clicking the twisty again. The code is smart enough that if the mouse
  12. * moves away and then back within the .5 second window, it will not
  13. * re-close.
  14. *
  15. * Multiple dropbuttons can be placed per page.
  16. *
  17. * If only one link is passed to the theme function, the function will render
  18. * a ctools-button with no twisty. The twisty is only rendered when 2 or more
  19. * links are provided. The wrapping element will be classed with both
  20. * ctools-button and ctools-dropbutton when a dropbutton is rendered.
  21. *
  22. * If the user does not have javascript enabled, the link will not appear,
  23. * and instead by default the list of links will appear as a normal inline
  24. * list.
  25. *
  26. * The menu is minimally styled by default, and to make it look different
  27. * will require your own CSS. You can apply your own class to the
  28. * dropbutton to help style it.
  29. *
  30. * The twisty is rendered as a border on a widthless and heightless element.
  31. * There is no image for the twisty.
  32. * The color of the twisty is the color of the link by default. To adjust the
  33. * size of the twisty, adjust the border widths on .ctools-twisty. The adjust
  34. * the color of the twisty, assign a new color to the .ctools-button class or
  35. * assign a color to .ctools-twisty. You shouldn't need to adjust border-color.
  36. *
  37. * Use the theme() function to render dropbutton e.g.
  38. * theme('links__ctools_dropbutton', array()) where array contains a renderable
  39. * array of links.
  40. */
  41. /**
  42. * Delegated implementation of hook_theme()
  43. */
  44. function ctools_dropbutton_theme(&$items) {
  45. $items['links__ctools_dropbutton'] = array(
  46. 'variables' => array('title' => NULL, 'links' => NULL, 'image' => FALSE, 'class' => NULL),
  47. 'file' => 'includes/dropbutton.theme.inc',
  48. );
  49. }
  50. /**
  51. * Create a dropbutton menu.
  52. *
  53. * @param $title
  54. * The text to place in the clickable area to activate the dropbutton. This
  55. * text is indented to -9999px by default.
  56. * @param $links
  57. * A list of links to provide within the dropbutton, suitable for use
  58. * in via Drupal's theme('links').
  59. * @param $image
  60. * If true, the dropbutton link is an image and will not get extra decorations
  61. * that a text dropbutton link will.
  62. * @param $class
  63. * An optional class to add to the dropbutton's container div to allow you
  64. * to style a single dropbutton however you like without interfering with
  65. * other dropbuttons.
  66. */
  67. function theme_links__ctools_dropbutton($vars) {
  68. // Check to see if the number of links is greater than 1;
  69. // otherwise just treat this like a button.
  70. if (!empty($vars['links'])) {
  71. $is_drop_button = (count($vars['links']) > 1);
  72. // Add needed files
  73. if ($is_drop_button) {
  74. ctools_add_js('dropbutton');
  75. ctools_add_css('dropbutton');
  76. }
  77. ctools_add_css('button');
  78. // Provide a unique identifier for every button on the page.
  79. static $id = 0;
  80. $id++;
  81. // Wrapping div
  82. $class = 'ctools-no-js';
  83. $class .= ($is_drop_button) ? ' ctools-dropbutton' : '';
  84. $class .= ' ctools-button';
  85. if (!empty($vars['class'])) {
  86. $class .= ($vars['class']) ? (' ' . implode(' ', $vars['class'])) : '';
  87. }
  88. $output = '';
  89. $output .= '<div class="' . $class . '" id="ctools-button-' . $id . '">';
  90. // Add a twisty if this is a dropbutton
  91. if ($is_drop_button) {
  92. $vars['title'] = ($vars['title'] ? check_plain($vars['title']) : t('open'));
  93. $output .= '<div class="ctools-link">';
  94. if ($vars['image']) {
  95. $output .= '<a href="#" class="ctools-twisty ctools-image">' . $vars['title'] . '</a>';
  96. }
  97. else {
  98. $output .= '<a href="#" class="ctools-twisty ctools-text">' . $vars['title'] . '</a>';
  99. }
  100. $output .= '</div>'; // ctools-link
  101. }
  102. // The button content
  103. $output .= '<div class="ctools-content">';
  104. // Check for attributes. theme_links expects an array().
  105. $vars['attributes'] = (!empty($vars['attributes'])) ? $vars['attributes'] : array();
  106. // Remove the inline and links classes from links if they exist.
  107. // These classes are added and styled by Drupal core and mess up the default
  108. // styling of any link list.
  109. if (!empty($vars['attributes']['class'])) {
  110. $classes = $vars['attributes']['class'];
  111. foreach ($classes as $key => $class) {
  112. if ($class === 'inline' || $class === 'links') {
  113. unset($vars['attributes']['class'][$key]);
  114. }
  115. }
  116. }
  117. // Call theme_links to render the list of links.
  118. $output .= theme_links(array('links' => $vars['links'], 'attributes' => $vars['attributes'], 'heading' => ''));
  119. $output .= '</div>'; // ctools-content
  120. $output .= '</div>'; // ctools-dropbutton
  121. return $output;
  122. }
  123. else {
  124. return '';
  125. }
  126. }