]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/oojs/oojs-ui/php/widgets/ButtonWidget.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / oojs / oojs-ui / php / widgets / ButtonWidget.php
1 <?php
2
3 namespace OOUI;
4
5 /**
6  * Generic widget for buttons.
7  */
8 class ButtonWidget extends Widget {
9         use ButtonElement;
10         use IconElement;
11         use IndicatorElement;
12         use LabelElement;
13         use TitledElement;
14         use FlaggedElement;
15         use TabIndexedElement;
16         use AccessKeyedElement;
17
18         /* Static Properties */
19
20         public static $tagName = 'span';
21
22         /* Properties */
23
24         /**
25          * Whether button is active.
26          *
27          * @var boolean
28          */
29         protected $active = false;
30
31         /**
32          * Hyperlink to visit when clicked.
33          *
34          * @var string
35          */
36         protected $href = null;
37
38         /**
39          * Target to open hyperlink in.
40          *
41          * @var string
42          */
43         protected $target = null;
44
45         /**
46          * Search engine traversal hint.
47          *
48          * True if search engines should avoid following this hyperlink.
49          *
50          * @var boolean
51          */
52         protected $noFollow = true;
53
54         /**
55          * @param array $config Configuration options
56          * @param bool $config['active'] Whether button should be shown as active (default: false)
57          * @param string $config['href'] Hyperlink to visit when clicked
58          * @param string $config['target'] Target to open hyperlink in
59          * @param bool $config['noFollow'] Search engine traversal hint (default: true)
60          */
61         public function __construct( array $config = [] ) {
62                 // Parent constructor
63                 parent::__construct( $config );
64
65                 // Traits
66                 $this->initializeButtonElement( $config );
67                 $this->initializeIconElement( $config );
68                 $this->initializeIndicatorElement( $config );
69                 $this->initializeLabelElement( $config );
70                 $this->initializeTitledElement(
71                         array_merge( $config, [ 'titled' => $this->button ] ) );
72                 $this->initializeFlaggedElement( $config );
73                 $this->initializeTabIndexedElement(
74                         array_merge( $config, [ 'tabIndexed' => $this->button ] ) );
75                 $this->initializeAccessKeyedElement(
76                         array_merge( $config, [ 'accessKeyed' => $this->button ] ) );
77
78                 // Initialization
79                 $this->button->appendContent( $this->icon, $this->label, $this->indicator );
80                 $this
81                         ->addClasses( [ 'oo-ui-buttonWidget' ] )
82                         ->appendContent( $this->button );
83
84                 $this->setActive( isset( $config['active'] ) ? $config['active'] : false );
85                 $this->setHref( isset( $config['href'] ) ? $config['href'] : null );
86                 $this->setTarget( isset( $config['target'] ) ? $config['target'] : null );
87                 $this->setNoFollow( isset( $config['noFollow'] ) ? $config['noFollow'] : true );
88         }
89
90         /**
91          * Get hyperlink location.
92          *
93          * @return string Hyperlink location
94          */
95         public function getHref() {
96                 return $this->href;
97         }
98
99         /**
100          * Get hyperlink target.
101          *
102          * @return string Hyperlink target
103          */
104         public function getTarget() {
105                 return $this->target;
106         }
107
108         /**
109          * Get search engine traversal hint.
110          *
111          * @return bool Whether search engines should avoid traversing this hyperlink
112          */
113         public function getNoFollow() {
114                 return $this->noFollow;
115         }
116
117         /**
118          * Set hyperlink location.
119          *
120          * @param string|null $href Hyperlink location, null to remove
121          * @return $this
122          */
123         public function setHref( $href ) {
124                 $this->href = is_string( $href ) ? $href : null;
125
126                 $this->updateHref();
127
128                 return $this;
129         }
130
131         /**
132          * Update the href attribute, in case of changes to href or disabled
133          * state.
134          *
135          * @return $this
136          */
137         public function updateHref() {
138                 if ( $this->href !== null && !$this->isDisabled() ) {
139                         $this->button->setAttributes( [ 'href' => $this->href ] );
140                 } else {
141                         $this->button->removeAttributes( [ 'href' ] );
142                 }
143                 return $this;
144         }
145
146         /**
147          * Set hyperlink target.
148          *
149          * @param string|null $target Hyperlink target, null to remove
150          * @return $this
151          */
152         public function setTarget( $target ) {
153                 $this->target = is_string( $target ) ? $target : null;
154
155                 if ( $this->target !== null ) {
156                         $this->button->setAttributes( [ 'target' => $target ] );
157                 } else {
158                         $this->button->removeAttributes( [ 'target' ] );
159                 }
160
161                 return $this;
162         }
163
164         /**
165          * Set search engine traversal hint.
166          *
167          * @param bool $noFollow True if search engines should avoid traversing this hyperlink
168          * @return $this
169          */
170         public function setNoFollow( $noFollow ) {
171                 $this->noFollow = is_bool( $noFollow ) ? $noFollow : true;
172
173                 if ( $this->noFollow ) {
174                         $this->button->setAttributes( [ 'rel' => 'nofollow' ] );
175                 } else {
176                         $this->button->removeAttributes( [ 'rel' ] );
177                 }
178
179                 return $this;
180         }
181
182         /**
183          * Toggle active state.
184          *
185          * A button should be marked as active when clicking it would only refresh the page.
186          *
187          * @param bool $active Make button active
188          * @return $this
189          */
190         public function setActive( $active = null ) {
191                 $this->active = !!$active;
192                 $this->toggleClasses( [ 'oo-ui-buttonElement-active' ], $this->active );
193                 return $this;
194         }
195
196         /**
197          * Check if button is active.
198          *
199          * @return bool Button is active
200          */
201         public function isActive() {
202                 return $this->active;
203         }
204
205         public function getConfig( &$config ) {
206                 if ( $this->active !== false ) {
207                         $config['active'] = $this->active;
208                 }
209                 if ( $this->href !== null ) {
210                         $config['href'] = $this->href;
211                 }
212                 if ( $this->target !== null ) {
213                         $config['target'] = $this->target;
214                 }
215                 if ( $this->noFollow !== true ) {
216                         $config['noFollow'] = $this->noFollow;
217                 }
218                 return parent::getConfig( $config );
219         }
220 }