Debian Codegraph @

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

raw
1 <?php
2
3 namespace OOUI;
4
5 /**
6 * Element containing an indicator.
7 *
8 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
9 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
10 * instead of performing an action directly, or an item in a list which has errors that need to be
11 * resolved.
12 *
13 * @abstract
14 */
15 trait IndicatorElement {
16 /**
17 * Symbolic indicator name
18 *
19 * @var string|null
20 */
21 protected $indicatorName = null;
22
23 /**
24 * @var Tag
25 */
26 protected $indicator;
27
28 /**
29 * @param array $config Configuration options
30 * - string $config['indicator'] Symbolic indicator name
31 */
32 public function initializeIndicatorElement( array $config = [] ) {
33 // Properties
34 // FIXME 'indicatorElement' is a very stupid way to call '$indicator'
35 $this->indicator = $config['indicatorElement'] ?? new Tag( 'span' );
36
37 // Initialization
38 $this->indicator->addClasses( [ 'oo-ui-indicatorElement-indicator' ] );
39 $this->setIndicator( $config['indicator'] ?? null );
40
41 $this->registerConfigCallback( function ( &$config ) {
42 if ( $this->indicatorName !== null ) {
43 $config['indicator'] = $this->indicatorName;
44 }
45 } );
46 }
47
48 /**
49 * Set indicator name.
50 *
51 * @param string|null $indicator Symbolic name of indicator to use or null for no indicator
52 * @return $this
53 */
54 public function setIndicator( $indicator = null ) {
55 if ( $this->indicatorName !== null ) {
56 $this->indicator->removeClasses( [ 'oo-ui-indicator-' . $this->indicatorName ] );
57 }
58 if ( $indicator !== null ) {
59 $this->indicator->addClasses( [ 'oo-ui-indicator-' . $indicator ] );
60 }
61
62 $this->indicatorName = $indicator;
63 $this->toggleClasses( [ 'oo-ui-indicatorElement' ], (bool)$this->indicatorName );
64 $this->indicator->toggleClasses( [ 'oo-ui-indicatorElement-noIndicator' ],
65 !$this->indicatorName );
66
67 return $this;
68 }
69
70 /**
71 * Get indicator name.
72 *
73 * @return string|null Symbolic name of indicator, null if not set
74 */
75 public function getIndicator() {
76 return $this->indicatorName;
77 }
78
79 /**
80 * Do not use outside of Theme::updateElementClasses
81 *
82 * @protected
83 * @return Tag
84 */
85 public function getIndicatorElement() {
86 return $this->indicator;
87 }
88
89 /**
90 * Toggle CSS classes.
91 *
92 * @param array $classes List of classes to add
93 * @param bool|null $toggle Add classes
94 * @return $this
95 */
96 abstract public function toggleClasses( array $classes, $toggle = null );
97
98 /**
99 * @param callable $func
100 */
101 abstract public function registerConfigCallback( callable $func );
102 }