PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/view/phui/PHUITagView.php

https://github.com/navyuginfo/phabricator
PHP | 208 lines | 178 code | 30 blank | 0 comment | 10 complexity | 1065dcc2ea92a5adda800af0b9e3a329 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, LGPL-3.0, MIT, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. final class PHUITagView extends AphrontView {
  3. const TYPE_PERSON = 'person';
  4. const TYPE_OBJECT = 'object';
  5. const TYPE_STATE = 'state';
  6. const COLOR_RED = 'red';
  7. const COLOR_ORANGE = 'orange';
  8. const COLOR_YELLOW = 'yellow';
  9. const COLOR_BLUE = 'blue';
  10. const COLOR_INDIGO = 'indigo';
  11. const COLOR_VIOLET = 'violet';
  12. const COLOR_GREEN = 'green';
  13. const COLOR_BLACK = 'black';
  14. const COLOR_GREY = 'grey';
  15. const COLOR_WHITE = 'white';
  16. const COLOR_OBJECT = 'object';
  17. const COLOR_PERSON = 'person';
  18. private $type;
  19. private $href;
  20. private $name;
  21. private $phid;
  22. private $backgroundColor;
  23. private $dotColor;
  24. private $closed;
  25. private $external;
  26. private $id;
  27. private $icon;
  28. public function setID($id) {
  29. $this->id = $id;
  30. return $this;
  31. }
  32. public function getID() {
  33. return $this->id;
  34. }
  35. public function setType($type) {
  36. $this->type = $type;
  37. switch ($type) {
  38. case self::TYPE_OBJECT:
  39. $this->setBackgroundColor(self::COLOR_OBJECT);
  40. break;
  41. case self::TYPE_PERSON:
  42. $this->setBackgroundColor(self::COLOR_PERSON);
  43. break;
  44. }
  45. return $this;
  46. }
  47. public function setDotColor($dot_color) {
  48. $this->dotColor = $dot_color;
  49. return $this;
  50. }
  51. public function setBackgroundColor($background_color) {
  52. $this->backgroundColor = $background_color;
  53. return $this;
  54. }
  55. public function setPHID($phid) {
  56. $this->phid = $phid;
  57. return $this;
  58. }
  59. public function setName($name) {
  60. $this->name = $name;
  61. return $this;
  62. }
  63. public function setHref($href) {
  64. $this->href = $href;
  65. return $this;
  66. }
  67. public function setClosed($closed) {
  68. $this->closed = $closed;
  69. return $this;
  70. }
  71. public function setIcon($icon) {
  72. $icon_view = id(new PHUIIconView())
  73. ->setIconFont($icon);
  74. $this->icon = $icon_view;
  75. return $this;
  76. }
  77. public function render() {
  78. if (!$this->type) {
  79. throw new Exception(pht('You must call setType() before render()!'));
  80. }
  81. require_celerity_resource('phui-tag-view-css');
  82. $classes = array(
  83. 'phui-tag-view',
  84. 'phui-tag-type-'.$this->type,
  85. );
  86. $color = null;
  87. if ($this->backgroundColor) {
  88. $color = 'phui-tag-color-'.$this->backgroundColor;
  89. }
  90. if ($this->dotColor) {
  91. $dotcolor = 'phui-tag-color-'.$this->dotColor;
  92. $dot = phutil_tag(
  93. 'span',
  94. array(
  95. 'class' => 'phui-tag-dot '.$dotcolor,
  96. ),
  97. '');
  98. } else {
  99. $dot = null;
  100. }
  101. if ($this->icon) {
  102. $icon = $this->icon;
  103. $classes[] = 'phui-tag-icon-view';
  104. } else {
  105. $icon = null;
  106. }
  107. $content = phutil_tag(
  108. 'span',
  109. array(
  110. 'class' => 'phui-tag-core '.$color,
  111. ),
  112. array($dot, $this->name));
  113. if ($this->closed) {
  114. $content = phutil_tag(
  115. 'span',
  116. array(
  117. 'class' => 'phui-tag-core-closed',
  118. ),
  119. $content);
  120. }
  121. if ($this->phid) {
  122. Javelin::initBehavior('phabricator-hovercards');
  123. return javelin_tag(
  124. 'a',
  125. array(
  126. 'id' => $this->id,
  127. 'href' => $this->href,
  128. 'class' => implode(' ', $classes),
  129. 'sigil' => 'hovercard',
  130. 'meta' => array(
  131. 'hoverPHID' => $this->phid,
  132. ),
  133. 'target' => $this->external ? '_blank' : null,
  134. ),
  135. array($icon, $content));
  136. } else {
  137. return phutil_tag(
  138. $this->href ? 'a' : 'span',
  139. array(
  140. 'id' => $this->id,
  141. 'href' => $this->href,
  142. 'class' => implode(' ', $classes),
  143. 'target' => $this->external ? '_blank' : null,
  144. ),
  145. array($icon, $content));
  146. }
  147. }
  148. public static function getTagTypes() {
  149. return array(
  150. self::TYPE_PERSON,
  151. self::TYPE_OBJECT,
  152. self::TYPE_STATE,
  153. );
  154. }
  155. public static function getColors() {
  156. return array(
  157. self::COLOR_RED,
  158. self::COLOR_ORANGE,
  159. self::COLOR_YELLOW,
  160. self::COLOR_BLUE,
  161. self::COLOR_INDIGO,
  162. self::COLOR_VIOLET,
  163. self::COLOR_GREEN,
  164. self::COLOR_BLACK,
  165. self::COLOR_GREY,
  166. self::COLOR_WHITE,
  167. self::COLOR_OBJECT,
  168. self::COLOR_PERSON,
  169. );
  170. }
  171. public function setExternal($external) {
  172. $this->external = $external;
  173. return $this;
  174. }
  175. public function getExternal() {
  176. return $this->external;
  177. }
  178. }