2016-07-01 00:57:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PHUITabGroupView extends AphrontTagView {
|
|
|
|
|
|
|
|
private $tabs = array();
|
2016-07-01 01:08:56 +02:00
|
|
|
private $selectedTab;
|
|
|
|
|
|
|
|
private $hideSingleTab;
|
2016-07-01 00:57:32 +02:00
|
|
|
|
|
|
|
protected function canAppendChild() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-01 01:08:56 +02:00
|
|
|
public function setHideSingleTab($hide_single_tab) {
|
|
|
|
$this->hideSingleTab = $hide_single_tab;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHideSingleTab() {
|
|
|
|
return $this->hideSingleTab;
|
|
|
|
}
|
|
|
|
|
2016-07-01 00:57:32 +02:00
|
|
|
public function addTab(PHUITabView $tab) {
|
|
|
|
$key = $tab->getKey();
|
|
|
|
$tab->lockKey();
|
|
|
|
|
|
|
|
if (isset($this->tabs[$key])) {
|
|
|
|
throw new Exception(
|
|
|
|
pht(
|
|
|
|
'Each tab in a tab group must have a unique key; attempting to add '.
|
|
|
|
'a second tab with a duplicate key ("%s").',
|
|
|
|
$key));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->tabs[$key] = $tab;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-07-01 01:08:56 +02:00
|
|
|
public function selectTab($key) {
|
|
|
|
if (empty($this->tabs[$key])) {
|
|
|
|
throw new Exception(
|
|
|
|
pht(
|
|
|
|
'Unable to select tab ("%s") which does not exist.',
|
|
|
|
$key));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->selectedTab = $key;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSelectedTabKey() {
|
2016-07-01 00:57:32 +02:00
|
|
|
if (!$this->tabs) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-07-01 01:08:56 +02:00
|
|
|
if ($this->selectedTab !== null) {
|
|
|
|
return $this->selectedTab;
|
|
|
|
}
|
|
|
|
|
2016-07-01 00:57:32 +02:00
|
|
|
return head($this->tabs)->getKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTagAttributes() {
|
|
|
|
$tab_map = mpull($this->tabs, 'getContentID', 'getKey');
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'sigil' => 'phui-object-box',
|
|
|
|
'meta' => array(
|
|
|
|
'tabMap' => $tab_map,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTagContent() {
|
|
|
|
Javelin::initBehavior('phui-object-box-tabs');
|
|
|
|
|
|
|
|
$tabs = id(new PHUIListView())
|
|
|
|
->setType(PHUIListView::NAVBAR_LIST);
|
|
|
|
$content = array();
|
|
|
|
|
2016-07-01 01:08:56 +02:00
|
|
|
$selected_tab = $this->getSelectedTabKey();
|
2016-07-01 00:57:32 +02:00
|
|
|
foreach ($this->tabs as $tab) {
|
|
|
|
$item = $tab->newMenuItem();
|
|
|
|
$tab_key = $tab->getKey();
|
|
|
|
|
|
|
|
if ($tab_key == $selected_tab) {
|
|
|
|
$item->setSelected(true);
|
|
|
|
$style = null;
|
|
|
|
} else {
|
|
|
|
$style = 'display: none;';
|
|
|
|
}
|
|
|
|
|
|
|
|
$tabs->addMenuItem($item);
|
|
|
|
|
|
|
|
$content[] = javelin_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'style' => $style,
|
|
|
|
'id' => $tab->getContentID(),
|
|
|
|
),
|
|
|
|
$tab);
|
|
|
|
}
|
|
|
|
|
2016-07-01 01:08:56 +02:00
|
|
|
if ($this->hideSingleTab && (count($this->tabs) == 1)) {
|
|
|
|
$tabs = null;
|
|
|
|
}
|
|
|
|
|
2016-07-01 00:57:32 +02:00
|
|
|
return array(
|
|
|
|
$tabs,
|
|
|
|
$content,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|