File: IconElement.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 (101 lines) | stat: -rw-r--r-- 2,299 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
101
<?php

namespace OOUI;

/**
 * Element containing an icon.
 *
 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
 * a control or convey information in a more space efficient way. Icons should rarely be used
 * without labels; such as in a toolbar where space is at a premium or within a context where the
 * meaning is very clear to the user.
 *
 * @abstract
 */
trait IconElement {
	/**
	 * Symbolic icon name.
	 *
	 * @var ?string
	 */
	protected $iconName = null;

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

	/**
	 * @param array $config Configuration options
	 *      - string $config['icon'] Symbolic icon name
	 */
	public function initializeIconElement( array $config = [] ) {
		// Properties
		// FIXME 'iconElement' is a very stupid way to call '$icon'
		$this->icon = $config['iconElement'] ?? new Tag( 'span' );

		// Initialization
		$this->icon->addClasses( [ 'oo-ui-iconElement-icon' ] );
		$this->setIcon( $config['icon'] ?? null );

		$this->registerConfigCallback( function ( &$config ) {
			if ( $this->iconName !== null ) {
				$config['icon'] = $this->iconName;
			}
		} );
	}

	/**
	 * Set icon name.
	 *
	 * @param string|null $icon Symbolic icon name
	 * @return $this
	 */
	public function setIcon( $icon = null ) {
		if ( $this->iconName !== null ) {
			$this->icon->removeClasses( [ 'oo-ui-icon-' . $this->iconName ] );
		}
		if ( $icon !== null ) {
			$this->icon->addClasses( [ 'oo-ui-icon-' . $icon ] );
		}

		$this->iconName = $icon;
		$this->toggleClasses( [ 'oo-ui-iconElement' ], (bool)$this->iconName );
		$this->icon->toggleClasses( [ 'oo-ui-iconElement-noIcon' ], !$this->iconName );

		return $this;
	}

	/**
	 * Get icon name.
	 *
	 * @return string Icon name
	 */
	public function getIcon() {
		return $this->iconName;
	}

	/**
	 * Do not use outside of Theme::updateElementClasses
	 *
	 * @protected
	 * @return Tag
	 */
	public function getIconElement() {
		return $this->icon;
	}

	/**
	 * Toggle CSS classes.
	 *
	 * @param array $classes List of classes to add
	 * @param bool|null $toggle Add classes
	 * @return $this
	 */
	abstract public function toggleClasses( array $classes, $toggle = null );

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