Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/view/phui/PHUIButtonView.php
12249 views
1
<?php
2
3
final class PHUIButtonView extends AphrontTagView {
4
5
const GREEN = 'green';
6
const GREY = 'grey';
7
const BLUE = 'blue';
8
const RED = 'red';
9
const DISABLED = 'disabled';
10
11
const SMALL = 'small';
12
const BIG = 'big';
13
14
const BUTTONTYPE_DEFAULT = 'buttontype.default';
15
const BUTTONTYPE_SIMPLE = 'buttontype.simple';
16
17
private $size;
18
private $text;
19
private $subtext;
20
private $color;
21
private $tag = 'button';
22
private $dropdown;
23
private $icon;
24
private $iconFirst;
25
private $href = null;
26
private $title = null;
27
private $disabled;
28
private $selected;
29
private $name;
30
private $tooltip;
31
private $noCSS;
32
private $hasCaret;
33
private $buttonType = self::BUTTONTYPE_DEFAULT;
34
private $auralLabel;
35
36
public function setName($name) {
37
$this->name = $name;
38
return $this;
39
}
40
41
public function getName() {
42
return $this->name;
43
}
44
45
public function setText($text) {
46
$this->text = $text;
47
return $this;
48
}
49
50
public function setHref($href) {
51
$this->href = $href;
52
return $this;
53
}
54
55
public function setTitle($title) {
56
$this->title = $title;
57
return $this;
58
}
59
60
public function setSubtext($subtext) {
61
$this->subtext = $subtext;
62
return $this;
63
}
64
65
public function setColor($color) {
66
$this->color = $color;
67
return $this;
68
}
69
70
public function getColor() {
71
return $this->color;
72
}
73
74
public function setDisabled($disabled) {
75
$this->disabled = $disabled;
76
return $this;
77
}
78
79
public function setSelected($selected) {
80
$this->selected = $selected;
81
return $this;
82
}
83
84
public function setTag($tag) {
85
$this->tag = $tag;
86
return $this;
87
}
88
89
public function setSize($size) {
90
$this->size = $size;
91
return $this;
92
}
93
94
public function setDropdown($dd) {
95
$this->dropdown = $dd;
96
return $this;
97
}
98
99
public function setTooltip($text) {
100
$this->tooltip = $text;
101
return $this;
102
}
103
104
public function setNoCSS($no_css) {
105
$this->noCSS = $no_css;
106
return $this;
107
}
108
109
public function setHasCaret($has_caret) {
110
$this->hasCaret = $has_caret;
111
return $this;
112
}
113
114
public function getHasCaret() {
115
return $this->hasCaret;
116
}
117
118
public function setButtonType($button_type) {
119
$this->buttonType = $button_type;
120
return $this;
121
}
122
123
public function getButtonType() {
124
return $this->buttonType;
125
}
126
127
public function setAuralLabel($aural_label) {
128
$this->auralLabel = $aural_label;
129
return $this;
130
}
131
132
public function getAuralLabel() {
133
return $this->auralLabel;
134
}
135
136
public function setIcon($icon, $first = true) {
137
if (!($icon instanceof PHUIIconView)) {
138
$icon = id(new PHUIIconView())
139
->setIcon($icon);
140
}
141
$this->icon = $icon;
142
$this->iconFirst = $first;
143
return $this;
144
}
145
146
protected function getTagName() {
147
return $this->tag;
148
}
149
150
public function setDropdownMenu(PhabricatorActionListView $actions) {
151
Javelin::initBehavior('phui-dropdown-menu');
152
153
$this->addSigil('phui-dropdown-menu');
154
$this->setDropdown(true);
155
$this->setMetadata($actions->getDropdownMenuMetadata());
156
157
return $this;
158
}
159
160
public function setDropdownMenuID($id) {
161
Javelin::initBehavior('phui-dropdown-menu');
162
163
$this->addSigil('phui-dropdown-menu');
164
$this->setMetadata(
165
array(
166
'menuID' => $id,
167
));
168
169
return $this;
170
}
171
172
protected function getTagAttributes() {
173
174
require_celerity_resource('phui-button-css');
175
require_celerity_resource('phui-button-simple-css');
176
177
$classes = array();
178
$classes[] = 'button';
179
180
if ($this->color) {
181
$classes[] = 'button-'.$this->color;
182
}
183
184
if ($this->size) {
185
$classes[] = $this->size;
186
}
187
188
if ($this->dropdown) {
189
$classes[] = 'dropdown';
190
}
191
192
if ($this->icon) {
193
$classes[] = 'has-icon';
194
}
195
196
if ($this->text !== null) {
197
$classes[] = 'has-text';
198
}
199
200
if ($this->iconFirst == false) {
201
$classes[] = 'icon-last';
202
}
203
204
if ($this->disabled) {
205
$classes[] = 'disabled';
206
}
207
208
if ($this->selected) {
209
$classes[] = 'selected';
210
}
211
212
switch ($this->getButtonType()) {
213
case self::BUTTONTYPE_DEFAULT:
214
$classes[] = 'phui-button-default';
215
break;
216
case self::BUTTONTYPE_SIMPLE:
217
$classes[] = 'phui-button-simple';
218
break;
219
}
220
221
$sigil = null;
222
$meta = null;
223
if ($this->tooltip) {
224
Javelin::initBehavior('phabricator-tooltips');
225
require_celerity_resource('aphront-tooltip-css');
226
$sigil = 'has-tooltip';
227
$meta = array(
228
'tip' => $this->tooltip,
229
);
230
}
231
232
if ($this->noCSS) {
233
$classes = array();
234
}
235
236
// See PHI823. If we aren't rendering a "<button>" or "<input>" tag,
237
// give the tag we are rendering a "button" role as a hint to screen
238
// readers.
239
$role = null;
240
if ($this->tag !== 'button' && $this->tag !== 'input') {
241
$role = 'button';
242
}
243
244
$attrs = array(
245
'class' => $classes,
246
'href' => $this->href,
247
'name' => $this->name,
248
'title' => $this->title,
249
'sigil' => $sigil,
250
'meta' => $meta,
251
'role' => $role,
252
);
253
254
if ($this->tag == 'input') {
255
$attrs['type'] = 'submit';
256
$attrs['value'] = $this->text;
257
}
258
259
return $attrs;
260
}
261
262
protected function getTagContent() {
263
if ($this->tag === 'input') {
264
return null;
265
}
266
267
$icon = $this->icon;
268
$text = null;
269
$subtext = null;
270
271
if ($this->subtext) {
272
$subtext = phutil_tag(
273
'div',
274
array(
275
'class' => 'phui-button-subtext',
276
),
277
$this->subtext);
278
}
279
280
if ($this->text !== null) {
281
$text = phutil_tag(
282
'div',
283
array(
284
'class' => 'phui-button-text',
285
),
286
array(
287
$this->text,
288
$subtext,
289
));
290
}
291
292
$caret = null;
293
if ($this->dropdown || $this->getHasCaret()) {
294
$caret = phutil_tag('span', array('class' => 'caret'), '');
295
}
296
297
$aural = null;
298
if ($this->auralLabel !== null) {
299
$aural = phutil_tag(
300
'span',
301
array(
302
'class' => 'aural-only',
303
),
304
$this->auralLabel);
305
}
306
307
308
if ($this->iconFirst == true) {
309
return array($aural, $icon, $text, $caret);
310
} else {
311
return array($aural, $text, $icon, $caret);
312
}
313
}
314
}
315
316