forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPHUIListView.php
193 lines (154 loc) · 4.12 KB
/
PHUIListView.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
final class PHUIListView extends AphrontTagView {
const NAVBAR_LIST = 'phui-list-navbar';
const SIDENAV_LIST = 'phui-list-sidenav';
const TABBAR_LIST = 'phui-list-tabbar';
private $items = array();
private $type;
protected function canAppendChild() {
return false;
}
public function newLabel($name, $key = null) {
$item = id(new PHUIListItemView())
->setType(PHUIListItemView::TYPE_LABEL)
->setName($name);
if ($key !== null) {
$item->setKey($key);
}
$this->addMenuItem($item);
return $item;
}
public function newLink($name, $href, $key = null) {
$item = id(new PHUIListItemView())
->setType(PHUIListItemView::TYPE_LINK)
->setName($name)
->setHref($href);
if ($key !== null) {
$item->setKey($key);
}
$this->addMenuItem($item);
return $item;
}
public function newButton($name, $href) {
$item = id(new PHUIListItemView())
->setType(PHUIListItemView::TYPE_BUTTON)
->setName($name)
->setHref($href);
$this->addMenuItem($item);
return $item;
}
public function addMenuItem(PHUIListItemView $item) {
return $this->addMenuItemAfter(null, $item);
}
public function addMenuItemAfter($key, PHUIListItemView $item) {
if ($key === null) {
$this->items[] = $item;
return $this;
}
if (!$this->getItem($key)) {
throw new Exception(pht("No such key '%s' to add menu item after!",
$key));
}
$result = array();
foreach ($this->items as $other) {
$result[] = $other;
if ($other->getKey() == $key) {
$result[] = $item;
}
}
$this->items = $result;
return $this;
}
public function addMenuItemBefore($key, PHUIListItemView $item) {
if ($key === null) {
array_unshift($this->items, $item);
return $this;
}
$this->requireKey($key);
$result = array();
foreach ($this->items as $other) {
if ($other->getKey() == $key) {
$result[] = $item;
}
$result[] = $other;
}
$this->items = $result;
return $this;
}
public function addMenuItemToLabel($key, PHUIListItemView $item) {
$this->requireKey($key);
$other = $this->getItem($key);
if ($other->getType() != PHUIListItemView::TYPE_LABEL) {
throw new Exception(pht("Menu item '%s' is not a label!", $key));
}
$seen = false;
$after = null;
foreach ($this->items as $other) {
if (!$seen) {
if ($other->getKey() == $key) {
$seen = true;
}
} else {
if ($other->getType() == PHUIListItemView::TYPE_LABEL) {
break;
}
}
$after = $other->getKey();
}
return $this->addMenuItemAfter($after, $item);
}
private function requireKey($key) {
if (!$this->getItem($key)) {
throw new Exception(pht("No menu item with key '%s' exists!", $key));
}
}
public function getItem($key) {
$key = (string)$key;
// NOTE: We could optimize this, but need to update any map when items have
// their keys change. Since that's moderately complex, wait for a profile
// or use case.
foreach ($this->items as $item) {
if ($item->getKey() == $key) {
return $item;
}
}
return null;
}
public function getItems() {
return $this->items;
}
protected function willRender() {
$key_map = array();
foreach ($this->items as $item) {
$key = $item->getKey();
if ($key !== null) {
if (isset($key_map[$key])) {
throw new Exception(
pht("Menu contains duplicate items with key '%s'!", $key));
}
$key_map[$key] = $item;
}
}
}
public function getTagName() {
return 'ul';
}
public function setType($type) {
$this->type = $type;
return $this;
}
protected function getTagAttributes() {
require_celerity_resource('phui-list-view-css');
$classes = array();
$classes[] = 'phui-list-view';
if ($this->type) {
$classes[] = $this->type;
}
return array(
'class' => implode(' ', $classes),
);
}
protected function getTagContent() {
return $this->items;
}
}