PluginProbe
Depicter — Popup & Slider Builder / 1.2.0
Depicter — Popup & Slider Builder v1.2.0
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Document / Models / Element.php

Element.php in Depicter — Popup & Slider Builder 1.2.0, at app/src/Document/Models/Element.php

468 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 object|null
96 */
97 public $options;
98
99 /**
100 * @var \Depicter\Document\Models\Common\Animation|null
101 */
102 public $animation;
103
104 /**
105 * @var \Depicter\Document\Models\Common\Parallax|null
106 */
107 public $parallax;
108
109 /**
110 * @var array|null
111 */
112 public $actions;
113
114 /**
115 * @var int
116 */
117 public $depth;
118
119 /**
120 * @var array
121 */
122 public $devices;
123
124 /**
125 * Selector and CSS list
126 *
127 * @var array
128 */
129 protected $selectorCssList = [];
130
131 /**
132 * Instance of Element type or base Element class
133 *
134 * @var Element
135 */
136 protected $alias;
137
138 /**
139 * @var object|null
140 */
141 public $cropData;
142
143 /**
144 * @var \Depicter\Document\Models\Common\ResponsiveScale|null
145 */
146 public $responsiveScale;
147
148 /**
149 * @var string
150 */
151 protected $markup;
152
153
154
155 /**
156 * Element constructor.
157 */
158 public function __construct() {
159 $this->devices = Breakpoints::names();
160 }
161
162 /**
163 * get element number
164 *
165 * @return string
166 */
167 public function getID() {
168 return Helper::getElementIdFromSlug( $this->id );
169 }
170
171 /**
172 * get element slug
173 *
174 * @return string
175 */
176 public function getSlug() {
177 return $this->id;
178 }
179
180 /**
181 * Get element name
182 *
183 * @return string
184 */
185 public function getName() {
186 return $this->name ? $this->name : $this->id;
187 }
188
189 /**
190 * Get section ID number
191 *
192 * @return int
193 */
194 public function getSectionID() {
195 return Helper::getSectionIdFromSlug( $this->section );
196 }
197
198 /**
199 * Return child elements IDs for group type element
200 * @return array|null
201 */
202 public function getElementIds() {
203 return $this->children;
204 }
205
206 /**
207 * Sets belonging elements for group type element
208 *
209 * @param array $elementObjects
210 */
211 public function setElementObjects( $elementObjects )
212 {
213 $this->childrenObjects = $elementObjects;
214 }
215
216 /**
217 * Prepare class for rendering markup
218 *
219 * @return Element
220 * @throws \JsonMapper_Exception
221 */
222 public function prepare() {
223 return $this->alias();
224 }
225
226 /**
227 * Check if unique element class exist then render element through that class
228 *
229 * @return Element
230 * @throws \JsonMapper_Exception
231 */
232 public function alias(){
233 if( $this->alias ){
234 return $this->alias;
235 }
236
237 $className = '\\Depicter\\Document\\Models\\Elements\\' . ucfirst( $this->type );
238 if ( class_exists( $className ) ) {
239 $mapper = new \JsonMapper();
240 $this->alias = $mapper->map( $this, new $className() );
241 // pass the document ID to new class too
242 $this->alias->setDocumentID( $this->getDocumentID() );
243
244 return $this->alias;
245 }
246
247 return $this;
248 }
249
250 public function render() {}
251
252 public function getClassNames() {
253 $classes = [];
254
255 $classes[] = Selector::prefixify('element');
256 $classes[] = Selector::prefixify('layer');
257
258 $classes[] = $this->getSelector();
259 $classes[] = $this->getDataSheetClassName();
260
261 if ( isset( $this->visible->default ) && $this->visible->default === false ) {
262 $classes[] = 'depicter-hide-on-desktop ';
263 }
264
265 if ( isset( $this->visible->tablet ) && $this->visible->tablet === false ) {
266 $classes[] = 'depicter-hide-on-tablet ';
267 }
268
269 if ( isset( $this->visible->mobile ) && $this->visible->mobile === false ) {
270 $classes[] = 'depicter-hide-on-mobile ';
271 }
272
273 return trim( implode(' ', $classes) );
274 }
275
276 /**
277 * Get default data attributes
278 *
279 * @return array
280 * @throws \JsonMapper_Exception
281 */
282 public function getDefaultAttributes() {
283 $dataAttrs = [
284 'id' => $this->getCssID(),
285 'class' => $this->getClassNames(),
286 'data-type' => $this->type,
287 'data-wrap' => !!$this->wrap ? "true" : "false",
288 'data-name' => $this->getName()
289 ];
290
291 if( ! empty( $this->position->getOffset("default" ) ) ){
292 $dataAttrs['data-offset'] = $this->position->getOffset("default" );
293 }
294 if( ! empty( $this->position->getOffset("tablet" ) ) ){
295 $dataAttrs['data-tablet-offset'] = $this->position->getOffset("tablet" );
296 }
297 if( ! empty( $this->position->getOffset("mobile" ) ) ){
298 $dataAttrs['data-mobile-offset'] = $this->position->getOffset("mobile" );
299 }
300
301 if( $this->hideOnSections && $hideOnSections = Helper::getInvisibleSectionsCssIdList( $this->hideOnSections, $this->getDocumentID() ) ){
302 $dataAttrs['data-hide-on-sections'] = $hideOnSections;
303 }
304
305 if ( $actions = Helper::getActions( $this->actions ) ) {
306 $dataAttrs['data-actions'] = $actions;
307 }
308 if ( $this->animation && false !== $animationData = $this->animation->getAnimationAttrs() ) {
309 $dataAttrs = Arr::merge( $dataAttrs, $animationData );
310 }
311
312 if ( $this->parallax && false !== $parallaxData = $this->parallax->getParallaxAttrs() ) {
313 $dataAttrs = Arr::merge( $dataAttrs, $parallaxData );
314 }
315
316 if( ! empty( $this->size ) ){
317 $dataAttrs['data-width' ] = implode( ',', $this->size->getResponsiveSizes( 'width' , true ) );
318 $dataAttrs['data-height'] = implode( ',', $this->size->getResponsiveSizes( 'height', true ) );
319 }
320
321 $dataAttrs['data-responsive-scale' ] = ! empty( $this->responsiveScale ) ? implode( ',', $this->responsiveScale->getResponsiveSizes() ) : 'true,,';
322
323 if( ! empty( $this->prepare()->styles->hover ) && $hoverOffDevices = $this->prepare()->styles->hover->getDisabledDeviceList() ){
324 $dataAttrs['data-hover-off' ] = implode( ',', $hoverOffDevices );
325 }
326
327 if ( ! empty( $this->prepare()->styles->blendingMode ) ) {
328 $dataAttrs['data-frame-class'] = $this->getSelector() . '-frame' ;
329 }
330
331 return $dataAttrs;
332 }
333
334 /**
335 * Get section CSS ID for data sheet if dataSource is assigned
336 *
337 * @return string
338 */
339 public function getDataSheetClassName() {
340 if( $this->getDataSheetID() ){
341 return $this->getCssID();
342 }
343 return '';
344 }
345
346 /**
347 * Check if element has link
348 * @return false|\TypeRocket\Html\Html
349 */
350 public function getLinkTag() {
351 if ( !empty( $this->options->url->path ) ) {
352 $urlPath = $this->maybeReplaceDataSheetTags( $this->options->url->path );
353 $urlArgs = isset( $this->options->url->openInNewTab ) && empty( $this->options->url->openInNewTab ) ? [] : ['target' => '_blank'];
354 return Html::a( '', $urlPath, $urlArgs );
355 }
356
357 return false;
358 }
359
360 /**
361 * Retrieves list of fonts used in typography options
362 *
363 * @return array
364 * @throws \JsonMapper_Exception
365 */
366 public function getFontsList()
367 {
368 $fontsList = ! empty( $this->prepare()->styles ) ? $this->prepare()->styles->getFontsList() : [];
369 \Depicter::app()->documentFonts()->addFonts( $this->getDocumentID(), $fontsList, 'google' );
370
371 return $fontsList;
372 }
373
374 /**
375 * Retrieves the content of element
376 *
377 * @return string
378 */
379 protected function getContent(){
380 return $this->options->content ?? '';
381 }
382
383 /**
384 * Get element CSS ID
385 *
386 * @return string
387 */
388 public function getCssID() {
389 $cssID = Selector::getFullSelectorPath( $this->getDocumentID(), null, $this->getID() );
390 if( $dataSheetId = $this->getDataSheetID() ){
391 $cssID .= "-{$dataSheetId}";
392 }
393
394 return $cssID;
395 }
396
397 /**
398 * Get element selector
399 *
400 * @return string
401 */
402 public function getSelector() {
403 return Selector::getUniqueSelector( $this->getDocumentID(), $this->getSectionID(), $this->getID(), Selector::ELEMENT_PREFIX );
404 }
405
406 /**
407 * Get element style selector
408 *
409 * @return string
410 */
411 public function getStyleSelector() {
412 return Selector::PREFIX_CSS . " ." . $this->getSelector();
413 }
414
415 /**
416 * Get element frame selector
417 *
418 * @return string
419 */
420 public function getFrameStyleSelector() {
421 return Selector::PREFIX_CSS . ' .' . $this->getSelector() . '-frame';
422 }
423
424 /**
425 * Get list of selector and CSS for element
426 *
427 * @return array
428 * @throws \JsonMapper_Exception
429 */
430 public function getSelectorAndCssList(){
431
432 if( ! empty( $this->prepare()->styles ) ){
433 $this->selectorCssList[ '.' . $this->getStyleSelector() ] = $this->prepare()->styles->getGeneralCss('normal');
434
435 $transitionCss = $this->prepare()->styles->getTransitionCss();
436 $hoverCss = $this->prepare()->styles->getGeneralCss('hover');
437 $transitionCss['hover'] = $hoverCss['hover'];
438
439 $this->selectorCssList[ '.' . $this->getStyleSelector() . ':not(.depicter-hover-off)' ] = $transitionCss;
440
441 if ( !empty( $this->prepare()->styles->blendingMode ) ) {
442 $this->selectorCssList[ '.' . $this->getFrameStyleSelector() ] = $this->prepare()->styles->getBlendingModeStyle();
443 }
444 }
445
446 if ( $this->getCustomStyles() ) {
447 $this->selectorCssList[ '.' . $this->getStyleSelector() ]['customStyle'] = $this->getCustomStyles();
448 }
449
450 return $this->selectorCssList;
451 }
452
453 /**
454 * Retrieves custom styles of document
455 *
456 * @return string
457 */
458 protected function getCustomStyles(){
459 if ( ! is_null( $this->options ) && ! empty( $this->options->customStyle ) ) {
460 $customStyles = $this->options->customStyle;
461 // replace "selector" with unique selector of section
462 return str_replace('selector', '.'.$this->getStyleSelector(), $customStyles );
463 }
464 return '';
465 }
466
467 }
468