File: AccessKeyedElement.php

package info (click to toggle)
mediawiki 1%3A1.39.13-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 320,416 kB
  • sloc: php: 815,516; javascript: 601,264; sql: 11,218; python: 4,863; xml: 3,080; sh: 990; ruby: 82; makefile: 78
file content (100 lines) | stat: -rw-r--r-- 2,263 bytes parent folder | download | duplicates (3)
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
<?php

namespace OOUI;

/**
 * Element with an access key.
 *
 * Access keys allow an user to go to a specific element by using
 * a shortcut combination of a browser specific keys + the key
 * set to the field.
 *
 * @abstract
 */
trait AccessKeyedElement {

	/**
	 * Access key
	 *
	 * @var ?string
	 */
	protected $accessKey = null;

	/**
	 * @var Tag
	 */
	protected $accessKeyed;

	/**
	 * @param array $config Configuration options
	 *      - string $config['accessKey'] Access key. If not provided, no access key will be added
	 */
	public function initializeAccessKeyedElement( array $config = [] ) {
		// Properties
		$this->accessKeyed = $config['accessKeyed'] ?? $this;

		// Initialization
		$this->setAccessKey( $config['accessKey'] ?? null );
		$this->registerConfigCallback( function ( &$config ) {
			if ( $this->accessKey !== null ) {
				$config['accessKey'] = $this->accessKey;
			}
		} );
	}

	/**
	 * Set access key.
	 *
	 * @param string $accessKey Tag's access key, use empty string to remove
	 * @return $this
	 */
	public function setAccessKey( $accessKey ) {
		$accessKey = is_string( $accessKey ) && strlen( $accessKey ) ? $accessKey : null;

		if ( $this->accessKey !== $accessKey ) {
			if ( $accessKey !== null ) {
				$this->accessKeyed->setAttributes( [ 'accesskey' => $accessKey ] );
			} else {
				$this->accessKeyed->removeAttributes( [ 'accesskey' ] );
			}
			$this->accessKey = $accessKey;

			// Only if this is a TitledElement
			if ( method_exists( $this, 'updateTitle' ) ) {
				// @phan-suppress-next-line PhanUndeclaredMethod
				$this->updateTitle();
			}
		}

		return $this;
	}

	/**
	 * Get access key.
	 *
	 * @return string Access key string
	 */
	public function getAccessKey() {
		return $this->accessKey;
	}

	/**
	 * Add information about the access key to the element's tooltip label.
	 * (This is only public for hacky usage in FieldLayout.)
	 *
	 * @param string $title Tooltip label for `title` attribute
	 * @return string
	 */
	public function formatTitleWithAccessKey( $title ) {
		$accessKey = $this->getAccessKey();
		if ( $accessKey ) {
			$title .= " [$accessKey]";
		}
		return $title;
	}

	/**
	 * @param callable $func
	 */
	abstract public function registerConfigCallback( callable $func );
}