Background.php
1 year ago
BackgroundDefaults.php
2 years ago
BackgroundImage.php
1 year ago
BackgroundOverlay.php
1 year ago
BackgroundSlideshow.php
1 year ago
BackgroundVideo.php
1 year ago
BackgroundOverlay.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core\Background; |
| 4 | use Kubio\Core\Element; |
| 5 | use Kubio\Core\ElementBase; |
| 6 | use function array_push; |
| 7 | |
| 8 | class BackgroundOverlay extends ElementBase { |
| 9 | const SHAPE_NONE = 'none'; |
| 10 | |
| 11 | function __construct( $value ) { |
| 12 | parent::__construct( $value, BackgroundDefaults::getDefaultOverlay() ); |
| 13 | } |
| 14 | |
| 15 | function getOverlayShapeStyle() { |
| 16 | $tile_style = array( |
| 17 | 'backgroundPosition' => 'top left', |
| 18 | 'backgroundRepeat' => 'repeat', |
| 19 | ); |
| 20 | $non_tile = array( |
| 21 | 'backgroundPosition' => 'center center', |
| 22 | 'backgroundRepeat' => 'no-repeat', |
| 23 | 'backgroundSize' => 'cover', |
| 24 | ); |
| 25 | $style = $this->get( 'shape.isTile' ) ? $tile_style : $non_tile; |
| 26 | |
| 27 | $style['filter'] = 'invert(' . $this->get( 'shape.light' ) . '%)'; |
| 28 | return $style; |
| 29 | } |
| 30 | |
| 31 | function getOverlayLayerComputedStyle() { |
| 32 | $style = array(); |
| 33 | switch ( $this->get( 'type' ) ) { |
| 34 | case 'color': |
| 35 | $style = array( |
| 36 | 'backgroundColor' => $this->get( 'color.value' ), |
| 37 | 'opacity' => $this->get( 'color.opacity' ), |
| 38 | ); |
| 39 | break; |
| 40 | case 'gradient': |
| 41 | $style = array( |
| 42 | 'backgroundImage' => $this->get( 'gradient' ), |
| 43 | ); |
| 44 | break; |
| 45 | } |
| 46 | return $style; |
| 47 | } |
| 48 | |
| 49 | function showShape() { |
| 50 | return $this->get( 'shape.value' ) !== self::SHAPE_NONE; |
| 51 | } |
| 52 | |
| 53 | function getShapeLayerClasses() { |
| 54 | $classes = array( 'shape-layer' ); |
| 55 | if ( $this->showShape() ) { |
| 56 | array_push( $classes, 'kubio-shape-' . $this->get( 'shape.value' ) ); |
| 57 | } |
| 58 | return $classes; |
| 59 | } |
| 60 | |
| 61 | function __toString() { |
| 62 | if ( ! $this->get( 'enabled' ) ) { |
| 63 | return ''; |
| 64 | } |
| 65 | |
| 66 | $shape = $this->showShape() ? new Element( |
| 67 | Element::DIV, |
| 68 | array( |
| 69 | 'style' => $this->getOverlayShapeStyle(), |
| 70 | 'className' => $this->getShapeLayerClasses(), |
| 71 | ), |
| 72 | array() |
| 73 | ) : null; |
| 74 | |
| 75 | return new Element( |
| 76 | Element::DIV, |
| 77 | array( |
| 78 | 'className' => 'overlay-layer', |
| 79 | ), |
| 80 | array( |
| 81 | new Element( |
| 82 | Element::DIV, |
| 83 | array( |
| 84 | 'className' => 'overlay-image-layer', |
| 85 | 'style' => $this->getOverlayLayerComputedStyle(), |
| 86 | ) |
| 87 | ), |
| 88 | $shape, |
| 89 | ) |
| 90 | ) . ''; |
| 91 | } |
| 92 | } |
| 93 |