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