Debian Codegraph @

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

raw
1 <?php
2
3 namespace OOUI;
4
5 /**
6 * Element with a title.
7 *
8 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
9 * not visible on touch devices.
10 *
11 * @abstract
12 */
13 trait TitledElement {
14 /**
15 * Title text.
16 *
17 * @var string
18 */
19 protected $title = null;
20
21 /**
22 * @var Element
23 */
24 protected $titled;
25
26 /**
27 * @param array $config Configuration options
28 * - string $config['title'] Title. If not provided, the static property 'title' is used.
29 * If config for an invisible label (LabelElement) is present, and a title is
30 * omitted, the label will be used as a fallback for the title.
31 */
32 public function initializeTitledElement( array $config = [] ) {
33 // Properties
34 $this->titled = $config['titled'] ?? $this;
35
36 // Initialization
37 $title = $config['title'] ?? null;
38 if (
39 $title === null &&
40 isset( $config['invisibleLabel'] ) && $config['invisibleLabel'] &&
41 isset( $config['label'] ) && is_string( $config['label'] )
42 ) {
43 // If config for an invisible label is present, use this as a fallback title
44 $title = $config['label'];
45 }
46 $this->setTitle( $title );
47
48 $this->registerConfigCallback( function ( &$config ) {
49 if ( $this->title !== null ) {
50 $config['title'] = $this->title;
51 }
52 } );
53 }
54
55 /**
56 * Set title.
57 *
58 * @param string|null $title Title text or null for browser default title, which is no title for
59 * most elements.
60 * @return $this
61 */
62 public function setTitle( $title ) {
63 if ( $this->title !== $title ) {
64 $this->title = $title;
65 $this->updateTitle();
66 }
67
68 return $this;
69 }
70
71 /**
72 * Update the title attribute, in case of changes to title or accessKey.
73 *
74 * @return $this
75 */
76 protected function updateTitle() {
77 $title = $this->getTitle();
78 if ( $title !== null ) {
79 // Only if this is an AccessKeyedElement
80 if ( method_exists( $this, 'formatTitleWithAccessKey' ) ) {
81 // @phan-suppress-next-line PhanUndeclaredMethod
82 $title = $this->formatTitleWithAccessKey( $title );
83 }
84 $this->titled->setAttributes( [ 'title' => $title ] );
85 } else {
86 $this->titled->removeAttributes( [ 'title' ] );
87 }
88 return $this;
89 }
90
91 /**
92 * Get title.
93 *
94 * @return string Title string
95 */
96 public function getTitle() {
97 return $this->title;
98 }
99
100 /**
101 * @param callable $func
102 */
103 abstract public function registerConfigCallback( callable $func );
104 }