@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PHUITabGroupView extends AphrontTagView {
4
5 private $tabs = array();
6 private $selectedTab;
7 private $vertical;
8
9 private $hideSingleTab;
10
11 protected function canAppendChild() {
12 return false;
13 }
14
15 public function setVertical($vertical) {
16 $this->vertical = $vertical;
17 return $this;
18 }
19
20 public function getVertical() {
21 return $this->vertical;
22 }
23
24 public function setHideSingleTab($hide_single_tab) {
25 $this->hideSingleTab = $hide_single_tab;
26 return $this;
27 }
28
29 public function getHideSingleTab() {
30 return $this->hideSingleTab;
31 }
32
33 public function addTab(PHUITabView $tab) {
34 $key = $tab->getKey();
35 $tab->lockKey();
36
37 if (isset($this->tabs[$key])) {
38 throw new Exception(
39 pht(
40 'Each tab in a tab group must have a unique key; attempting to add '.
41 'a second tab with a duplicate key ("%s").',
42 $key));
43 }
44
45 $this->tabs[$key] = $tab;
46
47 return $this;
48 }
49
50 public function selectTab($key) {
51 if (empty($this->tabs[$key])) {
52 throw new Exception(
53 pht(
54 'Unable to select tab ("%s") which does not exist.',
55 $key));
56 }
57
58 $this->selectedTab = $key;
59
60 return $this;
61 }
62
63 public function getSelectedTabKey() {
64 if (!$this->tabs) {
65 return null;
66 }
67
68 if ($this->selectedTab !== null) {
69 return $this->selectedTab;
70 }
71
72 return head($this->tabs)->getKey();
73 }
74
75 protected function getTagAttributes() {
76 $tab_map = mpull($this->tabs, 'getContentID', 'getKey');
77
78 $classes = array();
79 if ($this->getVertical()) {
80 $classes[] = 'phui-tab-group-view-vertical';
81 }
82
83 return array(
84 'class' => $classes,
85 'sigil' => 'phui-tab-group-view',
86 'meta' => array(
87 'tabMap' => $tab_map,
88 ),
89 );
90 }
91
92 protected function getTagContent() {
93 Javelin::initBehavior('phui-tab-group');
94
95 $tabs = new PHUIListView();
96
97 if ($this->getVertical()) {
98 $tabs->setType(PHUIListView::NAVBAR_VERTICAL);
99 } else {
100 $tabs->setType(PHUIListView::NAVBAR_LIST);
101 }
102
103 $content = array();
104
105 $selected_tab = $this->getSelectedTabKey();
106 foreach ($this->tabs as $tab) {
107 $item = $tab->newMenuItem();
108 $tab_key = $tab->getKey();
109
110 if ($tab_key == $selected_tab) {
111 $item->setSelected(true);
112 $style = null;
113 } else {
114 $style = 'display: none;';
115 }
116
117 $tabs->addMenuItem($item);
118
119 $content[] = javelin_tag(
120 'div',
121 array(
122 'style' => $style,
123 'id' => $tab->getContentID(),
124 ),
125 $tab);
126 }
127
128 if ($this->hideSingleTab && (count($this->tabs) == 1)) {
129 $tabs = null;
130 }
131
132 if ($tabs && $this->getVertical()) {
133 $content = phutil_tag(
134 'table',
135 array(
136 'style' => 'width: 100%',
137 ),
138 phutil_tag(
139 'tbody',
140 array(),
141 phutil_tag(
142 'tr',
143 array(),
144 array(
145 phutil_tag(
146 'td',
147 array(
148 'class' => 'phui-tab-group-view-tab-column',
149 ),
150 $tabs),
151 phutil_tag(
152 'td',
153 array(),
154 $content),
155 ))));
156 $tabs = null;
157 }
158
159 return array(
160 $tabs,
161 $content,
162 );
163 }
164
165}