]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/oojs/oojs-ui/php/mixins/TitledElement.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / oojs / oojs-ui / php / mixins / TitledElement.php
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          * @param string $config['title'] Title. If not provided, the static property 'title' is used.
29          */
30         public function initializeTitledElement( array $config = [] ) {
31                 // Properties
32                 $this->titled = isset( $config['titled'] ) ? $config['titled'] : $this;
33
34                 // Initialization
35                 $this->setTitle(
36                         isset( $config['title'] ) ? $config['title'] : null
37                 );
38
39                 $this->registerConfigCallback( function ( &$config ) {
40                         if ( $this->title !== null ) {
41                                 $config['title'] = $this->title;
42                         }
43                 } );
44         }
45
46         /**
47          * Set title.
48          *
49          * @param string|null $title Title text or null for no title
50          * @return $this
51          */
52         public function setTitle( $title ) {
53                 $title = $title !== '' ? $title : null;
54
55                 if ( $this->title !== $title ) {
56                         $this->title = $title;
57                         $this->updateTitle();
58                 }
59
60                 return $this;
61         }
62
63         /**
64          * Update the title attribute, in case of changes to title or accessKey.
65          *
66          * @return $this
67          */
68         protected function updateTitle() {
69                 $title = $this->getTitle();
70                 if ( $title !== null ) {
71                         // Only if this is an AccessKeyedElement
72                         if ( method_exists( $this, 'formatTitleWithAccessKey' ) ) {
73                                 $title = $this->formatTitleWithAccessKey( $title );
74                         }
75                         $this->titled->setAttributes( [ 'title' => $title ] );
76                 } else {
77                         $this->titled->removeAttributes( [ 'title' ] );
78                 }
79                 return $this;
80         }
81
82         /**
83          * Get title.
84          *
85          * @return string Title string
86          */
87         public function getTitle() {
88                 return $this->title;
89         }
90 }