LayoutHelper.php
154 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core\Layout; |
| 4 | |
| 5 | use Kubio\Config; |
| 6 | use Kubio\Core\LodashBasic; |
| 7 | use Kubio\Core\Styles\FlexAlign; |
| 8 | use Kubio\Core\Styles\Utils; |
| 9 | |
| 10 | |
| 11 | class LayoutHelper { |
| 12 | |
| 13 | public static $prefixes; |
| 14 | public static $gridColumns = 12; |
| 15 | |
| 16 | public $rowLayoutByMedia; |
| 17 | public $layoutByMedia; |
| 18 | |
| 19 | const ROW_CLASS_PREFIX = 'h-row'; |
| 20 | const COLUMN_CLASS_PREFIX = 'h-col'; |
| 21 | |
| 22 | public static $ColumnWidthTypes; |
| 23 | public static function init() { |
| 24 | LayoutHelper::$prefixes = Config::value( 'definitions.layout.prefixes' ); |
| 25 | } |
| 26 | |
| 27 | public function __construct( $layoutByMedia = array(), $rowLayoutByMedia = array() ) { |
| 28 | $this->layoutByMedia = $layoutByMedia; |
| 29 | $this->rowLayoutByMedia = $rowLayoutByMedia; |
| 30 | |
| 31 | self::$ColumnWidthTypes = Config::value( 'props.columnWidth.enums.types' ); |
| 32 | } |
| 33 | |
| 34 | public function prefix( $path ) { |
| 35 | return LodashBasic::get( self::$prefixes, $path ); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | public function getSelfVAlignClasses() { |
| 40 | $verticalAlignByMedia = LodashBasic::mapValues( $this->layoutByMedia, 'verticalAlign' ); |
| 41 | $verticalAlignClasses = FlexAlign::getVAlignClasses( $verticalAlignByMedia, array( 'self' => true ) ); |
| 42 | return $verticalAlignClasses; |
| 43 | } |
| 44 | |
| 45 | public function getColumnLayoutClasses( $columnWidthByMedia ) { |
| 46 | $equalWidth = LodashBasic::get( $this->rowLayoutByMedia, array( 'desktop', 'equalWidth' ), false ); |
| 47 | if ( $equalWidth ) { |
| 48 | return $this->getColumnGridClasses(); |
| 49 | } |
| 50 | return $this->getColumnWidthClasses( $columnWidthByMedia ); |
| 51 | } |
| 52 | |
| 53 | public function getColumnGridClasses() { |
| 54 | $noColumnsByMedia = LodashBasic::mapValues( |
| 55 | $this->rowLayoutByMedia, |
| 56 | function ( $layout ) { |
| 57 | return round( self::$gridColumns / $layout['itemsPerRow'] ); |
| 58 | } |
| 59 | ); |
| 60 | return Utils::composeClassesByMedia( $noColumnsByMedia, self::COLUMN_CLASS_PREFIX ); |
| 61 | } |
| 62 | |
| 63 | public function getColumnWidthClasses( $columnWidthByMedia, $canUseHtml = true ) { |
| 64 | if ( ! $canUseHtml ) { |
| 65 | return array( 'h-col-none' ); |
| 66 | } |
| 67 | $classes = $this->computeColumnWidthClasses( $columnWidthByMedia ); |
| 68 | return $classes; |
| 69 | } |
| 70 | |
| 71 | public function computeColumnWidthClasses( $columnWidthByMedia ) { |
| 72 | $columnTypeToClass = Config::value( 'props.columnWidth.enums.typeToClass' ); |
| 73 | $widthClassesByMedia = LodashBasic::mapValues( |
| 74 | $columnWidthByMedia, |
| 75 | function ( $width ) use ( $columnTypeToClass ) { |
| 76 | $type = LodashBasic::get( $width, 'type' ); |
| 77 | return LodashBasic::get( $columnTypeToClass, $type, '' ); |
| 78 | } |
| 79 | ); |
| 80 | return Utils::composeClassesByMedia( $widthClassesByMedia, self::COLUMN_CLASS_PREFIX, true ); |
| 81 | } |
| 82 | |
| 83 | public function getInheritedColumnVAlignClasses() { |
| 84 | $verticalAlignByMedia_ = LodashBasic::mapValues( $this->rowLayoutByMedia, 'verticalAlign' ); |
| 85 | $equalHeightByMedia = LodashBasic::mapValues( $this->rowLayoutByMedia, 'equalHeight' ); |
| 86 | $verticalAlignByMedia = array(); |
| 87 | foreach ( $equalHeightByMedia as $media => $stretch ) { |
| 88 | if ( ! $stretch ) { |
| 89 | $verticalAlignByMedia[ $media ] = $verticalAlignByMedia_[ $media ]; |
| 90 | } |
| 91 | } |
| 92 | $verticalAlignClasses = FlexAlign::getVAlignClasses( $verticalAlignByMedia, array( 'self' => true ) ); |
| 93 | return $verticalAlignClasses; |
| 94 | } |
| 95 | |
| 96 | public function getRowAlignClasses() { |
| 97 | $verticalAlignByMedia_ = LodashBasic::mapValues( $this->layoutByMedia, 'verticalAlign' ); |
| 98 | $equalHeightByMedia = LodashBasic::mapValues( $this->layoutByMedia, 'equalHeight' ); |
| 99 | $verticalAlignByMedia = $verticalAlignByMedia_; |
| 100 | foreach ( $equalHeightByMedia as $media => $stretch ) { |
| 101 | if ( $stretch ) { |
| 102 | $verticalAlignByMedia[ $media ] = 'stretch'; |
| 103 | } |
| 104 | } |
| 105 | $hAlignByMedia = LodashBasic::mapValues( $this->layoutByMedia, 'horizontalAlign' ); |
| 106 | $classes = LodashBasic::concat( FlexAlign::getVAlignClasses( $verticalAlignByMedia ), FlexAlign::getHAlignClasses( $hAlignByMedia ) ); |
| 107 | return $classes; |
| 108 | } |
| 109 | |
| 110 | public function getRowGapClasses() { |
| 111 | return $this->mapGapClasses( $this->prefix( 'row.outer' ), $this->layoutByMedia ); |
| 112 | } |
| 113 | |
| 114 | public function mapGapClasses( $propsToSuffix, $layoutByMedia, $inheritedLayoutByMedia = array() ) { |
| 115 | $classes = array(); |
| 116 | $mergedLayoutByMedia = LodashBasic::merge( $inheritedLayoutByMedia, $layoutByMedia ); |
| 117 | foreach ( $mergedLayoutByMedia as $media => $layout ) { |
| 118 | foreach ( $propsToSuffix as $path => $gapSuffix ) { |
| 119 | $value = LodashBasic::get( $layout, $path, null ); |
| 120 | if ( $value === 'inherit' ) { |
| 121 | $inheritedLayoutInMedia = LodashBasic::get( $inheritedLayoutByMedia, $media ); |
| 122 | $inheritedValue = LodashBasic::get( $inheritedLayoutInMedia, $path ); |
| 123 | $value = $inheritedValue; |
| 124 | } |
| 125 | if ( $value !== null ) { |
| 126 | $classes = LodashBasic::concat( $classes, Utils::composeClassForMedia( $media, $value, $gapSuffix ) ); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | return $classes; |
| 131 | } |
| 132 | public function getRowGapInnerClasses() { |
| 133 | return $this->mapGapClasses( $this->prefix( 'row.inner' ), $this->layoutByMedia ); |
| 134 | } |
| 135 | |
| 136 | public function getColumnInnerGapsClasses() { |
| 137 | return $this->mapGapClasses( $this->prefix( 'column' ), $this->layoutByMedia, $this->rowLayoutByMedia ); |
| 138 | } |
| 139 | |
| 140 | public function getColumnWidthType( $equalWidth, $columnWidth ) { |
| 141 | return $equalWidth ? self::$ColumnWidthTypes['EQUAL_WIDTH_COLUMNS'] : $columnWidth['type']; |
| 142 | } |
| 143 | |
| 144 | public function getColumnContentFlexBasis( $equalWidth, $columnWidth ) { |
| 145 | switch ( $this->getColumnWidthType( $equalWidth, $columnWidth ) ) { |
| 146 | case self::$ColumnWidthTypes['FIT_TO_CONTENT']: |
| 147 | return 'flex-basis-auto'; |
| 148 | default: |
| 149 | return 'flex-basis-100'; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | LayoutHelper::init(); |
| 154 |