| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* StackLayouts contain a series of PanelLayouts |
| 7 |
*/ |
| 8 |
class StackLayout extends PanelLayout { |
| 9 |
|
| 10 |
use GroupElement; |
| 11 |
|
| 12 |
/** @var bool */ |
| 13 |
protected $continuous; |
| 14 |
/** @var PanelLayout|null */ |
| 15 |
protected $currentItem; |
| 16 |
|
| 17 |
/** |
| 18 |
* @param array $config Configuration options |
| 19 |
* - bool $config['continuous'] Show all panels, one after another (default: false) |
| 20 |
* - PanelLayout[] $config['items'] Panel layouts to add to the stack layout. |
| 21 |
*/ |
| 22 |
public function __construct( array $config = [] ) { |
| 23 |
$config = array_merge( [ |
| 24 |
'preserveContent' => false, |
| 25 |
'scrollable' => $config['continuous'] ?? false |
| 26 |
], $config ); |
| 27 |
|
| 28 |
// Parent constructor |
| 29 |
parent::__construct( $config ); |
| 30 |
|
| 31 |
// Properties |
| 32 |
$this->continuous = $config['continuous'] ?? false; |
| 33 |
|
| 34 |
// Traits |
| 35 |
$this->initializeGroupElement( array_merge( $config, [ 'group' => $this ] ) ); |
| 36 |
|
| 37 |
// Initialization |
| 38 |
$this->addClasses( [ 'oo-ui-stackLayout' ] ); |
| 39 |
if ( $this->continuous ) { |
| 40 |
$this->addClasses( [ 'oo-ui-stackLayout-continuous' ] ); |
| 41 |
} |
| 42 |
$this->addItems( $config['items'] ?? [] ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @param PanelLayout|null $item |
| 47 |
*/ |
| 48 |
public function setItem( $item ) { |
| 49 |
if ( $item !== $this->currentItem ) { |
| 50 |
$items = $this->getItems(); |
| 51 |
$this->updateHiddenState( $items, $item ); |
| 52 |
$this->currentItem = $item; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @param Element[] $items |
| 58 |
* @param PanelLayout|null $selectedItem |
| 59 |
*/ |
| 60 |
private function updateHiddenState( $items, $selectedItem ) { |
| 61 |
if ( !$this->continuous ) { |
| 62 |
foreach ( $items as $item ) { |
| 63 |
if ( !$selectedItem || $selectedItem !== $item ) { |
| 64 |
$item->toggle( false ); |
| 65 |
$item->setAttributes( [ 'aria-hidden' => 'true' ] ); |
| 66 |
} |
| 67 |
} |
| 68 |
if ( $selectedItem ) { |
| 69 |
$selectedItem->toggle( true ); |
| 70 |
$selectedItem->removeAttributes( [ 'aria-hidden' ] ); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** @inheritDoc */ |
| 76 |
public function getConfig( &$config ) { |
| 77 |
$config = parent::getConfig( $config ); |
| 78 |
if ( $this->continuous ) { |
| 79 |
$config['continuous'] = $this->continuous; |
| 80 |
// scrollable default has changed to true |
| 81 |
if ( !$this->hasClass( 'oo-ui-panelLayout-scrollable' ) ) { |
| 82 |
$config['scrollable'] = false; |
| 83 |
} else { |
| 84 |
unset( $config['scrollable'] ); |
| 85 |
} |
| 86 |
} |
| 87 |
return $config; |
| 88 |
} |
| 89 |
|
| 90 |
} |