]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - vendor/oojs/oojs-ui/php/mixins/ButtonElement.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / vendor / oojs / oojs-ui / php / mixins / ButtonElement.php
1 <?php
2
3 namespace OOUI;
4
5 /**
6  * Element with a button.
7  *
8  * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
9  * and access keys for accessibility purposes.
10  *
11  * @abstract
12  */
13 trait ButtonElement {
14
15         /**
16          * Button is framed.
17          *
18          * @var boolean
19          */
20         protected $framed = false;
21
22         /**
23          * @var Tag
24          */
25         protected $button;
26
27         /**
28          * @param array $config Configuration options
29          * @param bool $config['framed'] Render button with a frame (default: true)
30          */
31         public function initializeButtonElement( array $config = [] ) {
32                 // Properties
33                 if ( ! $this instanceof Element ) {
34                         throw new Exception( "ButtonElement trait can only be used on Element instances" );
35                 }
36                 $target = isset( $config['button'] ) ? $config['button'] : new Tag( 'a' );
37                 $this->button = $target;
38
39                 // Initialization
40                 $this->addClasses( [ 'oo-ui-buttonElement' ] );
41                 $this->button->addClasses( [ 'oo-ui-buttonElement-button' ] );
42                 $this->toggleFramed( isset( $config['framed'] ) ? $config['framed'] : true );
43
44                 // Add `role="button"` on `<a>` elements, where it's needed
45                 if ( strtolower( $this->button->getTag() ) === 'a' ) {
46                         $this->button->setAttributes( [
47                                 'role' => 'button',
48                         ] );
49                 }
50
51                 $this->registerConfigCallback( function ( &$config ) {
52                         if ( $this->framed !== true ) {
53                                 $config['framed'] = $this->framed;
54                         }
55                 } );
56         }
57
58         /**
59          * Toggle frame.
60          *
61          * @param bool $framed Make button framed, omit to toggle
62          * @return $this
63          */
64         public function toggleFramed( $framed = null ) {
65                 $this->framed = $framed !== null ? !!$framed : !$this->framed;
66                 $this->toggleClasses( [ 'oo-ui-buttonElement-framed' ], $this->framed );
67                 $this->toggleClasses( [ 'oo-ui-buttonElement-frameless' ], !$this->framed );
68                 return $this;
69         }
70
71         /**
72          * Check if button has a frame.
73          *
74          * @return bool Button is framed
75          */
76         public function isFramed() {
77                 return $this->framed;
78         }
79 }