2013-08-05 19:46:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render a simple preview panel for a bound Remarkup text control.
|
|
|
|
*/
|
|
|
|
final class PHUIRemarkupPreviewPanel extends AphrontTagView {
|
|
|
|
|
|
|
|
private $header;
|
|
|
|
private $loadingText;
|
|
|
|
private $controlID;
|
|
|
|
private $previewURI;
|
2013-08-05 19:47:26 +02:00
|
|
|
private $skin = 'default';
|
2013-08-05 19:46:39 +02:00
|
|
|
|
|
|
|
protected function canAppendChild() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPreviewURI($preview_uri) {
|
|
|
|
$this->previewURI = $preview_uri;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setControlID($control_id) {
|
|
|
|
$this->controlID = $control_id;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setHeader($header) {
|
|
|
|
$this->header = $header;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLoadingText($loading_text) {
|
|
|
|
$this->loadingText = $loading_text;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-05 19:47:26 +02:00
|
|
|
public function setSkin($skin) {
|
|
|
|
static $skins = array(
|
|
|
|
'default' => true,
|
|
|
|
'document' => true,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (empty($skins[$skin])) {
|
2015-05-13 23:53:52 +02:00
|
|
|
throw new Exception(
|
|
|
|
pht(
|
|
|
|
"Invalid skin '%s'. Valid skins are: %s.",
|
|
|
|
$skin,
|
|
|
|
implode(', ', array_keys($skins))));
|
2013-08-05 19:47:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->skin = $skin;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-01-13 20:54:39 +01:00
|
|
|
protected function getTagName() {
|
2013-08-05 19:46:39 +02:00
|
|
|
return 'div';
|
|
|
|
}
|
|
|
|
|
2015-01-13 20:54:39 +01:00
|
|
|
protected function getTagAttributes() {
|
2013-08-05 19:47:26 +02:00
|
|
|
$classes = array();
|
|
|
|
$classes[] = 'phui-remarkup-preview';
|
|
|
|
|
|
|
|
if ($this->skin) {
|
|
|
|
$classes[] = 'phui-remarkup-preview-skin-'.$this->skin;
|
|
|
|
}
|
|
|
|
|
2013-08-05 19:46:39 +02:00
|
|
|
return array(
|
2013-08-05 19:47:26 +02:00
|
|
|
'class' => $classes,
|
2013-08-05 19:46:39 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTagContent() {
|
|
|
|
if ($this->previewURI === null) {
|
2015-05-13 23:53:52 +02:00
|
|
|
throw new PhutilInvalidStateException('setPreviewURI');
|
2013-08-05 19:46:39 +02:00
|
|
|
}
|
|
|
|
if ($this->controlID === null) {
|
2015-05-13 23:53:52 +02:00
|
|
|
throw new PhutilInvalidStateException('setControlID');
|
2013-08-05 19:46:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$preview_id = celerity_generate_unique_node_id();
|
|
|
|
|
|
|
|
require_celerity_resource('phui-remarkup-preview-css');
|
|
|
|
Javelin::initBehavior(
|
|
|
|
'remarkup-preview',
|
|
|
|
array(
|
|
|
|
'previewID' => $preview_id,
|
|
|
|
'controlID' => $this->controlID,
|
|
|
|
'uri' => $this->previewURI,
|
|
|
|
));
|
|
|
|
|
|
|
|
$loading = phutil_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'class' => 'phui-preview-loading-text',
|
|
|
|
),
|
|
|
|
nonempty($this->loadingText, pht('Loading preview...')));
|
|
|
|
|
|
|
|
$header = null;
|
|
|
|
if ($this->header) {
|
|
|
|
$header = phutil_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'class' => 'phui-preview-header',
|
|
|
|
),
|
|
|
|
$this->header);
|
|
|
|
}
|
|
|
|
|
|
|
|
$preview = phutil_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'id' => $preview_id,
|
|
|
|
'class' => 'phabricator-remarkup',
|
|
|
|
),
|
|
|
|
$loading);
|
|
|
|
|
2013-08-05 19:47:26 +02:00
|
|
|
$content = array($header, $preview);
|
|
|
|
|
|
|
|
switch ($this->skin) {
|
|
|
|
case 'document':
|
2013-08-26 20:53:11 +02:00
|
|
|
$content = id(new PHUIDocumentView())
|
2014-11-04 04:01:00 +01:00
|
|
|
->appendChild($content)
|
|
|
|
->setFontKit(PHUIDocumentView::FONT_SOURCE_SANS);
|
2013-08-05 19:47:26 +02:00
|
|
|
break;
|
|
|
|
default:
|
2013-08-26 20:53:11 +02:00
|
|
|
$content = id(new PHUIBoxView())
|
|
|
|
->appendChild($content)
|
|
|
|
->setBorder(true)
|
|
|
|
->addMargin(PHUI::MARGIN_LARGE)
|
|
|
|
->addPadding(PHUI::PADDING_LARGE)
|
|
|
|
->addClass('phui-panel-preview');
|
2013-08-05 19:47:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
2013-08-05 19:46:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|