]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - vendor/oojs/oojs-ui/php/Theme.php
MediaWiki 1.30.2-scripts
[autoinstalls/mediawiki.git] / vendor / oojs / oojs-ui / php / Theme.php
1 <?php
2
3 namespace OOUI;
4
5 /**
6  * Theme logic.
7  *
8  * @abstract
9  */
10 abstract class Theme {
11
12         /* Properties */
13
14         private static $singleton;
15
16         /* Static Methods */
17
18         /**
19          * @param Theme|null $theme Theme to use throughout the application
20          */
21         public static function setSingleton( Theme $theme = null ) {
22                 self::$singleton = $theme;
23         }
24
25         /**
26          * @return Theme
27          * @throws Exception
28          */
29         public static function singleton() {
30                 if ( !self::$singleton ) {
31                         throw new Exception( __METHOD__ . ' was called with no singleton theme set.' );
32                 }
33
34                 return self::$singleton;
35         }
36
37         /**
38          * Get a list of classes to be applied to a widget.
39          *
40          * The 'on' and 'off' lists combined MUST contain keys for all classes the theme adds or removes,
41          * otherwise state transitions will not work properly.
42          *
43          * @param Element $element Element for which to get classes
44          * @return array Categorized class names with `on` and `off` lists
45          */
46         public function getElementClasses( Element $element ) {
47                 return [ 'on' => [], 'off' => [] ];
48         }
49
50         /**
51          * Update CSS classes provided by the theme.
52          *
53          * For elements with theme logic hooks, this should be called any time there's a state change.
54          *
55          * @param Element $element Element for which to update classes
56          * @return array Categorized class names with `on` and `off` lists
57          */
58         public function updateElementClasses( Element $element ) {
59                 $classes = $this->getElementClasses( $element );
60                 $traits = class_uses( $element );
61
62                 if ( in_array( IconElement::class, $traits ) ) {
63                         $element->getIconElement()
64                                 ->removeClasses( $classes['off'] )
65                                 ->addClasses( $classes['on'] );
66                 }
67                 if ( in_array( IndicatorElement::class, $traits ) ) {
68                         $element->getIndicatorElement()
69                                 ->removeClasses( $classes['off'] )
70                                 ->addClasses( $classes['on'] );
71                 }
72         }
73 }