]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/oojs/oojs-ui/php/mixins/FlaggedElement.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / oojs / oojs-ui / php / mixins / FlaggedElement.php
1 <?php
2
3 namespace OOUI;
4
5 /**
6  * Element with named flags that can be added, removed, listed and checked.
7  *
8  * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
9  * the flag name. Flags are primarily useful for styling.
10  *
11  * @abstract
12  */
13 trait FlaggedElement {
14         /**
15          * Flags.
16          *
17          * @var string[]
18          */
19         protected $flags = [];
20
21         /**
22          * @var Element
23          */
24         protected $flagged;
25
26         /**
27          * @param array $config Configuration options
28          * @param string|string[] $config['flags'] Flags describing importance and functionality, e.g.
29          *   'primary', 'safe', 'progressive', 'destructive' or 'constructive'
30          */
31         public function initializeFlaggedElement( array $config = [] ) {
32                 // Properties
33                 $this->flagged = isset( $config['flagged'] ) ? $config['flagged'] : $this;
34
35                 // Initialization
36                 $this->setFlags( isset( $config['flags'] ) ? $config['flags'] : null );
37
38                 $this->registerConfigCallback( function ( &$config ) {
39                         if ( !empty( $this->flags ) ) {
40                                 $config['flags'] = $this->getFlags();
41                         }
42                 } );
43         }
44
45         /**
46          * Check if a flag is set.
47          *
48          * @param string $flag Name of flag
49          * @return bool Has flag
50          */
51         public function hasFlag( $flag ) {
52                 return isset( $this->flags[$flag] );
53         }
54
55         /**
56          * Get the names of all flags set.
57          *
58          * @return string[] Flag names
59          */
60         public function getFlags() {
61                 return array_keys( $this->flags );
62         }
63
64         /**
65          * Clear all flags.
66          *
67          * @return $this
68          */
69         public function clearFlags() {
70                 $remove = [];
71                 $classPrefix = 'oo-ui-flaggedElement-';
72
73                 foreach ( $this->flags as $flag ) {
74                         $remove[] = $classPrefix . $flag;
75                 }
76
77                 $this->flagged->removeClasses( $remove );
78                 $this->flags = [];
79
80                 return $this;
81         }
82
83         /**
84          * Add one or more flags.
85          *
86          * @param string|array $flags One or more flags to add, or an array keyed by flag name
87          *   containing boolean set/remove instructions.
88          * @return $this
89          */
90         public function setFlags( $flags ) {
91                 $add = [];
92                 $remove = [];
93                 $classPrefix = 'oo-ui-flaggedElement-';
94
95                 if ( is_string( $flags ) ) {
96                         // Set
97                         if ( !isset( $this->flags[$flags] ) ) {
98                                 $this->flags[$flags] = true;
99                                 $add[] = $classPrefix . $flags;
100                         }
101                 } elseif ( is_array( $flags ) ) {
102                         foreach ( $flags as $key => $value ) {
103                                 if ( is_numeric( $key ) ) {
104                                         // Set
105                                         if ( !isset( $this->flags[$value] ) ) {
106                                                 $this->flags[$value] = true;
107                                                 $add[] = $classPrefix . $value;
108                                         }
109                                 } else {
110                                         if ( $value ) {
111                                                 // Set
112                                                 if ( !isset( $this->flags[$key] ) ) {
113                                                         $this->flags[$key] = true;
114                                                         $add[] = $classPrefix . $key;
115                                                 }
116                                         } else {
117                                                 // Remove
118                                                 if ( isset( $this->flags[$key] ) ) {
119                                                         unset( $this->flags[$key] );
120                                                         $remove[] = $classPrefix . $key;
121                                                 }
122                                         }
123                                 }
124                         }
125                 }
126
127                 $this->flagged
128                         ->addClasses( $add )
129                         ->removeClasses( $remove );
130
131                 return $this;
132         }
133 }