1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
<?php
namespace OOUI;
/**
* Element containing a label.
*
* @abstract
*/
trait LabelElement {
/**
* Label value.
*
* @var string|HtmlSnippet|null
*/
protected $labelValue = null;
/**
* Label value.
*
* @var bool
*/
protected $invisibleLabel = false;
/**
* @var Tag
*/
protected $label;
/**
* @param array $config Configuration options
* - string|HtmlSnippet $config['label'] Label text
* - bool $config['invisibleLabel'] Whether the label should be visually hidden (but still
* accessible to screen-readers). (default: false)
*/
public function initializeLabelElement( array $config = [] ) {
// Properties
// FIXME 'labelElement' is a very stupid way to call '$label'
$this->label = $config['labelElement'] ?? new Tag( 'span' );
// Initialization
$this->label->addClasses( [ 'oo-ui-labelElement-label' ] );
$this->setLabel( $config['label'] ?? null );
$this->setInvisibleLabel( $config['invisibleLabel'] ?? false );
$this->registerConfigCallback( function ( &$config ) {
if ( $this->labelValue !== null ) {
$config['label'] = $this->labelValue;
}
if ( $this->invisibleLabel !== false ) {
$config['invisibleLabel'] = $this->invisibleLabel;
}
} );
}
/**
* Set the label.
*
* An empty string will result in the label being hidden. A string containing only whitespace will
* be converted to a single ` `.
*
* @param string|HtmlSnippet|null $label Label text
* @return $this
*/
public function setLabel( $label ) {
$this->labelValue = (string)$label !== '' ? $label : null;
$this->label->clearContent();
if ( $this->labelValue !== null ) {
if ( is_string( $this->labelValue ) && trim( $this->labelValue ) === '' ) {
$this->label->appendContent( new HtmlSnippet( ' ' ) );
} else {
$this->label->appendContent( $label );
}
}
$visibleLabel = $this->labelValue !== null && !$this->invisibleLabel;
$this->toggleClasses( [ 'oo-ui-labelElement' ], $visibleLabel );
return $this;
}
/**
* Set whether the label should be visually hidden (but still accessible to screen-readers).
*
* An empty string will result in the label being hidden. A string containing only whitespace will
* be converted to a single ` `.
*
* @param bool $invisibleLabel
* @return $this
*/
public function setInvisibleLabel( $invisibleLabel ) {
$this->invisibleLabel = (bool)$invisibleLabel;
$this->label->toggleClasses( [ 'oo-ui-labelElement-invisible' ], $this->invisibleLabel );
// Pretend that there is no label, a lot of CSS has been written with this assumption
$visibleLabel = $this->labelValue !== null && !$this->invisibleLabel;
$this->toggleClasses( [ 'oo-ui-labelElement' ], $visibleLabel );
return $this;
}
/**
* Get the label.
*
* @return string|HtmlSnippet|null Label text
*/
public function getLabel() {
return $this->labelValue;
}
/**
* Toggle CSS classes.
*
* @param array $classes List of classes to add
* @param bool|null $toggle Add classes
* @return $this
*/
abstract public function toggleClasses( array $classes, $toggle = null );
/**
* @param callable $func
*/
abstract public function registerConfigCallback( callable $func );
}
|