[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Render a simple preview panel for a bound Remarkup text control.
   5   */
   6  final class PHUIRemarkupPreviewPanel extends AphrontTagView {
   7  
   8    private $header;
   9    private $loadingText;
  10    private $controlID;
  11    private $previewURI;
  12    private $skin = 'default';
  13  
  14    protected function canAppendChild() {
  15      return false;
  16    }
  17  
  18    public function setPreviewURI($preview_uri) {
  19      $this->previewURI = $preview_uri;
  20      return $this;
  21    }
  22  
  23    public function setControlID($control_id) {
  24      $this->controlID = $control_id;
  25      return $this;
  26    }
  27  
  28    public function setHeader($header) {
  29      $this->header = $header;
  30      return $this;
  31    }
  32  
  33    public function setLoadingText($loading_text) {
  34      $this->loadingText = $loading_text;
  35      return $this;
  36    }
  37  
  38    public function setSkin($skin) {
  39      static $skins = array(
  40        'default' => true,
  41        'document' => true,
  42      );
  43  
  44      if (empty($skins[$skin])) {
  45        $valid = implode(', ', array_keys($skins));
  46        throw new Exception("Invalid skin '{$skin}'. Valid skins are: {$valid}.");
  47      }
  48  
  49      $this->skin = $skin;
  50      return $this;
  51    }
  52  
  53    public function getTagName() {
  54      return 'div';
  55    }
  56  
  57    public function getTagAttributes() {
  58      $classes = array();
  59      $classes[] = 'phui-remarkup-preview';
  60  
  61      if ($this->skin) {
  62        $classes[] = 'phui-remarkup-preview-skin-'.$this->skin;
  63      }
  64  
  65      return array(
  66        'class' => $classes,
  67      );
  68    }
  69  
  70    protected function getTagContent() {
  71      if ($this->previewURI === null) {
  72        throw new Exception('Call setPreviewURI() before rendering!');
  73      }
  74      if ($this->controlID === null) {
  75        throw new Exception('Call setControlID() before rendering!');
  76      }
  77  
  78      $preview_id = celerity_generate_unique_node_id();
  79  
  80      require_celerity_resource('phui-remarkup-preview-css');
  81      Javelin::initBehavior(
  82        'remarkup-preview',
  83        array(
  84          'previewID' => $preview_id,
  85          'controlID' => $this->controlID,
  86          'uri' => $this->previewURI,
  87        ));
  88  
  89      $loading = phutil_tag(
  90        'div',
  91        array(
  92          'class' => 'phui-preview-loading-text',
  93        ),
  94        nonempty($this->loadingText, pht('Loading preview...')));
  95  
  96      $header = null;
  97      if ($this->header) {
  98        $header = phutil_tag(
  99          'div',
 100          array(
 101            'class' => 'phui-preview-header',
 102          ),
 103          $this->header);
 104      }
 105  
 106      $preview = phutil_tag(
 107        'div',
 108        array(
 109          'id' => $preview_id,
 110          'class' => 'phabricator-remarkup',
 111        ),
 112        $loading);
 113  
 114      $content = array($header, $preview);
 115  
 116      switch ($this->skin) {
 117        case 'document':
 118          $content = id(new PHUIDocumentView())
 119            ->appendChild($content)
 120            ->setFontKit(PHUIDocumentView::FONT_SOURCE_SANS);
 121          break;
 122        default:
 123          $content = id(new PHUIBoxView())
 124            ->appendChild($content)
 125            ->setBorder(true)
 126            ->addMargin(PHUI::MARGIN_LARGE)
 127            ->addPadding(PHUI::PADDING_LARGE)
 128            ->addClass('phui-panel-preview');
 129          break;
 130      }
 131  
 132      return $content;
 133    }
 134  
 135  }


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