Debian Codegraph @

vendor/oojs/oojs-ui/php/mixins/AccessKeyedElement.php

raw
1 <?php
2
3 namespace OOUI;
4
5 /**
6 * Element with an access key.
7 *
8 * Access keys allow an user to go to a specific element by using
9 * a shortcut combination of a browser specific keys + the key
10 * set to the field.
11 *
12 * @abstract
13 */
14 trait AccessKeyedElement {
15
16 /**
17 * Access key
18 *
19 * @var ?string
20 */
21 protected $accessKey = null;
22
23 /**
24 * @var Tag
25 */
26 protected $accessKeyed;
27
28 /**
29 * @param array $config Configuration options
30 * - string $config['accessKey'] Access key. If not provided, no access key will be added
31 */
32 public function initializeAccessKeyedElement( array $config = [] ) {
33 // Properties
34 $this->accessKeyed = $config['accessKeyed'] ?? $this;
35
36 // Initialization
37 $this->setAccessKey( $config['accessKey'] ?? null );
38 $this->registerConfigCallback( function ( &$config ) {
39 if ( $this->accessKey !== null ) {
40 $config['accessKey'] = $this->accessKey;
41 }
42 } );
43 }
44
45 /**
46 * Set access key.
47 *
48 * @param string $accessKey Tag's access key, use empty string to remove
49 * @return $this
50 */
51 public function setAccessKey( $accessKey ) {
52 $accessKey = is_string( $accessKey ) && strlen( $accessKey ) ? $accessKey : null;
53
54 if ( $this->accessKey !== $accessKey ) {
55 if ( $accessKey !== null ) {
56 $this->accessKeyed->setAttributes( [ 'accesskey' => $accessKey ] );
57 } else {
58 $this->accessKeyed->removeAttributes( [ 'accesskey' ] );
59 }
60 $this->accessKey = $accessKey;
61
62 // Only if this is a TitledElement
63 if ( method_exists( $this, 'updateTitle' ) ) {
64 // @phan-suppress-next-line PhanUndeclaredMethod
65 $this->updateTitle();
66 }
67 }
68
69 return $this;
70 }
71
72 /**
73 * Get access key.
74 *
75 * @return string Access key string
76 */
77 public function getAccessKey() {
78 return $this->accessKey;
79 }
80
81 /**
82 * Add information about the access key to the element's tooltip label.
83 * (This is only public for hacky usage in FieldLayout.)
84 *
85 * @param string $title Tooltip label for `title` attribute
86 * @return string
87 */
88 public function formatTitleWithAccessKey( $title ) {
89 $accessKey = $this->getAccessKey();
90 if ( $accessKey ) {
91 $title .= " [$accessKey]";
92 }
93 return $title;
94 }
95
96 /**
97 * @param callable $func
98 */
99 abstract public function registerConfigCallback( callable $func );
100 }