/src/view/phui/PHUIListItemView.php

https://github.com/dannysu/phabricator · PHP · 334 lines · 268 code · 62 blank · 4 comment · 17 complexity · 4855fe48c02db7bae6cc27b7be2cc30e MD5 · raw file

  1. <?php
  2. final class PHUIListItemView extends AphrontTagView {
  3. const TYPE_LINK = 'type-link';
  4. const TYPE_SPACER = 'type-spacer';
  5. const TYPE_LABEL = 'type-label';
  6. const TYPE_BUTTON = 'type-button';
  7. const TYPE_CUSTOM = 'type-custom';
  8. const TYPE_DIVIDER = 'type-divider';
  9. const TYPE_ICON = 'type-icon';
  10. const STATUS_WARN = 'phui-list-item-warn';
  11. const STATUS_FAIL = 'phui-list-item-fail';
  12. private $name;
  13. private $href;
  14. private $type = self::TYPE_LINK;
  15. private $isExternal;
  16. private $key;
  17. private $icon;
  18. private $selected;
  19. private $disabled;
  20. private $renderNameAsTooltip;
  21. private $statusColor;
  22. private $order;
  23. private $aural;
  24. private $profileImage;
  25. private $indented;
  26. private $hideInApplicationMenu;
  27. private $icons = array();
  28. private $openInNewWindow = false;
  29. private $tooltip;
  30. public function setOpenInNewWindow($open_in_new_window) {
  31. $this->openInNewWindow = $open_in_new_window;
  32. return $this;
  33. }
  34. public function getOpenInNewWindow() {
  35. return $this->openInNewWindow;
  36. }
  37. public function setHideInApplicationMenu($hide) {
  38. $this->hideInApplicationMenu = $hide;
  39. return $this;
  40. }
  41. public function getHideInApplicationMenu() {
  42. return $this->hideInApplicationMenu;
  43. }
  44. public function setDropdownMenu(PhabricatorActionListView $actions) {
  45. Javelin::initBehavior('phui-dropdown-menu');
  46. $this->addSigil('phui-dropdown-menu');
  47. $this->setMetadata($actions->getDropdownMenuMetadata());
  48. return $this;
  49. }
  50. public function setAural($aural) {
  51. $this->aural = $aural;
  52. return $this;
  53. }
  54. public function getAural() {
  55. return $this->aural;
  56. }
  57. public function setOrder($order) {
  58. $this->order = $order;
  59. return $this;
  60. }
  61. public function getOrder() {
  62. return $this->order;
  63. }
  64. public function setRenderNameAsTooltip($render_name_as_tooltip) {
  65. $this->renderNameAsTooltip = $render_name_as_tooltip;
  66. return $this;
  67. }
  68. public function getRenderNameAsTooltip() {
  69. return $this->renderNameAsTooltip;
  70. }
  71. public function setSelected($selected) {
  72. $this->selected = $selected;
  73. return $this;
  74. }
  75. public function getSelected() {
  76. return $this->selected;
  77. }
  78. public function setIcon($icon) {
  79. $this->icon = $icon;
  80. return $this;
  81. }
  82. public function setProfileImage($image) {
  83. $this->profileImage = $image;
  84. return $this;
  85. }
  86. public function getIcon() {
  87. return $this->icon;
  88. }
  89. public function setIndented($indented) {
  90. $this->indented = $indented;
  91. return $this;
  92. }
  93. public function getIndented() {
  94. return $this->indented;
  95. }
  96. public function setKey($key) {
  97. $this->key = (string)$key;
  98. return $this;
  99. }
  100. public function getKey() {
  101. return $this->key;
  102. }
  103. public function setType($type) {
  104. $this->type = $type;
  105. return $this;
  106. }
  107. public function getType() {
  108. return $this->type;
  109. }
  110. public function setHref($href) {
  111. $this->href = $href;
  112. return $this;
  113. }
  114. public function getHref() {
  115. return $this->href;
  116. }
  117. public function setName($name) {
  118. $this->name = $name;
  119. return $this;
  120. }
  121. public function getName() {
  122. return $this->name;
  123. }
  124. public function setIsExternal($is_external) {
  125. $this->isExternal = $is_external;
  126. return $this;
  127. }
  128. public function getIsExternal() {
  129. return $this->isExternal;
  130. }
  131. public function setStatusColor($color) {
  132. $this->statusColor = $color;
  133. return $this;
  134. }
  135. public function addIcon($icon) {
  136. $this->icons[] = $icon;
  137. return $this;
  138. }
  139. public function getIcons() {
  140. return $this->icons;
  141. }
  142. public function setTooltip($tooltip) {
  143. $this->tooltip = $tooltip;
  144. return $this;
  145. }
  146. protected function getTagName() {
  147. return 'li';
  148. }
  149. protected function getTagAttributes() {
  150. $classes = array();
  151. $classes[] = 'phui-list-item-view';
  152. $classes[] = 'phui-list-item-'.$this->type;
  153. if ($this->icon || $this->profileImage) {
  154. $classes[] = 'phui-list-item-has-icon';
  155. }
  156. if ($this->selected) {
  157. $classes[] = 'phui-list-item-selected';
  158. }
  159. if ($this->disabled) {
  160. $classes[] = 'phui-list-item-disabled';
  161. }
  162. if ($this->statusColor) {
  163. $classes[] = $this->statusColor;
  164. }
  165. return array(
  166. 'class' => implode(' ', $classes),
  167. );
  168. }
  169. public function setDisabled($disabled) {
  170. $this->disabled = $disabled;
  171. return $this;
  172. }
  173. public function getDisabled() {
  174. return $this->disabled;
  175. }
  176. protected function getTagContent() {
  177. $name = null;
  178. $icon = null;
  179. $meta = null;
  180. $sigil = null;
  181. if ($this->name) {
  182. if ($this->getRenderNameAsTooltip()) {
  183. Javelin::initBehavior('phabricator-tooltips');
  184. $sigil = 'has-tooltip';
  185. $meta = array(
  186. 'tip' => $this->name,
  187. 'align' => 'E',
  188. );
  189. } else {
  190. if ($this->tooltip) {
  191. Javelin::initBehavior('phabricator-tooltips');
  192. $sigil = 'has-tooltip';
  193. $meta = array(
  194. 'tip' => $this->tooltip,
  195. 'align' => 'E',
  196. 'size' => 300,
  197. );
  198. }
  199. $external = null;
  200. if ($this->isExternal) {
  201. $external = " \xE2\x86\x97";
  202. }
  203. // If this element has an aural representation, make any name visual
  204. // only. This is primarily dealing with the links in the main menu like
  205. // "Profile" and "Logout". If we don't hide the name, the mobile
  206. // version of these elements will have two redundant names.
  207. $classes = array();
  208. $classes[] = 'phui-list-item-name';
  209. if ($this->aural !== null) {
  210. $classes[] = 'visual-only';
  211. }
  212. $name = phutil_tag(
  213. 'span',
  214. array(
  215. 'class' => implode(' ', $classes),
  216. ),
  217. array(
  218. $this->name,
  219. $external,
  220. ));
  221. }
  222. }
  223. $aural = null;
  224. if ($this->aural !== null) {
  225. $aural = javelin_tag(
  226. 'span',
  227. array(
  228. 'aural' => true,
  229. ),
  230. $this->aural);
  231. }
  232. if ($this->icon) {
  233. $icon_name = $this->icon;
  234. if ($this->getDisabled()) {
  235. $icon_name .= ' grey';
  236. }
  237. $icon = id(new PHUIIconView())
  238. ->addClass('phui-list-item-icon')
  239. ->setIcon($icon_name);
  240. }
  241. if ($this->profileImage) {
  242. $icon = id(new PHUIIconView())
  243. ->setHeadSize(PHUIIconView::HEAD_SMALL)
  244. ->addClass('phui-list-item-icon')
  245. ->setImage($this->profileImage);
  246. }
  247. $classes = array();
  248. if ($this->href) {
  249. $classes[] = 'phui-list-item-href';
  250. }
  251. if ($this->indented) {
  252. $classes[] = 'phui-list-item-indented';
  253. }
  254. $icons = $this->getIcons();
  255. return javelin_tag(
  256. $this->href ? 'a' : 'div',
  257. array(
  258. 'href' => $this->href,
  259. 'class' => implode(' ', $classes),
  260. 'meta' => $meta,
  261. 'sigil' => $sigil,
  262. 'target' => $this->getOpenInNewWindow() ? '_blank' : null,
  263. ),
  264. array(
  265. $aural,
  266. $icon,
  267. $icons,
  268. $this->renderChildren(),
  269. $name,
  270. ));
  271. }
  272. }