| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* IndexLayouts contain TabPanelLayout layouts as well as TabSelectWidget tabs that allow users |
| 7 |
* to navigate through the tab panels and select which one to display. |
| 8 |
* |
| 9 |
* Default php rendering shows all the tabs |
| 10 |
*/ |
| 11 |
class IndexLayout extends MenuLayout { |
| 12 |
|
| 13 |
/** |
| 14 |
* @var StackLayout |
| 15 |
*/ |
| 16 |
protected $stackLayout; |
| 17 |
/** |
| 18 |
* @var PanelLayout |
| 19 |
*/ |
| 20 |
protected $tabPanel; |
| 21 |
/** |
| 22 |
* @var TabPanelLayout[] |
| 23 |
*/ |
| 24 |
protected $tabPanels; |
| 25 |
/** |
| 26 |
* @var TabSelectWidget |
| 27 |
*/ |
| 28 |
protected $tabSelectWidget; |
| 29 |
/** |
| 30 |
* @var bool |
| 31 |
*/ |
| 32 |
protected $autoFocus; |
| 33 |
/** |
| 34 |
* @var bool |
| 35 |
*/ |
| 36 |
protected $continuous; |
| 37 |
/** |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
protected $currentTabPanelName; |
| 41 |
|
| 42 |
/** |
| 43 |
* @param array $config Configuration options |
| 44 |
* - bool $config['continuous'] Focus on the first focusable element when a new tab panel is |
| 45 |
* displayed. Disabled on mobile. (default: false) |
| 46 |
* - bool $config['autoFocus'] (default: true) |
| 47 |
* - bool $config['framed'] (default: true) |
| 48 |
*/ |
| 49 |
public function __construct( array $config = [] ) { |
| 50 |
$config = array_merge( |
| 51 |
$config, |
| 52 |
[ 'menuPosition' => 'top' ] |
| 53 |
); |
| 54 |
|
| 55 |
parent::__construct( $config ); |
| 56 |
|
| 57 |
$this->tabPanels = []; |
| 58 |
$this->continuous = $config['continuous'] ?? false; |
| 59 |
|
| 60 |
$this->stackLayout = $this->contentPanel ?? new StackLayout( [ |
| 61 |
'continuous' => $this->continuous, |
| 62 |
'expanded' => $this->expanded |
| 63 |
] ); |
| 64 |
$this->setContentPanel( $this->stackLayout ); |
| 65 |
$this->autoFocus = $config['autoFocus'] ?? true; |
| 66 |
|
| 67 |
$this->tabSelectWidget = new TabSelectWidget( [ |
| 68 |
'framed' => $config['framed'] ?? true |
| 69 |
] ); |
| 70 |
$this->tabPanel = $this->menuPanel ?? new PanelLayout( [ |
| 71 |
'expanded' => $this->expanded, |
| 72 |
'preserveContent' => false |
| 73 |
] ); |
| 74 |
$this->setMenuPanel( $this->tabPanel ); |
| 75 |
|
| 76 |
$this->toggleMenu( true ); |
| 77 |
|
| 78 |
$this->addClasses( [ 'oo-ui-indexLayout' ] ); |
| 79 |
$this->stackLayout->addClasses( [ 'oo-ui-indexLayout-stackLayout' ] ); |
| 80 |
$this->tabPanel |
| 81 |
->addClasses( [ 'oo-ui-indexLayout-tabPanel' ] ) |
| 82 |
->appendContent( $this->tabSelectWidget ); |
| 83 |
} |
| 84 |
|
| 85 |
/** @inheritDoc */ |
| 86 |
public function getConfig( &$config ) { |
| 87 |
$config = parent::getConfig( $config ); |
| 88 |
if ( !$this->autoFocus ) { |
| 89 |
$config['autoFocus'] = $this->autoFocus; |
| 90 |
} |
| 91 |
if ( $this->continuous ) { |
| 92 |
$config['continuous'] = $this->continuous; |
| 93 |
} |
| 94 |
if ( count( $this->tabPanels ) ) { |
| 95 |
$config['tabPanels'] = $this->tabPanels; |
| 96 |
} |
| 97 |
// menuPosition is not configurable |
| 98 |
unset( $config['menuPosition'] ); |
| 99 |
$config['tabSelectWidget'] = $this->tabSelectWidget; |
| 100 |
// stackLayout and tabPanel are identical to |
| 101 |
// contentPanel and menuPanel in MenuLayout |
| 102 |
return $config; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get the tabs widget. |
| 107 |
* |
| 108 |
* @return TabSelectWidget Tabs widget |
| 109 |
*/ |
| 110 |
public function getTabs() { |
| 111 |
return $this->tabSelectWidget; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Get a tab panel by its symbolic name. |
| 116 |
* |
| 117 |
* @param string $name Symbolic name of table panel |
| 118 |
* @return TabPanelLayout|null Tab panel, if found |
| 119 |
*/ |
| 120 |
public function getTabPanel( $name ): ?TabPanelLayout { |
| 121 |
return $this->tabPanels[$name] ?? null; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @return TabPanelLayout|null |
| 126 |
*/ |
| 127 |
public function getCurrentTabPanel() { |
| 128 |
$name = $this->getCurrentTabPanelName(); |
| 129 |
return $name ? $this->getTabPanel( $name ) : null; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
public function getCurrentTabPanelName() { |
| 136 |
return $this->currentTabPanelName; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Add tab panels to the index layout |
| 141 |
* |
| 142 |
* When tab panels are added with the same names as existing tab panels, the existing tab panels |
| 143 |
* will be automatically removed before the new tab panels are added. |
| 144 |
* |
| 145 |
* @param TabPanelLayout[] $tabPanels Tab panels to add |
| 146 |
*/ |
| 147 |
public function addTabPanels( array $tabPanels ) { |
| 148 |
$tabItems = []; |
| 149 |
foreach ( $tabPanels as $tabPanel ) { |
| 150 |
$this->tabPanels[ $tabPanel->getName() ] = $tabPanel; |
| 151 |
$tabItem = new TabOptionWidget( array_merge( [ |
| 152 |
'data' => $tabPanel->getName(), |
| 153 |
'label' => $tabPanel->getLabel(), |
| 154 |
], $tabPanel->getTabItemConfig() ) ); |
| 155 |
// TODO: Set aria-labelledby/aria-controls as in .js |
| 156 |
$tabItems[] = $tabItem; |
| 157 |
} |
| 158 |
$this->tabSelectWidget->addItems( $tabItems ); |
| 159 |
$this->stackLayout->addItems( $tabPanels ); |
| 160 |
|
| 161 |
// Select the first item |
| 162 |
$this->getTabs()->selectItem( $tabItems[ 0 ] ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Set the current tab panel by symbolic name. |
| 167 |
* |
| 168 |
* @param string $name Symbolic name of tab panel |
| 169 |
*/ |
| 170 |
public function setTabPanel( $name ) { |
| 171 |
if ( $name !== $this->currentTabPanelName ) { |
| 172 |
$tabPanel = $this->getTabPanel( $name ); |
| 173 |
$previousTabPanel = $this->getCurrentTabPanel(); |
| 174 |
$selectedItem = $this->getTabs()->findSelectedItem(); |
| 175 |
if ( !$selectedItem || $selectedItem->getData() !== $name ) { |
| 176 |
$this->getTabs()->selectItemByData( $name ); |
| 177 |
} |
| 178 |
if ( $tabPanel ) { |
| 179 |
if ( $previousTabPanel ) { |
| 180 |
$previousTabPanel->setActive( false ); |
| 181 |
} |
| 182 |
$this->currentTabPanelName = $name; |
| 183 |
$tabPanel->setActive( true ); |
| 184 |
$this->stackLayout->setItem( $tabPanel ); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
} |