[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/view/phui/ -> PHUIPropertyListView.php (source)

   1  <?php
   2  
   3  final class PHUIPropertyListView extends AphrontView {
   4  
   5    private $parts = array();
   6    private $hasKeyboardShortcuts;
   7    private $object;
   8    private $invokedWillRenderEvent;
   9    private $actionList;
  10    private $classes = array();
  11    private $stacked;
  12  
  13    const ICON_SUMMARY = 'fa-align-left bluegrey';
  14    const ICON_TESTPLAN = 'fa-file-text-o bluegrey';
  15  
  16    protected function canAppendChild() {
  17      return false;
  18    }
  19  
  20    public function setObject($object) {
  21      $this->object = $object;
  22      return $this;
  23    }
  24  
  25    public function setActionList(PhabricatorActionListView $list) {
  26      $this->actionList = $list;
  27      return $this;
  28    }
  29  
  30    public function setStacked($stacked) {
  31      $this->stacked = $stacked;
  32      return $this;
  33    }
  34  
  35    public function addClass($class) {
  36      $this->classes[] = $class;
  37      return $this;
  38    }
  39  
  40    public function setHasKeyboardShortcuts($has_keyboard_shortcuts) {
  41      $this->hasKeyboardShortcuts = $has_keyboard_shortcuts;
  42      return $this;
  43    }
  44  
  45    public function addProperty($key, $value) {
  46      $current = array_pop($this->parts);
  47  
  48      if (!$current || $current['type'] != 'property') {
  49        if ($current) {
  50          $this->parts[] = $current;
  51        }
  52        $current = array(
  53          'type' => 'property',
  54          'list' => array(),
  55        );
  56      }
  57  
  58      $current['list'][] = array(
  59        'key'   => $key,
  60        'value' => $value,
  61      );
  62  
  63      $this->parts[] = $current;
  64      return $this;
  65    }
  66  
  67    public function addSectionHeader($name, $icon = null) {
  68      $this->parts[] = array(
  69        'type' => 'section',
  70        'name' => $name,
  71        'icon' => $icon,
  72      );
  73      return $this;
  74    }
  75  
  76    public function addTextContent($content) {
  77      $this->parts[] = array(
  78        'type'    => 'text',
  79        'content' => $content,
  80      );
  81      return $this;
  82    }
  83  
  84    public function addRawContent($content) {
  85      $this->parts[] = array(
  86        'type'    => 'raw',
  87        'content' => $content,
  88      );
  89      return $this;
  90    }
  91  
  92    public function addImageContent($content) {
  93      $this->parts[] = array(
  94        'type'    => 'image',
  95        'content' => $content,
  96      );
  97      return $this;
  98    }
  99  
 100    public function invokeWillRenderEvent() {
 101      if ($this->object && $this->getUser() && !$this->invokedWillRenderEvent) {
 102        $event = new PhabricatorEvent(
 103          PhabricatorEventType::TYPE_UI_WILLRENDERPROPERTIES,
 104          array(
 105            'object'  => $this->object,
 106            'view'    => $this,
 107          ));
 108        $event->setUser($this->getUser());
 109        PhutilEventEngine::dispatchEvent($event);
 110      }
 111      $this->invokedWillRenderEvent = true;
 112    }
 113  
 114    public function render() {
 115      $this->invokeWillRenderEvent();
 116  
 117      require_celerity_resource('phui-property-list-view-css');
 118  
 119      $items = array();
 120  
 121      $parts = $this->parts;
 122  
 123      // If we have an action list, make sure we render a property part, even
 124      // if there are no properties. Otherwise, the action list won't render.
 125      if ($this->actionList) {
 126        $have_property_part = false;
 127        foreach ($this->parts as $part) {
 128          if ($part['type'] == 'property') {
 129            $have_property_part = true;
 130            break;
 131          }
 132        }
 133        if (!$have_property_part) {
 134          $parts[] = array(
 135            'type' => 'property',
 136            'list' => array(),
 137          );
 138        }
 139      }
 140  
 141      foreach ($parts as $part) {
 142        $type = $part['type'];
 143        switch ($type) {
 144          case 'property':
 145            $items[] = $this->renderPropertyPart($part);
 146            break;
 147          case 'section':
 148            $items[] = $this->renderSectionPart($part);
 149            break;
 150          case 'text':
 151          case 'image':
 152            $items[] = $this->renderTextPart($part);
 153            break;
 154          case 'raw':
 155            $items[] = $this->renderRawPart($part);
 156            break;
 157          default:
 158            throw new Exception(pht("Unknown part type '%s'!", $type));
 159        }
 160      }
 161      $this->classes[] = 'phui-property-list-section';
 162      $classes = implode(' ', $this->classes);
 163  
 164      return phutil_tag(
 165        'div',
 166        array(
 167          'class' => $classes,
 168        ),
 169        array(
 170          $items,
 171        ));
 172    }
 173  
 174    private function renderPropertyPart(array $part) {
 175      $items = array();
 176      foreach ($part['list'] as $spec) {
 177        $key = $spec['key'];
 178        $value = $spec['value'];
 179  
 180        // NOTE: We append a space to each value to improve the behavior when the
 181        // user double-clicks a property value (like a URI) to select it. Without
 182        // the space, the label is also selected.
 183  
 184        $items[] = phutil_tag(
 185          'dt',
 186          array(
 187            'class' => 'phui-property-list-key',
 188          ),
 189          array($key, ' '));
 190  
 191        $items[] = phutil_tag(
 192          'dd',
 193          array(
 194            'class' => 'phui-property-list-value',
 195          ),
 196          array($value, ' '));
 197      }
 198  
 199      $stacked = '';
 200      if ($this->stacked) {
 201        $stacked = 'phui-property-list-stacked';
 202      }
 203  
 204      $list = phutil_tag(
 205        'dl',
 206        array(
 207          'class' => 'phui-property-list-properties '.$stacked,
 208        ),
 209        $items);
 210  
 211      $shortcuts = null;
 212      if ($this->hasKeyboardShortcuts) {
 213        $shortcuts = new AphrontKeyboardShortcutsAvailableView();
 214      }
 215  
 216      $list = phutil_tag(
 217        'div',
 218        array(
 219          'class' => 'phui-property-list-properties-wrap',
 220        ),
 221        array($shortcuts, $list));
 222  
 223      $action_list = null;
 224      if ($this->actionList) {
 225        $action_list = phutil_tag(
 226          'div',
 227          array(
 228            'class' => 'phui-property-list-actions',
 229          ),
 230          $this->actionList);
 231        $this->actionList = null;
 232      }
 233  
 234      return phutil_tag(
 235        'div',
 236        array(
 237          'class' => 'phui-property-list-container grouped',
 238        ),
 239        array($action_list, $list));
 240    }
 241  
 242    private function renderSectionPart(array $part) {
 243      $name = $part['name'];
 244      if ($part['icon']) {
 245        $icon = id(new PHUIIconView())
 246          ->setIconFont($part['icon']);
 247        $name = phutil_tag(
 248          'span',
 249          array(
 250            'class' => 'phui-property-list-section-header-icon',
 251          ),
 252          array($icon, $name));
 253      }
 254  
 255      return phutil_tag(
 256        'div',
 257        array(
 258          'class' => 'phui-property-list-section-header',
 259        ),
 260        $name);
 261    }
 262  
 263    private function renderTextPart(array $part) {
 264      $classes = array();
 265      $classes[] = 'phui-property-list-text-content';
 266      if ($part['type'] == 'image') {
 267        $classes[] = 'phui-property-list-image-content';
 268      }
 269      return phutil_tag(
 270        'div',
 271        array(
 272          'class' => implode($classes, ' '),
 273        ),
 274        $part['content']);
 275    }
 276  
 277    private function renderRawPart(array $part) {
 278      $classes = array();
 279      $classes[] = 'phui-property-list-raw-content';
 280      return phutil_tag(
 281        'div',
 282        array(
 283          'class' => implode($classes, ' '),
 284        ),
 285        $part['content']);
 286    }
 287  
 288  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1