tabs.inc

Classes and theme functions for rendering javascript UI tabs.

File

includes/tabs.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Classes and theme functions for rendering javascript UI tabs.
  6. */
  7. /**
  8. * Contain a set of tabs as well as the ability to render them.
  9. *
  10. * There are three 'areas' of a tabset.
  11. * - title: The clickable link to display the tab area. These are always visible.
  12. * - body: The actual HTML body of the tab. Only one body is visible at a time.
  13. * - extra: An optional decorative area around the tabs.
  14. */
  15. class views_tabset {
  16. var $tabs = array();
  17. var $extra = '';
  18. var $selected = NULL;
  19. /**
  20. * Add a tab to the tabset.
  21. *
  22. * @param $name
  23. * The name of the tab; this is the internal identifier and must be
  24. * unique within the tabset.
  25. * @param $title
  26. * If given, this will be the visible title of the tab. This can also
  27. * be set via $tabset->set(). This will be the link to click on to
  28. * view the tab.
  29. * @param $body
  30. * If given, this is the body of the tab itself. It will display
  31. * when the tab title is clicked on.
  32. */
  33. function add($name, $title = '', $body = '') {
  34. if (is_object($name) && is_subclass_of($name, 'views_tab')) {
  35. $this->add_tab($name);
  36. }
  37. elseif (is_array($name)) {
  38. foreach ($name as $real_tab) {
  39. $this->add($real_tab);
  40. }
  41. }
  42. else {
  43. $this->add_tab(new views_tab($name, $title, $body));
  44. }
  45. }
  46. /**
  47. * Add a fully realized tab object to the tabset.
  48. *
  49. * @param $tab
  50. * A fully populated views_tab object.
  51. */
  52. function add_tab($tab) {
  53. $this->tabs[$tab->name] = $tab;
  54. }
  55. /**
  56. * Set the values of a tab.
  57. *
  58. * @param $name
  59. * The unique identifier of the tab to set.
  60. * @param $title
  61. * The title of the tab; this will be clickable.
  62. * @param $body
  63. * The HTML body of the tab.
  64. */
  65. function set($name, $title, $body = NULL) {
  66. if (empty($this->tabs[$name])) {
  67. return $this->add($name, $title, $body);
  68. }
  69. $this->tabs[$name]->title = $title;
  70. if (isset($body)) {
  71. $this->tabs[$name]->body = $body;
  72. }
  73. }
  74. /**
  75. * Set the body of a tab.
  76. */
  77. function set_body($name, $body) {
  78. if (empty($this->tabs[$name])) {
  79. return $this->add($name, '', $body);
  80. }
  81. $this->tabs[$name]->body = $body;
  82. }
  83. /**
  84. * Add text to the 'extra' region of the tabset.
  85. */
  86. function add_extra($text) {
  87. $this->extra .= $text;
  88. }
  89. /**
  90. * Remove a tab.
  91. *
  92. * @param $tab
  93. * May be the name of the tab or a views_tab object.
  94. */
  95. function remove($tab) {
  96. if (is_string($tab)) {
  97. unset($this->tabs[$tab]);
  98. }
  99. else {
  100. unset($this->tabs[$tab->name]);
  101. }
  102. }
  103. /**
  104. * Control which tab will be selected when it is rendered.
  105. */
  106. function set_selected($name) {
  107. $this->selected = $name;
  108. }
  109. /**
  110. * Output the HTML for the tabs.
  111. *
  112. * @return
  113. * HTML representation of the tabs.
  114. */
  115. function render() {
  116. views_add_js('tabs');
  117. views_add_css('views-tabs');
  118. if (empty($this->selected)) {
  119. $keys = array_keys($this->tabs);
  120. $this->selected = array_shift($keys);
  121. }
  122. drupal_alter('views_tabset', $this);
  123. return theme('views_tabset', $this->tabs, $this->extra, $this->selected);
  124. }
  125. }
  126. /**
  127. * An object to represent an individual tab within a tabset.
  128. */
  129. class views_tab {
  130. var $title;
  131. var $body;
  132. var $name;
  133. /**
  134. * Construct a new tab.
  135. */
  136. function views_tab($name, $title, $body = NULL) {
  137. $this->name = $name;
  138. $this->title = $title;
  139. $this->body = $body;
  140. }
  141. /**
  142. * Generate HTML output for a tab.
  143. */
  144. function render() {
  145. return theme('views_tab', $this->body);
  146. }
  147. }
  148. /**
  149. * Render a tabset.
  150. *
  151. * @todo Turn this into a template.
  152. */
  153. function theme_views_tabset($tabs, $extra = NULL, $selected = NULL) {
  154. $link_output = "<div class=\"views-tabs\"><ul id=\"views-tabset\">\n";
  155. $tab_output = "<div class=\"views-tab-area\">\n";
  156. foreach ($tabs as $name => $tab) {
  157. $link_output .= '<li' . ($name == $selected ? ' class="active"': '') . '><a href="#views-tab-' . $tab->name . '" id="views-tab-title-' . $tab->name . '">' . check_plain($tab->title) . '</a></li>' . "\n";
  158. $tab_output .= '<div id="views-tab-' . $tab->name . '" class="views-tab">' . $tab->render() . "</div>\n";
  159. }
  160. $link_output .= "</ul>\n";
  161. if ($extra) {
  162. $link_output .= "<div class=\"extra\">$extra</div>\n";
  163. }
  164. $link_output .= "</div>\n";
  165. $tab_output .= "</div>\n";
  166. return '<div class="views-tabset clear-block">' . $link_output . $tab_output . '</div>';
  167. }
  168. /**
  169. * Theme a simple tab.
  170. */
  171. function theme_views_tab($body) {
  172. return $body;
  173. }