]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/oojs/oojs-ui/php/mixins/IconElement.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / oojs / oojs-ui / php / mixins / IconElement.php
1 <?php
2
3 namespace OOUI;
4
5 /**
6  * Element containing an icon.
7  *
8  * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
9  * a control or convey information in a more space efficient way. Icons should rarely be used
10  * without labels; such as in a toolbar where space is at a premium or within a context where the
11  * meaning is very clear to the user.
12  *
13  * @abstract
14  */
15 trait IconElement {
16         /**
17          * Symbolic icon name.
18          *
19          * @var string
20          */
21         protected $iconName = null;
22
23         /**
24          * @var Tag
25          */
26         protected $icon;
27
28         /**
29          * @param array $config Configuration options
30          * @param string $config['icon'] Symbolic icon name
31          */
32         public function initializeIconElement( array $config = [] ) {
33                 // Properties
34                 // FIXME 'iconElement' is a very stupid way to call '$icon'
35                 $this->icon = isset( $config['iconElement'] ) ? $config['iconElement'] : new Tag( 'span' );
36
37                 // Initialization
38                 $this->icon->addClasses( [ 'oo-ui-iconElement-icon' ] );
39                 $this->setIcon( isset( $config['icon'] ) ? $config['icon'] : null );
40
41                 $this->registerConfigCallback( function ( &$config ) {
42                         if ( $this->iconName !== null ) {
43                                 $config['icon'] = $this->iconName;
44                         }
45                 } );
46         }
47
48         /**
49          * Set icon name.
50          *
51          * @param string|null $icon Symbolic icon name
52          * @return $this
53          */
54         public function setIcon( $icon = null ) {
55                 if ( $this->iconName !== null ) {
56                         $this->icon->removeClasses( [ 'oo-ui-icon-' . $this->iconName ] );
57                 }
58                 if ( $icon !== null ) {
59                         $this->icon->addClasses( [ 'oo-ui-icon-' . $icon ] );
60                 }
61
62                 $this->iconName = $icon;
63                 $this->toggleClasses( [ 'oo-ui-iconElement' ], (bool)$this->iconName );
64
65                 return $this;
66         }
67
68         /**
69          * Get icon name.
70          *
71          * @return string Icon name
72          */
73         public function getIcon() {
74                 return $this->iconName;
75         }
76
77         /**
78          * Do not use outside of Theme::updateElementClasses
79          *
80          * @protected
81          * @return Tag
82          */
83         public function getIconElement() {
84                 return $this->icon;
85         }
86 }