| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* MenuLayouts combine a menu and a content PanelLayout panel. |
| 7 |
* |
| 8 |
* The menu is positioned relative to the content (after, before, top, or bottom) |
| 9 |
*/ |
| 10 |
class MenuLayout extends Layout { |
| 11 |
|
| 12 |
/** |
| 13 |
* @var Tag |
| 14 |
*/ |
| 15 |
protected $menuWrapper; |
| 16 |
/** |
| 17 |
* @var Tag |
| 18 |
*/ |
| 19 |
protected $contentWrapper; |
| 20 |
/** |
| 21 |
* @var ?PanelLayout |
| 22 |
*/ |
| 23 |
protected $menuPanel; |
| 24 |
/** |
| 25 |
* @var ?PanelLayout |
| 26 |
*/ |
| 27 |
protected $contentPanel; |
| 28 |
/** |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $menuPosition; |
| 32 |
/** |
| 33 |
* @var bool |
| 34 |
*/ |
| 35 |
protected $expanded; |
| 36 |
|
| 37 |
/** |
| 38 |
* @param array $config Configuration options |
| 39 |
* - PanelLayout $config['menuPanel'] Menu panel |
| 40 |
* - PanelLayout $config['contentPanel'] Content panel |
| 41 |
* - bool $config['expanded'] Expand content to fill the parent element (default: true) |
| 42 |
* - bool $config['showMenu'] Show menu (default: true) |
| 43 |
* - string $config['menuPosition'] top, after, bottom, before (default: before) |
| 44 |
* @phpcs:ignore Generic.Files.LineLength |
| 45 |
* @phan-param array{menuPanel:PanelLayout,contentPanel:PanelLayout,expanded?:bool,showMenu?:bool,menuPosition?:string} $config |
| 46 |
*/ |
| 47 |
public function __construct( array $config = [] ) { |
| 48 |
parent::__construct( $config ); |
| 49 |
|
| 50 |
$this->menuPanel = null; |
| 51 |
$this->contentPanel = null; |
| 52 |
$this->expanded = $config['expanded'] ?? true; |
| 53 |
$this->menuWrapper = new Tag( 'div' ); |
| 54 |
$this->contentWrapper = new Tag( 'div' ); |
| 55 |
|
| 56 |
$this->menuWrapper->addClasses( [ 'oo-ui-menuLayout-menu' ] ); |
| 57 |
$this->contentWrapper->addClasses( [ 'oo-ui-menuLayout-content' ] ); |
| 58 |
$this->addClasses( [ |
| 59 |
'oo-ui-menuLayout', |
| 60 |
$this->expanded ? 'oo-ui-menuLayout-expanded' : 'oo-ui-menuLayout-static', |
| 61 |
] ); |
| 62 |
if ( !empty( $config['menuPanel'] ) ) { |
| 63 |
$this->setMenuPanel( $config['menuPanel'] ); |
| 64 |
} |
| 65 |
if ( !empty( $config['contentPanel'] ) ) { |
| 66 |
$this->setContentPanel( $config['contentPanel'] ); |
| 67 |
} |
| 68 |
$this->setMenuPosition( $config['menuPosition'] ?? 'before' ); |
| 69 |
$this->toggleMenu( (bool)( $config['showMenu'] ?? true ) ); |
| 70 |
} |
| 71 |
|
| 72 |
/** @inheritDoc */ |
| 73 |
public function getConfig( &$config ) { |
| 74 |
$config = parent::getConfig( $config ); |
| 75 |
if ( $this->menuPosition !== 'before' ) { |
| 76 |
$config['menuPosition'] = $this->menuPosition; |
| 77 |
} |
| 78 |
if ( !$this->expanded ) { |
| 79 |
$config['expanded'] = $this->expanded; |
| 80 |
} |
| 81 |
$showMenu = $this->hasClass( 'oo-ui-menuLayout-showMenu' ); |
| 82 |
if ( !$showMenu ) { |
| 83 |
$config['showMenu'] = $showMenu; |
| 84 |
} |
| 85 |
if ( $this->menuPanel ) { |
| 86 |
$config['menuPanel'] = $this->menuPanel; |
| 87 |
} |
| 88 |
if ( $this->contentPanel ) { |
| 89 |
$config['contentPanel'] = $this->contentPanel; |
| 90 |
} |
| 91 |
return $config; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* @param bool $showMenu |
| 96 |
*/ |
| 97 |
public function toggleMenu( $showMenu ) { |
| 98 |
$this->toggleClasses( [ 'oo-ui-menuLayout-showMenu' ], $showMenu ); |
| 99 |
$this->toggleClasses( [ 'oo-ui-menuLayout-hideMenu' ], !$showMenu ); |
| 100 |
$this->menuWrapper->setAttributes( [ |
| 101 |
'aria-hidden' => $showMenu ? 'false' : 'true' |
| 102 |
] ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @param string $position |
| 107 |
*/ |
| 108 |
public function setMenuPosition( $position ) { |
| 109 |
if ( !in_array( $position, [ 'top', 'bottom', 'before', 'after' ], true ) ) { |
| 110 |
$position = 'before'; |
| 111 |
} |
| 112 |
|
| 113 |
$this->removeClasses( [ 'oo-ui-menuLayout-' . $this->menuPosition ] ); |
| 114 |
$this->menuPosition = $position; |
| 115 |
$this->clearContent(); |
| 116 |
if ( $this->menuPosition == 'top' || $this->menuPosition == 'before' ) { |
| 117 |
$this->appendContent( $this->menuWrapper, $this->contentWrapper ); |
| 118 |
} else { |
| 119 |
$this->appendContent( $this->contentWrapper, $this->menuWrapper ); |
| 120 |
} |
| 121 |
$this->addClasses( [ 'oo-ui-menuLayout-' . $position ] ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @param PanelLayout $menuPanel |
| 126 |
*/ |
| 127 |
public function setMenuPanel( PanelLayout $menuPanel ) { |
| 128 |
$this->menuPanel = $menuPanel; |
| 129 |
$this->menuWrapper->appendContent( $this->menuPanel ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @param PanelLayout $contentPanel |
| 134 |
*/ |
| 135 |
public function setContentPanel( PanelLayout $contentPanel ) { |
| 136 |
$this->contentPanel = $contentPanel; |
| 137 |
$this->contentWrapper->appendContent( $this->contentPanel ); |
| 138 |
} |
| 139 |
} |