| 1 |
<?php |
| 2 |
namespace Depicter\Document\Models; |
| 3 |
|
| 4 |
use Averta\Core\Utility\Arr; |
| 5 |
use Depicter\Document\CSS\Breakpoints; |
| 6 |
use Depicter\Document\CSS\Selector; |
| 7 |
use Depicter\Document\Helper\Helper; |
| 8 |
use Depicter\Document\Models\Traits\HasDataSheetTrait; |
| 9 |
use Depicter\Document\Models\Traits\HasDocumentIdTrait; |
| 10 |
use Depicter\Editor\Models\Common\Size; |
| 11 |
use Depicter\Editor\Models\Common\Styles; |
| 12 |
use Depicter\Html\Html; |
| 13 |
|
| 14 |
class Element |
| 15 |
{ |
| 16 |
use HasDocumentIdTrait; |
| 17 |
use HasDataSheetTrait; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $id; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
public $name; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
public $type; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
public $section; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var array|null |
| 41 |
*/ |
| 42 |
public $hideOnSections = []; |
| 43 |
|
| 44 |
/** |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
public $parent; |
| 48 |
|
| 49 |
/** |
| 50 |
* @var \Depicter\Document\Models\Common\Position\States |
| 51 |
*/ |
| 52 |
public $position; |
| 53 |
|
| 54 |
/** |
| 55 |
* @var bool |
| 56 |
*/ |
| 57 |
public $wrap; |
| 58 |
|
| 59 |
/** |
| 60 |
* @var bool |
| 61 |
*/ |
| 62 |
public $locked = false; |
| 63 |
|
| 64 |
/** |
| 65 |
* @var object |
| 66 |
*/ |
| 67 |
public $visible = true; |
| 68 |
|
| 69 |
/** |
| 70 |
* @var bool|null |
| 71 |
*/ |
| 72 |
public $keepAspectRatio; |
| 73 |
|
| 74 |
/** |
| 75 |
* @var array|null |
| 76 |
*/ |
| 77 |
public $children; |
| 78 |
|
| 79 |
/** |
| 80 |
* @var array |
| 81 |
*/ |
| 82 |
public $childrenObjects = []; |
| 83 |
|
| 84 |
/** |
| 85 |
* @var \Depicter\Document\Models\Common\Size\States|null |
| 86 |
*/ |
| 87 |
public $size; |
| 88 |
|
| 89 |
/** |
| 90 |
* @var \Depicter\Document\Models\Common\Styles|null |
| 91 |
*/ |
| 92 |
public $styles; |
| 93 |
|
| 94 |
/** |
| 95 |
* @var \Depicter\Document\Models\Common\InnerStyles|null |
| 96 |
*/ |
| 97 |
public $innerStyles; |
| 98 |
|
| 99 |
/** |
| 100 |
* @var object|null |
| 101 |
*/ |
| 102 |
public $options; |
| 103 |
|
| 104 |
/** |
| 105 |
* @var \Depicter\Document\Models\Common\Animation|null |
| 106 |
*/ |
| 107 |
public $animation; |
| 108 |
|
| 109 |
/** |
| 110 |
* @var \Depicter\Document\Models\Common\Parallax|null |
| 111 |
*/ |
| 112 |
public $parallax; |
| 113 |
|
| 114 |
/** |
| 115 |
* @var array|null |
| 116 |
*/ |
| 117 |
public $actions; |
| 118 |
|
| 119 |
/** |
| 120 |
* @var int |
| 121 |
*/ |
| 122 |
public $depth; |
| 123 |
|
| 124 |
/** |
| 125 |
* @var array |
| 126 |
*/ |
| 127 |
public $devices; |
| 128 |
|
| 129 |
/** |
| 130 |
* Selector and CSS list |
| 131 |
* |
| 132 |
* @var array |
| 133 |
*/ |
| 134 |
protected $selectorCssList = []; |
| 135 |
|
| 136 |
/** |
| 137 |
* Instance of Element type or base Element class |
| 138 |
* |
| 139 |
* @var Element |
| 140 |
*/ |
| 141 |
protected $alias; |
| 142 |
|
| 143 |
/** |
| 144 |
* @var object|null |
| 145 |
*/ |
| 146 |
public $cropData; |
| 147 |
|
| 148 |
/** |
| 149 |
* @var \Depicter\Document\Models\Common\ResponsiveScale|null |
| 150 |
*/ |
| 151 |
public $responsiveScale; |
| 152 |
|
| 153 |
/** |
| 154 |
* @var string |
| 155 |
*/ |
| 156 |
protected $markup; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
/** |
| 161 |
* Element constructor. |
| 162 |
*/ |
| 163 |
public function __construct() { |
| 164 |
$this->devices = Breakpoints::names(); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* get element number |
| 169 |
* |
| 170 |
* @return string |
| 171 |
*/ |
| 172 |
public function getID() { |
| 173 |
return Helper::getElementIdFromSlug( $this->id ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* get element slug |
| 178 |
* |
| 179 |
* @return string |
| 180 |
*/ |
| 181 |
public function getSlug() { |
| 182 |
return $this->id; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get element name |
| 187 |
* |
| 188 |
* @return string |
| 189 |
*/ |
| 190 |
public function getName() { |
| 191 |
return $this->name ? $this->name : $this->id; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Get section ID number |
| 196 |
* |
| 197 |
* @return int |
| 198 |
*/ |
| 199 |
public function getSectionID() { |
| 200 |
return Helper::getSectionIdFromSlug( $this->section ); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Return child elements IDs for group type element |
| 205 |
* @return array|null |
| 206 |
*/ |
| 207 |
public function getElementIds() { |
| 208 |
return $this->children; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Sets belonging elements for group type element |
| 213 |
* |
| 214 |
* @param array $elementObjects |
| 215 |
*/ |
| 216 |
public function setElementObjects( $elementObjects ) |
| 217 |
{ |
| 218 |
$this->childrenObjects = $elementObjects; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Prepare class for rendering markup |
| 223 |
* |
| 224 |
* @return Element |
| 225 |
* @throws \JsonMapper_Exception |
| 226 |
*/ |
| 227 |
public function prepare() { |
| 228 |
return $this->alias(); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Check if unique element class exist then render element through that class |
| 233 |
* |
| 234 |
* @return Element |
| 235 |
* @throws \JsonMapper_Exception |
| 236 |
*/ |
| 237 |
public function alias(){ |
| 238 |
if( $this->alias ){ |
| 239 |
return $this->alias; |
| 240 |
} |
| 241 |
|
| 242 |
$className = '\\Depicter\\Document\\Models\\Elements\\' . ucfirst( $this->type ); |
| 243 |
if ( class_exists( $className ) ) { |
| 244 |
$mapper = new \JsonMapper(); |
| 245 |
$this->alias = $mapper->map( $this, new $className() ); |
| 246 |
// pass the document ID to new class too |
| 247 |
$this->alias->setDocumentID( $this->getDocumentID() ); |
| 248 |
|
| 249 |
return $this->alias; |
| 250 |
} |
| 251 |
|
| 252 |
return $this; |
| 253 |
} |
| 254 |
|
| 255 |
public function render() {} |
| 256 |
|
| 257 |
public function getClassNames() { |
| 258 |
$classes = []; |
| 259 |
|
| 260 |
$classes[] = Selector::prefixify('element'); |
| 261 |
$classes[] = Selector::prefixify('layer'); |
| 262 |
|
| 263 |
$classes[] = $this->getSelector(); |
| 264 |
$classes[] = $this->getDataSheetClassName(); |
| 265 |
|
| 266 |
if( $this->options->className ?? 0 ){ |
| 267 |
$classes[] = $this->options->className; |
| 268 |
} |
| 269 |
|
| 270 |
if ( isset( $this->visible->default ) && $this->visible->default === false ) { |
| 271 |
$classes[] = 'depicter-hide-on-desktop '; |
| 272 |
} |
| 273 |
|
| 274 |
if ( isset( $this->visible->tablet ) && $this->visible->tablet === false ) { |
| 275 |
$classes[] = 'depicter-hide-on-tablet '; |
| 276 |
} |
| 277 |
|
| 278 |
if ( isset( $this->visible->mobile ) && $this->visible->mobile === false ) { |
| 279 |
$classes[] = 'depicter-hide-on-mobile '; |
| 280 |
} |
| 281 |
|
| 282 |
return trim( implode(' ', $classes ) ); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Get default data attributes |
| 287 |
* |
| 288 |
* @return array |
| 289 |
* @throws \JsonMapper_Exception |
| 290 |
*/ |
| 291 |
public function getDefaultAttributes() { |
| 292 |
$dataAttrs = [ |
| 293 |
'id' => $this->getCssID(), |
| 294 |
'class' => $this->getClassNames(), |
| 295 |
'data-type' => $this->type, |
| 296 |
'data-wrap' => !!$this->wrap ? "true" : "false", |
| 297 |
'data-name' => $this->getName() |
| 298 |
]; |
| 299 |
|
| 300 |
if( ! empty( $this->position->getOffset("default" ) ) ){ |
| 301 |
$dataAttrs['data-offset'] = $this->position->getOffset("default" ); |
| 302 |
} |
| 303 |
if( ! empty( $this->position->getOffset("tablet" ) ) ){ |
| 304 |
$dataAttrs['data-tablet-offset'] = $this->position->getOffset("tablet" ); |
| 305 |
} |
| 306 |
if( ! empty( $this->position->getOffset("mobile" ) ) ){ |
| 307 |
$dataAttrs['data-mobile-offset'] = $this->position->getOffset("mobile" ); |
| 308 |
} |
| 309 |
|
| 310 |
if( $this->hideOnSections && $hideOnSections = Helper::getInvisibleSectionsCssIdList( $this->hideOnSections, $this->getDocumentID() ) ){ |
| 311 |
$dataAttrs['data-hide-on-sections'] = $hideOnSections; |
| 312 |
} |
| 313 |
|
| 314 |
if ( $actions = Helper::getActions( $this->actions ) ) { |
| 315 |
$dataAttrs['data-actions'] = $actions; |
| 316 |
} |
| 317 |
if ( $this->animation && false !== $animationData = $this->animation->getAnimationAttrs() ) { |
| 318 |
$dataAttrs = Arr::merge( $dataAttrs, $animationData ); |
| 319 |
} |
| 320 |
|
| 321 |
if ( $this->parallax && false !== $parallaxData = $this->parallax->getParallaxAttrs() ) { |
| 322 |
$dataAttrs = Arr::merge( $dataAttrs, $parallaxData ); |
| 323 |
} |
| 324 |
|
| 325 |
if( ! empty( $this->size ) ){ |
| 326 |
$dataAttrs['data-width' ] = implode( ',', $this->size->getResponsiveSizes( 'width' , true ) ); |
| 327 |
$dataAttrs['data-height'] = implode( ',', $this->size->getResponsiveSizes( 'height', true ) ); |
| 328 |
} |
| 329 |
|
| 330 |
$dataAttrs['data-responsive-scale' ] = ! empty( $this->responsiveScale ) ? implode( ',', $this->responsiveScale->getResponsiveSizes() ) : 'true,,'; |
| 331 |
|
| 332 |
if( ! empty( $this->prepare()->styles->hover ) && $hoverOffDevices = $this->prepare()->styles->hover->getDisabledDeviceList() ){ |
| 333 |
$dataAttrs['data-hover-off' ] = implode( ',', $hoverOffDevices ); |
| 334 |
} |
| 335 |
|
| 336 |
if ( ! empty( $this->prepare()->styles->blendingMode ) ) { |
| 337 |
$dataAttrs['data-frame-class'] = $this->getSelector() . '-frame' ; |
| 338 |
} |
| 339 |
|
| 340 |
return $dataAttrs; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Get section CSS ID for data sheet if dataSource is assigned |
| 345 |
* |
| 346 |
* @return string |
| 347 |
*/ |
| 348 |
public function getDataSheetClassName() { |
| 349 |
if( $this->getDataSheetID() ){ |
| 350 |
return $this->getCssID(); |
| 351 |
} |
| 352 |
return ''; |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Check if element has link |
| 357 |
* @return false|\TypeRocket\Html\Html |
| 358 |
*/ |
| 359 |
public function getLinkTag() { |
| 360 |
if ( !empty( $this->options->url->path ) ) { |
| 361 |
$urlPath = $this->maybeReplaceDataSheetTags( $this->options->url->path ); |
| 362 |
$urlArgs = isset( $this->options->url->openInNewTab ) && empty( $this->options->url->openInNewTab ) ? [] : ['target' => '_blank']; |
| 363 |
return Html::a( '', $urlPath, $urlArgs ); |
| 364 |
} |
| 365 |
|
| 366 |
return false; |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Retrieves list of fonts used in typography options |
| 371 |
* |
| 372 |
* @return array |
| 373 |
* @throws \JsonMapper_Exception |
| 374 |
*/ |
| 375 |
public function getFontsList() |
| 376 |
{ |
| 377 |
$fontsList = ! empty( $this->prepare()->styles ) ? $this->prepare()->styles->getFontsList() : []; |
| 378 |
\Depicter::app()->documentFonts()->addFonts( $this->getDocumentID(), $fontsList, 'google' ); |
| 379 |
|
| 380 |
return $fontsList; |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Retrieves the content of element |
| 385 |
* |
| 386 |
* @return string |
| 387 |
*/ |
| 388 |
protected function getContent(){ |
| 389 |
return $this->options->content ?? ''; |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Get element CSS ID |
| 394 |
* |
| 395 |
* @return string |
| 396 |
*/ |
| 397 |
public function getCssID() { |
| 398 |
$cssID = Selector::getFullSelectorPath( $this->getDocumentID(), null, $this->getID() ); |
| 399 |
if( $dataSheetId = $this->getDataSheetID() ){ |
| 400 |
$cssID .= "-{$dataSheetId}"; |
| 401 |
} |
| 402 |
|
| 403 |
return $cssID; |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Get element selector |
| 408 |
* |
| 409 |
* @return string |
| 410 |
*/ |
| 411 |
public function getSelector() { |
| 412 |
return Selector::getUniqueSelector( $this->getDocumentID(), $this->getSectionID(), $this->getID(), Selector::ELEMENT_PREFIX ); |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Get element style selector |
| 417 |
* |
| 418 |
* @return string |
| 419 |
*/ |
| 420 |
public function getStyleSelector() { |
| 421 |
return Selector::PREFIX_CSS . " ." . $this->getSelector(); |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Get element frame selector |
| 426 |
* |
| 427 |
* @return string |
| 428 |
*/ |
| 429 |
public function getFrameStyleSelector() { |
| 430 |
return Selector::PREFIX_CSS . ' .' . $this->getSelector() . '-frame'; |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* Get list of selector and CSS for element |
| 435 |
* |
| 436 |
* @return array |
| 437 |
* @throws \JsonMapper_Exception |
| 438 |
*/ |
| 439 |
public function getSelectorAndCssList(){ |
| 440 |
|
| 441 |
if( ! empty( $this->prepare()->styles ) ){ |
| 442 |
$this->prepare()->styles->setDataSheet( $this->getDataSheet() ); |
| 443 |
$this->selectorCssList[ '.' . $this->getStyleSelector() ] = $this->prepare()->styles->getGeneralCss('normal'); |
| 444 |
|
| 445 |
$transitionCss = $this->prepare()->styles->getTransitionCss(); |
| 446 |
$hoverCss = $this->prepare()->styles->getGeneralCss('hover'); |
| 447 |
$transitionCss['hover'] = $hoverCss['hover']; |
| 448 |
|
| 449 |
$this->selectorCssList[ '.' . $this->getStyleSelector() . ':not(.depicter-hover-off)' ] = $transitionCss; |
| 450 |
|
| 451 |
if ( !empty( $this->prepare()->styles->blendingMode ) ) { |
| 452 |
$this->selectorCssList[ '.' . $this->getFrameStyleSelector() ] = $this->prepare()->styles->getBlendingModeStyle(); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
if ( $this->getCustomStyles() ) { |
| 457 |
$this->selectorCssList[ '.' . $this->getStyleSelector() ]['customStyle'] = $this->getCustomStyles(); |
| 458 |
} |
| 459 |
|
| 460 |
return $this->selectorCssList; |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Retrieves custom styles of document |
| 465 |
* |
| 466 |
* @return string |
| 467 |
*/ |
| 468 |
protected function getCustomStyles(){ |
| 469 |
if ( ! is_null( $this->options ) && ! empty( $this->options->customStyle ) ) { |
| 470 |
$customStyles = $this->options->customStyle; |
| 471 |
// replace "selector" with unique selector of section |
| 472 |
return str_replace('selector', '.'.$this->getStyleSelector(), $customStyles ); |
| 473 |
} |
| 474 |
return ''; |
| 475 |
} |
| 476 |
|
| 477 |
/** |
| 478 |
* Get markup for preloading media urls |
| 479 |
* |
| 480 |
* @return string |
| 481 |
*/ |
| 482 |
public function getPreloadTags(){ |
| 483 |
if( method_exists( $this, 'getPreloadMarkup' ) ){ |
| 484 |
return $this->getPreloadMarkup(); |
| 485 |
} |
| 486 |
return ''; |
| 487 |
} |
| 488 |
|
| 489 |
} |
| 490 |
|