| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* TabPanelLayouts are used within IndexLayouts to create tab panels that |
| 7 |
* users can select and display from the index's optional TabSelectWidget |
| 8 |
* navigation. TabPanels are usually not instantiated directly, rather extended to include the |
| 9 |
* required content and functionality. |
| 10 |
* |
| 11 |
* Each tab panel must have a unique symbolic name, which is passed to the constructor. |
| 12 |
*/ |
| 13 |
class TabPanelLayout extends PanelLayout { |
| 14 |
|
| 15 |
/** |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
protected $name; |
| 19 |
/** |
| 20 |
* @var string|HtmlSnippet|null |
| 21 |
*/ |
| 22 |
protected $label; |
| 23 |
/** |
| 24 |
* @var bool |
| 25 |
*/ |
| 26 |
protected $active; |
| 27 |
/** |
| 28 |
* @var array Config for a {@see TabOptionWidget} |
| 29 |
*/ |
| 30 |
protected $tabItemConfig; |
| 31 |
|
| 32 |
/** |
| 33 |
* @param string $name Unique symbolic name of tab panel |
| 34 |
* @param array $config Configuration options |
| 35 |
* - string|HtmlSnippet $config['label'] Label for tab panel's tab |
| 36 |
* - array $config['tabItemConfig'] Additional config for the {@see TabOptionWidget} that |
| 37 |
* represents this panel in an {@see IndexLayout} |
| 38 |
*/ |
| 39 |
public function __construct( $name, array $config = [] ) { |
| 40 |
// Allow passing positional parameters inside the config array |
| 41 |
if ( is_array( $name ) && isset( $name['name'] ) ) { |
| 42 |
$config = $name; |
| 43 |
$name = $config['name']; |
| 44 |
} |
| 45 |
|
| 46 |
$config = array_merge( [ 'scrollable' => true ], $config ); |
| 47 |
|
| 48 |
// Parent constructor |
| 49 |
parent::__construct( $config ); |
| 50 |
|
| 51 |
// Initialization |
| 52 |
$this->name = $name; |
| 53 |
$this->label = $config['label'] ?? null; |
| 54 |
$this->tabItemConfig = $config['tabItemConfig'] ?? []; |
| 55 |
$this->addClasses( [ 'oo-ui-tabPanelLayout' ] ); |
| 56 |
$this->setAttributes( [ |
| 57 |
'role' => 'tabpanel', |
| 58 |
] ); |
| 59 |
} |
| 60 |
|
| 61 |
/** @inheritDoc */ |
| 62 |
public function getConfig( &$config ) { |
| 63 |
$config['name'] = $this->name; |
| 64 |
$config['label'] = $this->label; |
| 65 |
if ( $this->tabItemConfig ) { |
| 66 |
$config['tabItemConfig'] = $this->tabItemConfig; |
| 67 |
} |
| 68 |
// scrollable default has changed to true |
| 69 |
if ( !$this->hasClass( 'oo-ui-panelLayout-scrollable' ) ) { |
| 70 |
$config['scrollable'] = false; |
| 71 |
} else { |
| 72 |
unset( $config['scrollable'] ); |
| 73 |
} |
| 74 |
return parent::getConfig( $config ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @return string |
| 79 |
*/ |
| 80 |
public function getName() { |
| 81 |
return $this->name; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function getTabItemConfig() { |
| 88 |
return $this->tabItemConfig; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @return string|HtmlSnippet|null |
| 93 |
*/ |
| 94 |
public function getLabel() { |
| 95 |
return $this->label; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @param bool $active |
| 100 |
*/ |
| 101 |
public function setActive( $active ) { |
| 102 |
$this->active = $active; |
| 103 |
$this->removeClasses( [ 'oo-ui-tabPanelLayout-active' ] ); |
| 104 |
if ( $active ) { |
| 105 |
$this->addClasses( [ 'oo-ui-tabPanelLayout-active' ] ); |
| 106 |
} |
| 107 |
} |
| 108 |
} |