| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* Theme logic. |
| 7 |
* |
| 8 |
* @abstract |
| 9 |
*/ |
| 10 |
abstract class Theme { |
| 11 |
|
| 12 |
/* Properties */ |
| 13 |
|
| 14 |
/** @var self|null */ |
| 15 |
private static $singleton; |
| 16 |
|
| 17 |
/* Static Methods */ |
| 18 |
|
| 19 |
/** |
| 20 |
* @param Theme|null $theme Theme to use throughout the application |
| 21 |
*/ |
| 22 |
public static function setSingleton( ?Theme $theme = null ) { |
| 23 |
self::$singleton = $theme; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @return Theme |
| 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 string[][] 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 |
*/ |
| 57 |
public function updateElementClasses( Element $element ) { |
| 58 |
$classes = $this->getElementClasses( $element ); |
| 59 |
|
| 60 |
if ( method_exists( $element, 'getIconElement' ) ) { |
| 61 |
// @phan-suppress-next-line PhanUndeclaredMethod |
| 62 |
$element->getIconElement() |
| 63 |
->removeClasses( $classes['off'] ) |
| 64 |
->addClasses( $classes['on'] ); |
| 65 |
} |
| 66 |
if ( method_exists( $element, 'getIndicatorElement' ) ) { |
| 67 |
// @phan-suppress-next-line PhanUndeclaredMethod |
| 68 |
$element->getIndicatorElement() |
| 69 |
->removeClasses( $classes['off'] ) |
| 70 |
->addClasses( $classes['on'] ); |
| 71 |
} |
| 72 |
} |
| 73 |
} |