| 1 |
<?php |
| 2 |
namespace Depicter\Html; |
| 3 |
|
| 4 |
|
| 5 |
use Averta\WordPress\Handler\Error; |
| 6 |
|
| 7 |
class Canvas |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @var Html |
| 11 |
*/ |
| 12 |
private $html; |
| 13 |
|
| 14 |
/** |
| 15 |
* @var Html |
| 16 |
*/ |
| 17 |
private $head; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var Html |
| 21 |
*/ |
| 22 |
private $body; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
private $cssLinks; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
private $jsLinks; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
private $content; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
private $inlineStyle; |
| 43 |
|
| 44 |
|
| 45 |
public function __construct( $content = '' ) |
| 46 |
{ |
| 47 |
$this->content = $content; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
public function registerStyle( $cssLinks = [] ) |
| 52 |
{ |
| 53 |
$cssLinks = (array) $cssLinks; |
| 54 |
|
| 55 |
foreach ( $cssLinks as $cssLinkId => $cssLink ){ |
| 56 |
if( is_numeric( $cssLinkId ) ){ |
| 57 |
Error::trigger( 'You must specify a valid ID-name for each css link.' ); |
| 58 |
} |
| 59 |
$this->cssLinks[ $cssLinkId ] = $cssLink; |
| 60 |
} |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
public function registerScript( $jsLinks = [] ) |
| 65 |
{ |
| 66 |
$jsLinks = (array) $jsLinks; |
| 67 |
|
| 68 |
foreach ( $jsLinks as $jsLinkId => $jsLink ){ |
| 69 |
if( is_numeric( $jsLinkId ) ){ |
| 70 |
Error::trigger( 'You must specify a valid ID-name for each javascript link.' ); |
| 71 |
} |
| 72 |
$this->jsLinks[ $jsLinkId ] = $jsLink; |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
public function setContent( $content ) |
| 78 |
{ |
| 79 |
$this->content = $content; |
| 80 |
} |
| 81 |
|
| 82 |
public function setInlineStyle( $style ) |
| 83 |
{ |
| 84 |
$this->inlineStyle = $style; |
| 85 |
} |
| 86 |
|
| 87 |
public function render() |
| 88 |
{ |
| 89 |
$this->html = Html::html([]); |
| 90 |
|
| 91 |
// Make head tag |
| 92 |
$this->renderHead(); |
| 93 |
|
| 94 |
// Make body tag |
| 95 |
$this->renderBody(); |
| 96 |
|
| 97 |
$this->html->nest( "\n" .$this->head ); |
| 98 |
$this->html->nest( "\n" .$this->body . "\n" ); |
| 99 |
|
| 100 |
return $this->html; |
| 101 |
} |
| 102 |
|
| 103 |
protected function renderHead(){ |
| 104 |
$this->head = Html::head([]); |
| 105 |
|
| 106 |
$this->head->nest( "\n" . Html::meta([ 'charset' => 'utf-8' ]) ); |
| 107 |
$this->head->nest( "\n" . Html::meta([ 'name' => 'viewport', 'content' => 'width=device-width, initial-scale=1.0' ]) ); |
| 108 |
$this->head->nest( "\n" . Html::title([], 'Depicter Preview') ); |
| 109 |
|
| 110 |
$this->renderStyleTags(); |
| 111 |
|
| 112 |
if( $this->inlineStyle ){ |
| 113 |
$this->head->nest( "\n" .Html::style([], $this->inlineStyle ) . "\n" ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
protected function renderBody() |
| 118 |
{ |
| 119 |
$this->body = Html::body([ |
| 120 |
'class' => 'depicter-preview-canvas' |
| 121 |
], "\n" . $this->content . "\n" ); |
| 122 |
|
| 123 |
$this->renderScriptTags(); |
| 124 |
} |
| 125 |
|
| 126 |
protected function renderStyleTags(){ |
| 127 |
if( empty( $this->cssLinks ) ){ |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
foreach ( $this->cssLinks as $cssId => $cssLink ){ |
| 132 |
$linkTag = Html::link([ |
| 133 |
'rel' => "stylesheet", |
| 134 |
'id' => $cssId . '-css', |
| 135 |
'href' => $cssLink, |
| 136 |
'media' => 'all' |
| 137 |
]); |
| 138 |
$this->head->nest( "\n" .$linkTag ); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
protected function renderScriptTags(){ |
| 143 |
if( empty( $this->jsLinks ) ){ |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
foreach ( $this->jsLinks as $jsId => $jsLink ){ |
| 148 |
$scriptTag = Html::script([ |
| 149 |
'id' => $jsId . '-js', |
| 150 |
'src' => $jsLink |
| 151 |
]); |
| 152 |
$this->body->nest( $scriptTag . "\n" ); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|