PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/view/phui/PHUITagView.php

http://github.com/facebook/phabricator
PHP | 318 lines | 262 code | 51 blank | 5 comment | 17 complexity | dced6e00c7f9bdf523e4413f499126df MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT, LGPL-2.1, LGPL-3.0
  1. <?php
  2. final class PHUITagView extends AphrontTagView {
  3. const TYPE_PERSON = 'person';
  4. const TYPE_OBJECT = 'object';
  5. const TYPE_STATE = 'state';
  6. const TYPE_SHADE = 'shade';
  7. const TYPE_OUTLINE = 'outline';
  8. const COLOR_RED = 'red';
  9. const COLOR_ORANGE = 'orange';
  10. const COLOR_YELLOW = 'yellow';
  11. const COLOR_BLUE = 'blue';
  12. const COLOR_INDIGO = 'indigo';
  13. const COLOR_VIOLET = 'violet';
  14. const COLOR_GREEN = 'green';
  15. const COLOR_BLACK = 'black';
  16. const COLOR_SKY = 'sky';
  17. const COLOR_FIRE = 'fire';
  18. const COLOR_GREY = 'grey';
  19. const COLOR_WHITE = 'white';
  20. const COLOR_PINK = 'pink';
  21. const COLOR_BLUEGREY = 'bluegrey';
  22. const COLOR_CHECKERED = 'checkered';
  23. const COLOR_DISABLED = 'disabled';
  24. const COLOR_PLACEHOLDER = 'placeholder';
  25. const COLOR_OBJECT = 'object';
  26. const COLOR_PERSON = 'person';
  27. const BORDER_NONE = 'border-none';
  28. private $type;
  29. private $href;
  30. private $name;
  31. private $phid;
  32. private $color;
  33. private $backgroundColor;
  34. private $dotColor;
  35. private $closed;
  36. private $external;
  37. private $icon;
  38. private $shade;
  39. private $slimShady;
  40. private $border;
  41. public function setType($type) {
  42. $this->type = $type;
  43. switch ($type) {
  44. case self::TYPE_SHADE:
  45. case self::TYPE_OUTLINE:
  46. break;
  47. case self::TYPE_OBJECT:
  48. $this->setBackgroundColor(self::COLOR_OBJECT);
  49. break;
  50. case self::TYPE_PERSON:
  51. $this->setBackgroundColor(self::COLOR_PERSON);
  52. break;
  53. }
  54. return $this;
  55. }
  56. /**
  57. * This method has been deprecated, use @{method:setColor} instead.
  58. *
  59. * @deprecated
  60. */
  61. public function setShade($shade) {
  62. phlog(
  63. pht('Deprecated call to setShade(), use setColor() instead.'));
  64. $this->color = $shade;
  65. return $this;
  66. }
  67. public function setColor($color) {
  68. $this->color = $color;
  69. return $this;
  70. }
  71. public function setDotColor($dot_color) {
  72. $this->dotColor = $dot_color;
  73. return $this;
  74. }
  75. public function setBackgroundColor($background_color) {
  76. $this->backgroundColor = $background_color;
  77. return $this;
  78. }
  79. public function setPHID($phid) {
  80. $this->phid = $phid;
  81. return $this;
  82. }
  83. public function setName($name) {
  84. $this->name = $name;
  85. return $this;
  86. }
  87. public function setHref($href) {
  88. $this->href = $href;
  89. return $this;
  90. }
  91. public function setClosed($closed) {
  92. $this->closed = $closed;
  93. return $this;
  94. }
  95. public function setBorder($border) {
  96. $this->border = $border;
  97. return $this;
  98. }
  99. public function setIcon($icon) {
  100. $this->icon = $icon;
  101. return $this;
  102. }
  103. public function setSlimShady($is_eminem) {
  104. $this->slimShady = $is_eminem;
  105. return $this;
  106. }
  107. protected function getTagName() {
  108. return strlen($this->href) ? 'a' : 'span';
  109. }
  110. protected function getTagAttributes() {
  111. require_celerity_resource('phui-tag-view-css');
  112. $classes = array(
  113. 'phui-tag-view',
  114. 'phui-tag-type-'.$this->type,
  115. );
  116. if ($this->color) {
  117. $classes[] = 'phui-tag-'.$this->color;
  118. }
  119. if ($this->slimShady) {
  120. $classes[] = 'phui-tag-slim';
  121. }
  122. if ($this->type == self::TYPE_SHADE) {
  123. $classes[] = 'phui-tag-shade';
  124. }
  125. if ($this->icon) {
  126. $classes[] = 'phui-tag-icon-view';
  127. }
  128. if ($this->border) {
  129. $classes[] = 'phui-tag-'.$this->border;
  130. }
  131. $attributes = array(
  132. 'href' => $this->href,
  133. 'class' => $classes,
  134. );
  135. if ($this->external) {
  136. $attributes += array(
  137. 'target' => '_blank',
  138. 'rel' => 'noreferrer',
  139. );
  140. }
  141. if ($this->phid) {
  142. Javelin::initBehavior('phui-hovercards');
  143. $attributes += array(
  144. 'sigil' => 'hovercard',
  145. 'meta' => array(
  146. 'hoverPHID' => $this->phid,
  147. ),
  148. );
  149. }
  150. return $attributes;
  151. }
  152. protected function getTagContent() {
  153. if (!$this->type) {
  154. throw new PhutilInvalidStateException('setType', 'render');
  155. }
  156. $color = null;
  157. if (!$this->shade && $this->backgroundColor) {
  158. $color = 'phui-tag-color-'.$this->backgroundColor;
  159. }
  160. if ($this->dotColor) {
  161. $dotcolor = 'phui-tag-color-'.$this->dotColor;
  162. $dot = phutil_tag(
  163. 'span',
  164. array(
  165. 'class' => 'phui-tag-dot '.$dotcolor,
  166. ),
  167. '');
  168. } else {
  169. $dot = null;
  170. }
  171. if ($this->icon) {
  172. $icon = id(new PHUIIconView())
  173. ->setIcon($this->icon);
  174. } else {
  175. $icon = null;
  176. }
  177. $content = phutil_tag(
  178. 'span',
  179. array(
  180. 'class' => 'phui-tag-core '.$color,
  181. ),
  182. array($dot, $icon, $this->name));
  183. if ($this->closed) {
  184. $content = phutil_tag(
  185. 'span',
  186. array(
  187. 'class' => 'phui-tag-core-closed',
  188. ),
  189. array($icon, $content));
  190. }
  191. return $content;
  192. }
  193. public static function getTagTypes() {
  194. return array(
  195. self::TYPE_PERSON,
  196. self::TYPE_OBJECT,
  197. self::TYPE_STATE,
  198. );
  199. }
  200. public static function getColors() {
  201. return array(
  202. self::COLOR_RED,
  203. self::COLOR_ORANGE,
  204. self::COLOR_YELLOW,
  205. self::COLOR_BLUE,
  206. self::COLOR_INDIGO,
  207. self::COLOR_VIOLET,
  208. self::COLOR_GREEN,
  209. self::COLOR_BLACK,
  210. self::COLOR_GREY,
  211. self::COLOR_WHITE,
  212. self::COLOR_OBJECT,
  213. self::COLOR_PERSON,
  214. );
  215. }
  216. public static function getShades() {
  217. return array_keys(self::getShadeMap());
  218. }
  219. public static function getShadeMap() {
  220. return array(
  221. self::COLOR_RED => pht('Red'),
  222. self::COLOR_ORANGE => pht('Orange'),
  223. self::COLOR_YELLOW => pht('Yellow'),
  224. self::COLOR_BLUE => pht('Blue'),
  225. self::COLOR_INDIGO => pht('Indigo'),
  226. self::COLOR_VIOLET => pht('Violet'),
  227. self::COLOR_GREEN => pht('Green'),
  228. self::COLOR_GREY => pht('Grey'),
  229. self::COLOR_PINK => pht('Pink'),
  230. self::COLOR_CHECKERED => pht('Checkered'),
  231. self::COLOR_DISABLED => pht('Disabled'),
  232. );
  233. }
  234. public static function getShadeName($shade) {
  235. return idx(self::getShadeMap(), $shade, $shade);
  236. }
  237. public static function getOutlines() {
  238. return array_keys(self::getOutlineMap());
  239. }
  240. public static function getOutlineMap() {
  241. return array(
  242. self::COLOR_RED => pht('Red'),
  243. self::COLOR_ORANGE => pht('Orange'),
  244. self::COLOR_YELLOW => pht('Yellow'),
  245. self::COLOR_BLUE => pht('Blue'),
  246. self::COLOR_INDIGO => pht('Indigo'),
  247. self::COLOR_VIOLET => pht('Violet'),
  248. self::COLOR_GREEN => pht('Green'),
  249. self::COLOR_GREY => pht('Grey'),
  250. self::COLOR_PINK => pht('Pink'),
  251. self::COLOR_SKY => pht('Sky'),
  252. self::COLOR_FIRE => pht('Fire'),
  253. self::COLOR_BLACK => pht('Black'),
  254. self::COLOR_DISABLED => pht('Disabled'),
  255. );
  256. }
  257. public static function getOutlineName($outline) {
  258. return idx(self::getOutlineMap(), $outline, $outline);
  259. }
  260. public function setExternal($external) {
  261. $this->external = $external;
  262. return $this;
  263. }
  264. public function getExternal() {
  265. return $this->external;
  266. }
  267. }