[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PHUIObjectItemView extends AphrontTagView { 4 5 private $objectName; 6 private $header; 7 private $subhead; 8 private $href; 9 private $attributes = array(); 10 private $icons = array(); 11 private $barColor; 12 private $object; 13 private $effect; 14 private $footIcons = array(); 15 private $handleIcons = array(); 16 private $bylines = array(); 17 private $grippable; 18 private $actions = array(); 19 private $headIcons = array(); 20 private $disabled; 21 private $imageURI; 22 private $state; 23 private $fontIcon; 24 private $imageIcon; 25 26 const AGE_FRESH = 'fresh'; 27 const AGE_STALE = 'stale'; 28 const AGE_OLD = 'old'; 29 30 const STATE_SUCCESS = 'green'; 31 const STATE_FAIL = 'red'; 32 const STATE_WARN = 'yellow'; 33 const STATE_NOTE = 'blue'; 34 const STATE_BUILD = 'sky'; 35 36 public function setDisabled($disabled) { 37 $this->disabled = $disabled; 38 return $this; 39 } 40 41 public function getDisabled() { 42 return $this->disabled; 43 } 44 45 public function addHeadIcon($icon) { 46 $this->headIcons[] = $icon; 47 return $this; 48 } 49 50 public function setObjectName($name) { 51 $this->objectName = $name; 52 return $this; 53 } 54 55 public function setGrippable($grippable) { 56 $this->grippable = $grippable; 57 return $this; 58 } 59 60 public function getGrippable() { 61 return $this->grippable; 62 } 63 64 public function setEffect($effect) { 65 $this->effect = $effect; 66 return $this; 67 } 68 69 public function getEffect() { 70 return $this->effect; 71 } 72 73 public function setObject($object) { 74 $this->object = $object; 75 return $this; 76 } 77 78 public function getObject() { 79 return $this->object; 80 } 81 82 public function setHref($href) { 83 $this->href = $href; 84 return $this; 85 } 86 87 public function getHref() { 88 return $this->href; 89 } 90 91 public function setHeader($header) { 92 $this->header = $header; 93 return $this; 94 } 95 96 public function setSubHead($subhead) { 97 $this->subhead = $subhead; 98 return $this; 99 } 100 101 public function getHeader() { 102 return $this->header; 103 } 104 105 public function addByline($byline) { 106 $this->bylines[] = $byline; 107 return $this; 108 } 109 110 public function setImageURI($image_uri) { 111 $this->imageURI = $image_uri; 112 return $this; 113 } 114 115 public function getImageURI() { 116 return $this->imageURI; 117 } 118 119 public function setImageIcon($image_icon) { 120 $this->imageIcon = $image_icon; 121 return $this; 122 } 123 124 public function getImageIcon() { 125 return $this->imageIcon; 126 } 127 128 public function setState($state) { 129 $this->state = $state; 130 switch ($state) { 131 case self::STATE_SUCCESS: 132 $fi = 'fa-check-circle green'; 133 break; 134 case self::STATE_FAIL: 135 $fi = 'fa-times-circle red'; 136 break; 137 case self::STATE_WARN: 138 $fi = 'fa-exclamation-circle yellow'; 139 break; 140 case self::STATE_NOTE: 141 $fi = 'fa-info-circle blue'; 142 break; 143 case self::STATE_BUILD: 144 $fi = 'fa-refresh ph-spin sky'; 145 break; 146 } 147 $this->fontIcon = id(new PHUIIconView()) 148 ->setIconFont($fi.' fa-2x'); 149 return $this; 150 } 151 152 public function setEpoch($epoch, $age = self::AGE_FRESH) { 153 $date = phabricator_datetime($epoch, $this->getUser()); 154 155 $days = floor((time() - $epoch) / 60 / 60 / 24); 156 157 switch ($age) { 158 case self::AGE_FRESH: 159 $this->addIcon('none', $date); 160 break; 161 case self::AGE_STALE: 162 $attr = array( 163 'tip' => pht('Stale (%s day(s))', new PhutilNumber($days)), 164 'class' => 'icon-age-stale', 165 ); 166 167 $this->addIcon('fa-clock-o yellow', $date, $attr); 168 break; 169 case self::AGE_OLD: 170 $attr = array( 171 'tip' => pht('Old (%s day(s))', new PhutilNumber($days)), 172 'class' => 'icon-age-old', 173 ); 174 $this->addIcon('fa-clock-o red', $date, $attr); 175 break; 176 default: 177 throw new Exception("Unknown age '{$age}'!"); 178 } 179 180 return $this; 181 } 182 183 public function addAction(PHUIListItemView $action) { 184 if (count($this->actions) >= 3) { 185 throw new Exception('Limit 3 actions per item.'); 186 } 187 $this->actions[] = $action; 188 return $this; 189 } 190 191 public function addIcon($icon, $label = null, $attributes = array()) { 192 $this->icons[] = array( 193 'icon' => $icon, 194 'label' => $label, 195 'attributes' => $attributes, 196 ); 197 return $this; 198 } 199 200 public function addFootIcon($icon, $label = null) { 201 $this->footIcons[] = array( 202 'icon' => $icon, 203 'label' => $label, 204 ); 205 return $this; 206 } 207 208 public function addHandleIcon( 209 PhabricatorObjectHandle $handle, 210 $label = null) { 211 $this->handleIcons[] = array( 212 'icon' => $handle, 213 'label' => $label, 214 ); 215 return $this; 216 } 217 218 public function setBarColor($bar_color) { 219 $this->barColor = $bar_color; 220 return $this; 221 } 222 223 public function getBarColor() { 224 return $this->barColor; 225 } 226 227 public function addAttribute($attribute) { 228 if (!empty($attribute)) { 229 $this->attributes[] = $attribute; 230 } 231 return $this; 232 } 233 234 protected function getTagName() { 235 return 'li'; 236 } 237 238 protected function getTagAttributes() { 239 $item_classes = array(); 240 $item_classes[] = 'phui-object-item'; 241 242 if ($this->icons) { 243 $item_classes[] = 'phui-object-item-with-icons'; 244 } 245 246 if ($this->attributes) { 247 $item_classes[] = 'phui-object-item-with-attrs'; 248 } 249 250 if ($this->handleIcons) { 251 $item_classes[] = 'phui-object-item-with-handle-icons'; 252 } 253 254 if ($this->barColor) { 255 $item_classes[] = 'phui-object-item-bar-color-'.$this->barColor; 256 } 257 258 if ($this->footIcons) { 259 $item_classes[] = 'phui-object-item-with-foot-icons'; 260 } 261 262 if ($this->bylines) { 263 $item_classes[] = 'phui-object-item-with-bylines'; 264 } 265 266 if ($this->actions) { 267 $n = count($this->actions); 268 $item_classes[] = 'phui-object-item-with-actions'; 269 $item_classes[] = 'phui-object-item-with-'.$n.'-actions'; 270 } 271 272 if ($this->disabled) { 273 $item_classes[] = 'phui-object-item-disabled'; 274 } 275 276 if ($this->state) { 277 $item_classes[] = 'phui-object-item-state-'.$this->state; 278 } 279 280 switch ($this->effect) { 281 case 'highlighted': 282 $item_classes[] = 'phui-object-item-highlighted'; 283 break; 284 case 'selected': 285 $item_classes[] = 'phui-object-item-selected'; 286 break; 287 case null: 288 break; 289 default: 290 throw new Exception(pht('Invalid effect!')); 291 } 292 293 if ($this->getGrippable()) { 294 $item_classes[] = 'phui-object-item-grippable'; 295 } 296 297 if ($this->getImageURI()) { 298 $item_classes[] = 'phui-object-item-with-image'; 299 } 300 301 if ($this->getImageIcon()) { 302 $item_classes[] = 'phui-object-item-with-image-icon'; 303 } 304 305 if ($this->fontIcon) { 306 $item_classes[] = 'phui-object-item-with-ficon'; 307 } 308 309 return array( 310 'class' => $item_classes, 311 ); 312 } 313 314 public function getTagContent() { 315 $content_classes = array(); 316 $content_classes[] = 'phui-object-item-content'; 317 318 $header_name = null; 319 if ($this->objectName) { 320 $header_name = array( 321 phutil_tag( 322 'span', 323 array( 324 'class' => 'phui-object-item-objname', 325 ), 326 $this->objectName), 327 ' ', 328 ); 329 } 330 331 $header_link = phutil_tag( 332 $this->href ? 'a' : 'div', 333 array( 334 'href' => $this->href, 335 'class' => 'phui-object-item-link', 336 ), 337 $this->header); 338 339 $header = javelin_tag( 340 'div', 341 array( 342 'class' => 'phui-object-item-name', 343 'sigil' => 'slippery', 344 ), 345 array( 346 $this->headIcons, 347 $header_name, 348 $header_link, 349 )); 350 351 $icons = array(); 352 if ($this->icons) { 353 $icon_list = array(); 354 foreach ($this->icons as $spec) { 355 $icon = $spec['icon']; 356 $icon = id(new PHUIIconView()) 357 ->setIconFont($icon) 358 ->addClass('phui-object-item-icon-image'); 359 360 if (isset($spec['attributes']['tip'])) { 361 $sigil = 'has-tooltip'; 362 $meta = array( 363 'tip' => $spec['attributes']['tip'], 364 'align' => 'W', 365 ); 366 $icon->addSigil($sigil); 367 $icon->setMetadata($meta); 368 } 369 370 $label = phutil_tag( 371 'span', 372 array( 373 'class' => 'phui-object-item-icon-label', 374 ), 375 $spec['label']); 376 377 if (isset($spec['attributes']['href'])) { 378 $icon_href = phutil_tag( 379 'a', 380 array('href' => $spec['attributes']['href']), 381 array($label, $icon)); 382 } else { 383 $icon_href = array($label, $icon); 384 } 385 386 $classes = array(); 387 $classes[] = 'phui-object-item-icon'; 388 if ($spec['icon'] == 'none') { 389 $classes[] = 'phui-object-item-icon-none'; 390 } 391 if (isset($spec['attributes']['class'])) { 392 $classes[] = $spec['attributes']['class']; 393 } 394 395 $icon_list[] = javelin_tag( 396 'li', 397 array( 398 'class' => implode(' ', $classes), 399 ), 400 $icon_href); 401 } 402 403 $icons[] = phutil_tag( 404 'ul', 405 array( 406 'class' => 'phui-object-item-icons', 407 ), 408 $icon_list); 409 } 410 411 if ($this->handleIcons) { 412 $handle_bar = array(); 413 foreach ($this->handleIcons as $icon) { 414 $handle_bar[] = $this->renderHandleIcon($icon['icon'], $icon['label']); 415 } 416 $icons[] = phutil_tag( 417 'div', 418 array( 419 'class' => 'phui-object-item-handle-icons', 420 ), 421 $handle_bar); 422 } 423 424 $bylines = array(); 425 if ($this->bylines) { 426 foreach ($this->bylines as $byline) { 427 $bylines[] = phutil_tag( 428 'div', 429 array( 430 'class' => 'phui-object-item-byline', 431 ), 432 $byline); 433 } 434 $bylines = phutil_tag( 435 'div', 436 array( 437 'class' => 'phui-object-item-bylines', 438 ), 439 $bylines); 440 } 441 442 $subhead = null; 443 if ($this->subhead) { 444 $subhead = phutil_tag( 445 'div', 446 array( 447 'class' => 'phui-object-item-subhead', 448 ), 449 $this->subhead); 450 } 451 452 if ($icons) { 453 $icons = phutil_tag( 454 'div', 455 array( 456 'class' => 'phui-object-icon-pane', 457 ), 458 $icons); 459 } 460 461 $attrs = null; 462 if ($this->attributes) { 463 $attrs = array(); 464 $spacer = phutil_tag( 465 'span', 466 array( 467 'class' => 'phui-object-item-attribute-spacer', 468 ), 469 "\xC2\xB7"); 470 $first = true; 471 foreach ($this->attributes as $attribute) { 472 $attrs[] = phutil_tag( 473 'li', 474 array( 475 'class' => 'phui-object-item-attribute', 476 ), 477 array( 478 ($first ? null : $spacer), 479 $attribute, 480 )); 481 $first = false; 482 } 483 484 $attrs = phutil_tag( 485 'ul', 486 array( 487 'class' => 'phui-object-item-attributes', 488 ), 489 $attrs); 490 } 491 492 $foot = null; 493 if ($this->footIcons) { 494 $foot_bar = array(); 495 foreach ($this->footIcons as $icon) { 496 $foot_bar[] = $this->renderFootIcon($icon['icon'], $icon['label']); 497 } 498 $foot = phutil_tag( 499 'div', 500 array( 501 'class' => 'phui-object-item-foot-icons', 502 ), 503 $foot_bar); 504 } 505 506 $grippable = null; 507 if ($this->getGrippable()) { 508 $grippable = phutil_tag( 509 'div', 510 array( 511 'class' => 'phui-object-item-grip', 512 ), 513 ''); 514 } 515 516 $content = phutil_tag( 517 'div', 518 array( 519 'class' => implode(' ', $content_classes), 520 ), 521 array( 522 $subhead, 523 $attrs, 524 $this->renderChildren(), 525 $foot, 526 )); 527 528 $image = null; 529 if ($this->getImageURI()) { 530 $image = phutil_tag( 531 'div', 532 array( 533 'class' => 'phui-object-item-image', 534 'style' => 'background-image: url('.$this->getImageURI().')', 535 ), 536 ''); 537 } else if ($this->getImageIcon()) { 538 $image = phutil_tag( 539 'div', 540 array( 541 'class' => 'phui-object-item-image-icon', 542 ), 543 $this->getImageIcon()); 544 } 545 546 if ($image && $this->href) { 547 $image = phutil_tag( 548 'a', 549 array( 550 'href' => $this->href, 551 ), 552 $image); 553 } 554 555 $ficon = null; 556 if ($this->fontIcon) { 557 $image = phutil_tag( 558 'div', 559 array( 560 'class' => 'phui-object-item-ficon', 561 ), 562 $this->fontIcon); 563 } 564 565 $box = phutil_tag( 566 'div', 567 array( 568 'class' => 'phui-object-item-content-box', 569 ), 570 array( 571 $grippable, 572 $header, 573 $icons, 574 $bylines, 575 $content, 576 )); 577 578 $actions = array(); 579 if ($this->actions) { 580 Javelin::initBehavior('phabricator-tooltips'); 581 582 foreach (array_reverse($this->actions) as $action) { 583 $action->setRenderNameAsTooltip(true); 584 $actions[] = $action; 585 } 586 $actions = phutil_tag( 587 'ul', 588 array( 589 'class' => 'phui-object-item-actions', 590 ), 591 $actions); 592 } 593 594 return phutil_tag( 595 'div', 596 array( 597 'class' => 'phui-object-item-frame', 598 ), 599 array( 600 $actions, 601 $image, 602 $box, 603 )); 604 } 605 606 private function renderFootIcon($icon, $label) { 607 608 $icon = id(new PHUIIconView()) 609 ->setIconFont($icon); 610 611 $label = phutil_tag( 612 'span', 613 array( 614 ), 615 $label); 616 617 return phutil_tag( 618 'span', 619 array( 620 'class' => 'phui-object-item-foot-icon', 621 ), 622 array($icon, $label)); 623 } 624 625 626 private function renderHandleIcon(PhabricatorObjectHandle $handle, $label) { 627 Javelin::initBehavior('phabricator-tooltips'); 628 629 $options = array( 630 'class' => 'phui-object-item-handle-icon', 631 'style' => 'background-image: url('.$handle->getImageURI().')', 632 ); 633 634 if (strlen($label)) { 635 $options['sigil'] = 'has-tooltip'; 636 $options['meta'] = array('tip' => $label); 637 } 638 639 return javelin_tag( 640 'span', 641 $options, 642 ''); 643 } 644 645 646 647 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |