Path: blob/master/src/view/phui/PHUIPropertyListView.php
12249 views
<?php12final class PHUIPropertyListView extends AphrontView {34private $parts = array();5private $hasKeyboardShortcuts;6private $object;7private $invokedWillRenderEvent;8private $actionList = null;9private $classes = array();10private $stacked;1112const ICON_SUMMARY = 'fa-align-left';13const ICON_TESTPLAN = 'fa-file-text-o';1415protected function canAppendChild() {16return false;17}1819public function setObject($object) {20$this->object = $object;21return $this;22}2324public function setActionList(PhabricatorActionListView $list) {25$this->actionList = $list;26return $this;27}2829public function getActionList() {30return $this->actionList;31}3233public function setStacked($stacked) {34$this->stacked = $stacked;35return $this;36}3738public function addClass($class) {39$this->classes[] = $class;40return $this;41}4243public function setHasKeyboardShortcuts($has_keyboard_shortcuts) {44$this->hasKeyboardShortcuts = $has_keyboard_shortcuts;45return $this;46}4748public function addProperty($key, $value) {49$current = array_pop($this->parts);5051if (!$current || $current['type'] != 'property') {52if ($current) {53$this->parts[] = $current;54}55$current = array(56'type' => 'property',57'list' => array(),58);59}6061$current['list'][] = array(62'key' => $key,63'value' => $value,64);6566$this->parts[] = $current;67return $this;68}6970public function addSectionHeader($name, $icon = null) {71$this->parts[] = array(72'type' => 'section',73'name' => $name,74'icon' => $icon,75);76return $this;77}7879public function addTextContent($content) {80$this->parts[] = array(81'type' => 'text',82'content' => $content,83);84return $this;85}8687public function addRawContent($content) {88$this->parts[] = array(89'type' => 'raw',90'content' => $content,91);92return $this;93}9495public function addImageContent($content) {96$this->parts[] = array(97'type' => 'image',98'content' => $content,99);100return $this;101}102103public function invokeWillRenderEvent() {104if ($this->object && $this->getUser() && !$this->invokedWillRenderEvent) {105$event = new PhabricatorEvent(106PhabricatorEventType::TYPE_UI_WILLRENDERPROPERTIES,107array(108'object' => $this->object,109'view' => $this,110));111$event->setUser($this->getUser());112PhutilEventEngine::dispatchEvent($event);113}114$this->invokedWillRenderEvent = true;115}116117public function hasAnyProperties() {118$this->invokeWillRenderEvent();119120if ($this->parts) {121return true;122}123124return false;125}126127public function render() {128$this->invokeWillRenderEvent();129130require_celerity_resource('phui-property-list-view-css');131132$items = array();133134$parts = $this->parts;135136// If we have an action list, make sure we render a property part, even137// if there are no properties. Otherwise, the action list won't render.138if ($this->actionList) {139$this->classes[] = 'phui-property-list-has-actions';140$have_property_part = false;141foreach ($this->parts as $part) {142if ($part['type'] == 'property') {143$have_property_part = true;144break;145}146}147if (!$have_property_part) {148$parts[] = array(149'type' => 'property',150'list' => array(),151);152}153}154155foreach ($parts as $part) {156$type = $part['type'];157switch ($type) {158case 'property':159$items[] = $this->renderPropertyPart($part);160break;161case 'section':162$items[] = $this->renderSectionPart($part);163break;164case 'text':165case 'image':166$items[] = $this->renderTextPart($part);167break;168case 'raw':169$items[] = $this->renderRawPart($part);170break;171default:172throw new Exception(pht("Unknown part type '%s'!", $type));173}174}175$this->classes[] = 'phui-property-list-section';176$classes = implode(' ', $this->classes);177178return phutil_tag(179'div',180array(181'class' => $classes,182),183array(184$items,185));186}187188private function renderPropertyPart(array $part) {189$items = array();190foreach ($part['list'] as $spec) {191$key = $spec['key'];192$value = $spec['value'];193194// NOTE: We append a space to each value to improve the behavior when the195// user double-clicks a property value (like a URI) to select it. Without196// the space, the label is also selected.197198$items[] = phutil_tag(199'dt',200array(201'class' => 'phui-property-list-key',202),203array($key, ' '));204205$items[] = phutil_tag(206'dd',207array(208'class' => 'phui-property-list-value',209),210array($value, ' '));211}212213$stacked = '';214if ($this->stacked) {215$stacked = 'phui-property-list-stacked';216}217218$list = phutil_tag(219'dl',220array(221'class' => 'phui-property-list-properties',222),223$items);224225$shortcuts = null;226if ($this->hasKeyboardShortcuts) {227$shortcuts = new AphrontKeyboardShortcutsAvailableView();228}229230$list = phutil_tag(231'div',232array(233'class' => 'phui-property-list-properties-wrap '.$stacked,234),235array($shortcuts, $list));236237$action_list = null;238if ($this->actionList) {239$action_list = phutil_tag(240'div',241array(242'class' => 'phui-property-list-actions',243),244$this->actionList);245$this->actionList = null;246}247248return phutil_tag(249'div',250array(251'class' => 'phui-property-list-container grouped',252),253array($action_list, $list));254}255256private function renderSectionPart(array $part) {257$name = $part['name'];258if ($part['icon']) {259$icon = id(new PHUIIconView())260->setIcon($part['icon'].' bluegrey');261$name = phutil_tag(262'span',263array(264'class' => 'phui-property-list-section-header-icon',265),266array($icon, $name));267}268269return phutil_tag(270'div',271array(272'class' => 'phui-property-list-section-header',273),274$name);275}276277private function renderTextPart(array $part) {278$classes = array();279$classes[] = 'phui-property-list-text-content';280if ($part['type'] == 'image') {281$classes[] = 'phui-property-list-image-content';282}283return phutil_tag(284'div',285array(286'class' => implode(' ', $classes),287),288$part['content']);289}290291private function renderRawPart(array $part) {292$classes = array();293$classes[] = 'phui-property-list-raw-content';294return phutil_tag(295'div',296array(297'class' => implode(' ', $classes),298),299$part['content']);300}301302}303304305