Debian Codegraph @

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

raw
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 * - 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 = $config['iconElement'] ?? new Tag( 'span' );
36
37 // Initialization
38 $this->icon->addClasses( [ 'oo-ui-iconElement-icon' ] );
39 $this->setIcon( $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 $this->icon->toggleClasses( [ 'oo-ui-iconElement-noIcon' ], !$this->iconName );
65
66 return $this;
67 }
68
69 /**
70 * Get icon name.
71 *
72 * @return string Icon name
73 */
74 public function getIcon() {
75 return $this->iconName;
76 }
77
78 /**
79 * Do not use outside of Theme::updateElementClasses
80 *
81 * @protected
82 * @return Tag
83 */
84 public function getIconElement() {
85 return $this->icon;
86 }
87
88 /**
89 * Toggle CSS classes.
90 *
91 * @param array $classes List of classes to add
92 * @param bool|null $toggle Add classes
93 * @return $this
94 */
95 abstract public function toggleClasses( array $classes, $toggle = null );
96
97 /**
98 * @param callable $func
99 */
100 abstract public function registerConfigCallback( callable $func );
101 }