Debian Codegraph @

vendor/oojs/oojs-ui/php/widgets/MessageWidget.php

raw
1 <?php
2
3 namespace OOUI;
4
5 /**
6 * Notice widget.
7 */
8 class MessageWidget extends Widget {
9 use IconElement;
10 use LabelElement;
11 use TitledElement;
12 use FlaggedElement;
13
14 /**
15 * Defines whether the widget is inline
16 *
17 * @var bool
18 */
19 protected $inline;
20
21 /**
22 * Defines the displayed message type
23 *
24 * @var string
25 */
26 protected $type;
27
28 /**
29 * @var ButtonWidget|null
30 */
31 protected $closeButton = null;
32
33 /**
34 * Map legal types to their OOUI icon
35 *
36 * @var array
37 */
38 protected $iconMap = [
39 'notice' => 'infoFilled',
40 'error' => 'error',
41 'warning' => 'alert',
42 'success' => 'success',
43 ];
44
45 /**
46 * Default type for the widget
47 *
48 * @var string
49 */
50 protected $defaultType = 'notice';
51
52 /**
53 * @param array $config Configuration options
54 */
55 public function __construct( array $config = [] ) {
56 // Parent constructor
57 parent::__construct( $config );
58
59 // Traits
60 $this->initializeLabelElement( $config );
61 $this->initializeIconElement( $config );
62 $this->initializeTitledElement( $config );
63 $this->initializeFlaggedElement( $config );
64
65 $this->setType( $config['type'] ?? $this->defaultType );
66 $this->setInline( $config['inline'] ?? false );
67
68 // If an icon is passed in, set it again as setType will
69 // have overridden the setIcon call in the IconElement constructor
70 if ( isset( $config['icon'] ) ) {
71 $this->setIcon( $config['icon'] );
72 }
73
74 if ( !$this->inline && !empty( $config['showClose'] ) ) {
75 $this->closeButton = new ButtonWidget( [
76 'classes' => [ 'oo-ui-messageWidget-close' ],
77 'framed' => false,
78 'icon' => 'close',
79 // TODO We have no way to use localisation messages in PHP
80 // (and to use different languages when used from MediaWiki)
81 // 'label' => msg( 'ooui-popup-widget-close-button-aria-label' ),
82 // 'invisibleLabel' => true
83 ] );
84 $this->addClasses( [ 'oo-ui-messageWidget-showClose' ] );
85 }
86
87 $this->addClasses( [ 'oo-ui-messageWidget' ] );
88 $this->appendContent( [ $this->icon, $this->label, $this->closeButton ] );
89 }
90
91 /** @inheritDoc */
92 public function getConfig( &$config ) {
93 $config['type'] = $this->type;
94 $config['inline'] = $this->inline;
95 $config['showClose'] = $this->closeButton !== null;
96
97 return parent::getConfig( $config );
98 }
99
100 /**
101 * Set the inline state of the widget
102 *
103 * @param bool $inline Widget is inline
104 */
105 public function setInline( $inline ) {
106 $this->inline = (bool)$inline;
107 $this->toggleClasses( [ 'oo-ui-messageWidget-block' ], !$this->inline );
108 }
109
110 /**
111 * Set the widget type. The given type must belong to the list of
112 * legal types set by $this->iconMap
113 *
114 * @param string $type Given type
115 */
116 public function setType( $type ) {
117 if ( !array_key_exists( $type, $this->iconMap ) ) {
118 $type = $this->defaultType;
119 }
120
121 // Set flag
122 $this->clearFlags();
123 $this->setFlags( [ $type ] );
124
125 // Set icon
126 $this->setIcon( $this->iconMap[ $type ] );
127 $this->icon->removeClasses( [ 'oo-ui-image-' . $this->type ] );
128 $this->icon->addClasses( [ 'oo-ui-image-' . $type ] );
129
130 // Initialization
131 if ( $type === 'error' ) {
132 $this->setAttributes( [ 'role' => 'alert' ] );
133 $this->removeAttributes( [ 'aria-live' ] );
134 } else {
135 $this->setAttributes( [ 'aria-live' => 'polite' ] );
136 $this->removeAttributes( [ 'role' ] );
137 }
138
139 $this->type = $type;
140 }
141 }