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