Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / view / phui / PHUIButtonView.php
blob2231649f1215323aab27e45285d0a8adea9d786e
1 <?php
3 final class PHUIButtonView extends AphrontTagView {
5 const GREEN = 'green';
6 const GREY = 'grey';
7 const BLUE = 'blue';
8 const RED = 'red';
9 const DISABLED = 'disabled';
11 const SMALL = 'small';
12 const BIG = 'big';
14 const BUTTONTYPE_DEFAULT = 'buttontype.default';
15 const BUTTONTYPE_SIMPLE = 'buttontype.simple';
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;
36 public function setName($name) {
37 $this->name = $name;
38 return $this;
41 public function getName() {
42 return $this->name;
45 public function setText($text) {
46 $this->text = $text;
47 return $this;
50 public function setHref($href) {
51 $this->href = $href;
52 return $this;
55 public function setTitle($title) {
56 $this->title = $title;
57 return $this;
60 public function setSubtext($subtext) {
61 $this->subtext = $subtext;
62 return $this;
65 public function setColor($color) {
66 $this->color = $color;
67 return $this;
70 public function getColor() {
71 return $this->color;
74 public function setDisabled($disabled) {
75 $this->disabled = $disabled;
76 return $this;
79 public function setSelected($selected) {
80 $this->selected = $selected;
81 return $this;
84 public function setTag($tag) {
85 $this->tag = $tag;
86 return $this;
89 public function setSize($size) {
90 $this->size = $size;
91 return $this;
94 public function setDropdown($dd) {
95 $this->dropdown = $dd;
96 return $this;
99 public function setTooltip($text) {
100 $this->tooltip = $text;
101 return $this;
104 public function setNoCSS($no_css) {
105 $this->noCSS = $no_css;
106 return $this;
109 public function setHasCaret($has_caret) {
110 $this->hasCaret = $has_caret;
111 return $this;
114 public function getHasCaret() {
115 return $this->hasCaret;
118 public function setButtonType($button_type) {
119 $this->buttonType = $button_type;
120 return $this;
123 public function getButtonType() {
124 return $this->buttonType;
127 public function setAuralLabel($aural_label) {
128 $this->auralLabel = $aural_label;
129 return $this;
132 public function getAuralLabel() {
133 return $this->auralLabel;
136 public function setIcon($icon, $first = true) {
137 if (!($icon instanceof PHUIIconView)) {
138 $icon = id(new PHUIIconView())
139 ->setIcon($icon);
141 $this->icon = $icon;
142 $this->iconFirst = $first;
143 return $this;
146 protected function getTagName() {
147 return $this->tag;
150 public function setDropdownMenu(PhabricatorActionListView $actions) {
151 Javelin::initBehavior('phui-dropdown-menu');
153 $this->addSigil('phui-dropdown-menu');
154 $this->setDropdown(true);
155 $this->setMetadata($actions->getDropdownMenuMetadata());
157 return $this;
160 public function setDropdownMenuID($id) {
161 Javelin::initBehavior('phui-dropdown-menu');
163 $this->addSigil('phui-dropdown-menu');
164 $this->setMetadata(
165 array(
166 'menuID' => $id,
169 return $this;
172 protected function getTagAttributes() {
174 require_celerity_resource('phui-button-css');
175 require_celerity_resource('phui-button-simple-css');
177 $classes = array();
178 $classes[] = 'button';
180 if ($this->color) {
181 $classes[] = 'button-'.$this->color;
184 if ($this->size) {
185 $classes[] = $this->size;
188 if ($this->dropdown) {
189 $classes[] = 'dropdown';
192 if ($this->icon) {
193 $classes[] = 'has-icon';
196 if ($this->text !== null) {
197 $classes[] = 'has-text';
200 if ($this->iconFirst == false) {
201 $classes[] = 'icon-last';
204 if ($this->disabled) {
205 $classes[] = 'disabled';
208 if ($this->selected) {
209 $classes[] = 'selected';
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;
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,
232 if ($this->noCSS) {
233 $classes = array();
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';
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,
254 if ($this->tag == 'input') {
255 $attrs['type'] = 'submit';
256 $attrs['value'] = $this->text;
259 return $attrs;
262 protected function getTagContent() {
263 if ($this->tag === 'input') {
264 return null;
267 $icon = $this->icon;
268 $text = null;
269 $subtext = null;
271 if ($this->subtext) {
272 $subtext = phutil_tag(
273 'div',
274 array(
275 'class' => 'phui-button-subtext',
277 $this->subtext);
280 if ($this->text !== null) {
281 $text = phutil_tag(
282 'div',
283 array(
284 'class' => 'phui-button-text',
286 array(
287 $this->text,
288 $subtext,
292 $caret = null;
293 if ($this->dropdown || $this->getHasCaret()) {
294 $caret = phutil_tag('span', array('class' => 'caret'), '');
297 $aural = null;
298 if ($this->auralLabel !== null) {
299 $aural = phutil_tag(
300 'span',
301 array(
302 'class' => 'aural-only',
304 $this->auralLabel);
308 if ($this->iconFirst == true) {
309 return array($aural, $icon, $text, $caret);
310 } else {
311 return array($aural, $text, $icon, $caret);