2012-08-15 19:45:06 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorPropertyListView extends AphrontView {
|
|
|
|
|
2012-11-21 03:01:18 +01:00
|
|
|
private $parts = array();
|
2012-12-11 23:59:27 +01:00
|
|
|
private $hasKeyboardShortcuts;
|
2013-02-15 16:47:14 +01:00
|
|
|
private $object;
|
|
|
|
private $invokedWillRenderEvent;
|
2012-11-21 03:01:18 +01:00
|
|
|
|
|
|
|
protected function canAppendChild() {
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-15 19:45:06 +02:00
|
|
|
|
2013-02-15 16:47:14 +01:00
|
|
|
public function setObject($object) {
|
|
|
|
$this->object = $object;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-12-11 23:59:27 +01:00
|
|
|
public function setHasKeyboardShortcuts($has_keyboard_shortcuts) {
|
|
|
|
$this->hasKeyboardShortcuts = $has_keyboard_shortcuts;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
public function addProperty($key, $value) {
|
2012-11-21 03:01:18 +01:00
|
|
|
$current = array_pop($this->parts);
|
|
|
|
|
|
|
|
if (!$current || $current['type'] != 'property') {
|
|
|
|
if ($current) {
|
|
|
|
$this->parts[] = $current;
|
|
|
|
}
|
|
|
|
$current = array(
|
|
|
|
'type' => 'property',
|
|
|
|
'list' => array(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$current['list'][] = array(
|
|
|
|
'key' => $key,
|
|
|
|
'value' => $value,
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->parts[] = $current;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addSectionHeader($name) {
|
|
|
|
$this->parts[] = array(
|
|
|
|
'type' => 'section',
|
|
|
|
'name' => $name,
|
|
|
|
);
|
2012-08-15 19:45:06 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-10-15 23:50:53 +02:00
|
|
|
public function addTextContent($content) {
|
2012-11-21 03:01:18 +01:00
|
|
|
$this->parts[] = array(
|
|
|
|
'type' => 'text',
|
|
|
|
'content' => $content,
|
|
|
|
);
|
|
|
|
return $this;
|
2012-10-15 23:50:53 +02:00
|
|
|
}
|
|
|
|
|
2013-02-27 01:37:45 +01:00
|
|
|
public function addImageContent($content) {
|
|
|
|
$this->parts[] = array(
|
|
|
|
'type' => 'image',
|
|
|
|
'content' => $content,
|
|
|
|
);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-02-15 16:47:14 +01:00
|
|
|
public function invokeWillRenderEvent() {
|
|
|
|
if ($this->object && $this->getUser() && !$this->invokedWillRenderEvent) {
|
|
|
|
$event = new PhabricatorEvent(
|
|
|
|
PhabricatorEventType::TYPE_UI_WILLRENDERPROPERTIES,
|
|
|
|
array(
|
|
|
|
'object' => $this->object,
|
|
|
|
'view' => $this,
|
|
|
|
));
|
|
|
|
$event->setUser($this->getUser());
|
|
|
|
PhutilEventEngine::dispatchEvent($event);
|
|
|
|
}
|
|
|
|
$this->invokedWillRenderEvent = true;
|
|
|
|
}
|
|
|
|
|
2013-07-10 14:09:59 +02:00
|
|
|
public function applyCustomFields(array $fields) {
|
|
|
|
assert_instances_of($fields, 'PhabricatorCustomField');
|
|
|
|
|
2013-07-10 21:34:09 +02:00
|
|
|
// Move all the blocks to the end, regardless of their configuration order,
|
|
|
|
// because it always looks silly to render a block in the middle of a list
|
|
|
|
// of properties.
|
|
|
|
$head = array();
|
|
|
|
$tail = array();
|
|
|
|
foreach ($fields as $key => $field) {
|
|
|
|
$style = $field->getStyleForPropertyView();
|
|
|
|
switch ($style) {
|
|
|
|
case 'property':
|
|
|
|
$head[$key] = $field;
|
|
|
|
break;
|
|
|
|
case 'block':
|
|
|
|
$tail[$key] = $field;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception(
|
|
|
|
"Unknown field property view style '{$style}'; valid styles are ".
|
|
|
|
"'block' and 'property'.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$fields = $head + $tail;
|
|
|
|
|
2013-07-10 14:09:59 +02:00
|
|
|
foreach ($fields as $field) {
|
|
|
|
$label = $field->renderPropertyViewLabel();
|
|
|
|
$value = $field->renderPropertyViewValue();
|
|
|
|
if ($value !== null) {
|
|
|
|
switch ($field->getStyleForPropertyView()) {
|
|
|
|
case 'property':
|
|
|
|
$this->addProperty($label, $value);
|
|
|
|
break;
|
|
|
|
case 'block':
|
|
|
|
$this->invokeWillRenderEvent();
|
|
|
|
if ($label !== null) {
|
|
|
|
$this->addSectionHeader($label);
|
|
|
|
}
|
|
|
|
$this->addTextContent($value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-15 16:47:14 +01:00
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
public function render() {
|
2013-02-15 16:47:14 +01:00
|
|
|
$this->invokeWillRenderEvent();
|
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
require_celerity_resource('phabricator-property-list-view-css');
|
|
|
|
|
|
|
|
$items = array();
|
2012-11-21 03:01:18 +01:00
|
|
|
foreach ($this->parts as $part) {
|
|
|
|
$type = $part['type'];
|
|
|
|
switch ($type) {
|
|
|
|
case 'property':
|
|
|
|
$items[] = $this->renderPropertyPart($part);
|
|
|
|
break;
|
|
|
|
case 'section':
|
|
|
|
$items[] = $this->renderSectionPart($part);
|
|
|
|
break;
|
|
|
|
case 'text':
|
2013-02-27 01:37:45 +01:00
|
|
|
case 'image':
|
2012-11-21 03:01:18 +01:00
|
|
|
$items[] = $this->renderTextPart($part);
|
|
|
|
break;
|
|
|
|
default:
|
2013-03-02 00:37:32 +01:00
|
|
|
throw new Exception(pht("Unknown part type '%s'!", $type));
|
2012-11-21 03:01:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-02 15:04:54 +01:00
|
|
|
return phutil_tag(
|
2012-11-21 03:01:18 +01:00
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'class' => 'phabricator-property-list-view',
|
|
|
|
),
|
2013-03-09 22:52:41 +01:00
|
|
|
$items);
|
2012-11-21 03:01:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function renderPropertyPart(array $part) {
|
|
|
|
$items = array();
|
|
|
|
foreach ($part['list'] as $spec) {
|
|
|
|
$key = $spec['key'];
|
|
|
|
$value = $spec['value'];
|
|
|
|
|
2013-05-24 19:47:54 +02:00
|
|
|
// NOTE: We append a space to each value to improve the behavior when the
|
|
|
|
// user double-clicks a property value (like a URI) to select it. Without
|
|
|
|
// the space, the label is also selected.
|
|
|
|
|
2013-01-18 03:43:35 +01:00
|
|
|
$items[] = phutil_tag(
|
2012-08-15 19:45:06 +02:00
|
|
|
'dt',
|
|
|
|
array(
|
2012-11-21 03:01:18 +01:00
|
|
|
'class' => 'phabricator-property-list-key',
|
2012-08-15 19:45:06 +02:00
|
|
|
),
|
2013-05-24 19:47:54 +02:00
|
|
|
array($key, ' '));
|
2013-01-29 20:01:47 +01:00
|
|
|
|
|
|
|
$items[] = phutil_tag(
|
2012-08-15 19:45:06 +02:00
|
|
|
'dd',
|
|
|
|
array(
|
2012-11-21 03:01:18 +01:00
|
|
|
'class' => 'phabricator-property-list-value',
|
2012-08-15 19:45:06 +02:00
|
|
|
),
|
2013-05-24 19:47:54 +02:00
|
|
|
array($value, ' '));
|
2012-08-15 19:45:06 +02:00
|
|
|
}
|
|
|
|
|
2013-01-29 20:01:47 +01:00
|
|
|
$list = phutil_tag(
|
2012-08-15 19:45:06 +02:00
|
|
|
'dl',
|
|
|
|
array(
|
2012-11-21 03:01:18 +01:00
|
|
|
'class' => 'phabricator-property-list-properties',
|
2012-08-15 19:45:06 +02:00
|
|
|
),
|
2013-03-10 04:23:50 +01:00
|
|
|
$items);
|
2012-12-11 23:59:27 +01:00
|
|
|
|
|
|
|
$shortcuts = null;
|
|
|
|
if ($this->hasKeyboardShortcuts) {
|
2013-03-09 22:52:21 +01:00
|
|
|
$shortcuts = new AphrontKeyboardShortcutsAvailableView();
|
2012-12-11 23:59:27 +01:00
|
|
|
}
|
|
|
|
|
2013-01-29 20:01:47 +01:00
|
|
|
return array(
|
|
|
|
$shortcuts,
|
|
|
|
phutil_tag(
|
2012-12-11 23:59:27 +01:00
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'class' => 'phabricator-property-list-container',
|
|
|
|
),
|
2013-01-18 09:32:58 +01:00
|
|
|
array(
|
|
|
|
$list,
|
|
|
|
phutil_tag(
|
|
|
|
'div',
|
|
|
|
array('class' => 'phabriator-property-list-view-end'),
|
|
|
|
''),
|
2013-01-29 20:01:47 +01:00
|
|
|
)));
|
2012-11-21 03:01:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function renderSectionPart(array $part) {
|
2013-01-18 03:43:35 +01:00
|
|
|
return phutil_tag(
|
2012-11-21 03:01:18 +01:00
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'class' => 'phabricator-property-list-section-header',
|
|
|
|
),
|
2013-01-18 03:43:35 +01:00
|
|
|
$part['name']);
|
2012-08-15 19:45:06 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 03:01:18 +01:00
|
|
|
private function renderTextPart(array $part) {
|
2013-02-27 01:37:45 +01:00
|
|
|
$classes = array();
|
|
|
|
$classes[] = 'phabricator-property-list-text-content';
|
|
|
|
if ($part['type'] == 'image') {
|
|
|
|
$classes[] = 'phabricator-property-list-image-content';
|
2013-03-03 22:10:59 +01:00
|
|
|
$classes[] = 'phabricator-remarkup-dark';
|
2013-02-27 01:37:45 +01:00
|
|
|
}
|
2013-01-29 20:01:47 +01:00
|
|
|
return phutil_tag(
|
2012-11-21 03:01:18 +01:00
|
|
|
'div',
|
|
|
|
array(
|
2013-02-27 01:37:45 +01:00
|
|
|
'class' => implode($classes, ' '),
|
2012-11-21 03:01:18 +01:00
|
|
|
),
|
|
|
|
$part['content']);
|
|
|
|
}
|
2012-08-15 19:45:06 +02:00
|
|
|
|
|
|
|
}
|