| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* Element supporting "sequential focus navigation" using the 'tabindex' attribute. |
| 7 |
* |
| 8 |
* @abstract |
| 9 |
*/ |
| 10 |
trait TabIndexedElement { |
| 11 |
/** |
| 12 |
* Tab index value. |
| 13 |
* |
| 14 |
* @var int|null |
| 15 |
*/ |
| 16 |
protected $tabIndex = null; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var Element |
| 20 |
*/ |
| 21 |
protected $tabIndexed; |
| 22 |
|
| 23 |
/** |
| 24 |
* @param array $config Configuration options |
| 25 |
* - string|int|null $config['tabIndex'] Tab index value. Use 0 to use default ordering, |
| 26 |
* use -1 to prevent tab focusing, use null to suppress the `tabindex` attribute. |
| 27 |
* (default: 0) |
| 28 |
*/ |
| 29 |
public function initializeTabIndexedElement( array $config = [] ) { |
| 30 |
// Properties |
| 31 |
$this->tabIndexed = $config['tabIndexed'] ?? $this; |
| 32 |
|
| 33 |
// Initialization |
| 34 |
$this->setTabIndex( $config['tabIndex'] ?? 0 ); |
| 35 |
|
| 36 |
$this->registerConfigCallback( function ( &$config ) { |
| 37 |
if ( $this->tabIndex !== 0 ) { |
| 38 |
$config['tabIndex'] = $this->tabIndex; |
| 39 |
} |
| 40 |
} ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Set tab index value. |
| 45 |
* |
| 46 |
* @param string|int|null $tabIndex Tab index value or null for no tab index |
| 47 |
* @return $this |
| 48 |
*/ |
| 49 |
public function setTabIndex( $tabIndex ) { |
| 50 |
$tabIndex = preg_match( '/^-?\d+$/', $tabIndex ) ? (int)$tabIndex : null; |
| 51 |
|
| 52 |
if ( $this->tabIndex !== $tabIndex ) { |
| 53 |
$this->tabIndex = $tabIndex; |
| 54 |
$this->updateTabIndex(); |
| 55 |
} |
| 56 |
|
| 57 |
return $this; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Update the tabIndex attribute, in case of changes to tabIndex or disabled |
| 62 |
* state. |
| 63 |
* |
| 64 |
* @return $this |
| 65 |
*/ |
| 66 |
public function updateTabIndex() { |
| 67 |
$disabled = $this->isDisabled(); |
| 68 |
if ( $this->tabIndex !== null ) { |
| 69 |
// Do not index over disabled elements |
| 70 |
$this->tabIndexed->setAttributes( [ 'tabindex' => $disabled ? -1 : $this->tabIndex ] ); |
| 71 |
if ( $disabled ) { |
| 72 |
// ChromeVox and NVDA do not seem to inherit this from parent elements |
| 73 |
$this->tabIndexed->setAttributes( [ 'aria-disabled' => 'true' ] ); |
| 74 |
} else { |
| 75 |
$this->tabIndexed->removeAttributes( [ 'aria-disabled' ] ); |
| 76 |
} |
| 77 |
} else { |
| 78 |
$this->tabIndexed->removeAttributes( [ 'tabindex', 'aria-disabled' ] ); |
| 79 |
} |
| 80 |
return $this; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get tab index value. |
| 85 |
* |
| 86 |
* @return int|null Tab index value |
| 87 |
*/ |
| 88 |
public function getTabIndex() { |
| 89 |
return $this->tabIndex; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Get an ID of a focusable element of this widget, if any, to be used for `<label for>` value. |
| 94 |
* |
| 95 |
* If the element already has an ID then that is returned, otherwise unique ID is |
| 96 |
* generated, set on the element, and returned. |
| 97 |
* |
| 98 |
* @return string|null The ID of the focusable element |
| 99 |
*/ |
| 100 |
public function getInputId() { |
| 101 |
$id = $this->tabIndexed->getAttribute( 'id' ); |
| 102 |
|
| 103 |
if ( !$this->isLabelableNode( $this->tabIndexed ) ) { |
| 104 |
return null; |
| 105 |
} |
| 106 |
|
| 107 |
if ( $id === null ) { |
| 108 |
$id = Tag::generateElementId(); |
| 109 |
$this->tabIndexed->setAttributes( [ 'id' => $id ] ); |
| 110 |
} |
| 111 |
|
| 112 |
return $id; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Whether the node is 'labelable' according to the HTML spec |
| 117 |
* (i.e., whether it can be interacted with through a `<label for="…">`). |
| 118 |
* See: <https://html.spec.whatwg.org/multipage/forms.html#category-label>. |
| 119 |
* |
| 120 |
* @param Tag $tag |
| 121 |
* @return bool |
| 122 |
*/ |
| 123 |
private function isLabelableNode( Tag $tag ) { |
| 124 |
$labelableTags = [ 'button', 'meter', 'output', 'progress', 'select', 'textarea' ]; |
| 125 |
$tagName = strtolower( $tag->getTag() ); |
| 126 |
|
| 127 |
if ( $tagName === 'input' && $tag->getAttribute( 'type' ) !== 'hidden' ) { |
| 128 |
return true; |
| 129 |
} |
| 130 |
if ( in_array( $tagName, $labelableTags, true ) ) { |
| 131 |
return true; |
| 132 |
} |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
abstract public function isDisabled(); |
| 140 |
|
| 141 |
/** |
| 142 |
* @param callable $func |
| 143 |
*/ |
| 144 |
abstract public function registerConfigCallback( callable $func ); |
| 145 |
} |