dropbutton.theme.inc 5.1 KB

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