File: FlaggedElement.php

package info (click to toggle)
mediawiki 1%3A1.39.13-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 320,416 kB
  • sloc: php: 815,516; javascript: 601,264; sql: 11,218; python: 4,863; xml: 3,080; sh: 990; ruby: 82; makefile: 78
file content (139 lines) | stat: -rw-r--r-- 2,929 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php

namespace OOUI;

/**
 * Element with named flags that can be added, removed, listed and checked.
 *
 * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
 * the flag name. Flags are primarily useful for styling.
 *
 * @abstract
 */
trait FlaggedElement {
	/**
	 * Flags.
	 *
	 * @var bool[]
	 * @phan-var array<string,bool>
	 */
	protected $flags = [];

	/**
	 * @var Element
	 */
	protected $flagged;

	/**
	 * @param array $config Configuration options
	 *      - string|string[] $config['flags'] Flags describing importance and functionality, e.g.
	 *          'primary', 'safe', 'progressive', or 'destructive'.
	 */
	public function initializeFlaggedElement( array $config = [] ) {
		// Properties
		$this->flagged = $config['flagged'] ?? $this;

		// Initialization
		$this->setFlags( $config['flags'] ?? null );

		$this->registerConfigCallback( function ( &$config ) {
			if ( $this->flags ) {
				$config['flags'] = $this->getFlags();
			}
		} );
	}

	/**
	 * Check if a flag is set.
	 *
	 * @param string $flag Name of flag
	 * @return bool Has flag
	 */
	public function hasFlag( $flag ) {
		return isset( $this->flags[$flag] );
	}

	/**
	 * Get the names of all flags set.
	 *
	 * @return string[] Flag names
	 */
	public function getFlags() {
		return array_keys( $this->flags );
	}

	/**
	 * Clear all flags.
	 *
	 * @return $this
	 */
	public function clearFlags() {
		$remove = [];
		$classPrefix = 'oo-ui-flaggedElement-';

		foreach ( $this->flags as $flag => $value ) {
			$remove[] = $classPrefix . $flag;
		}

		$this->flagged->removeClasses( $remove );
		$this->flags = [];

		return $this;
	}

	/**
	 * Add one or more flags.
	 *
	 * @param string|array $flags One or more flags to add, or an array keyed by flag name
	 *   containing boolean set/remove instructions.
	 * @return $this
	 */
	public function setFlags( $flags ) {
		$add = [];
		$remove = [];
		$classPrefix = 'oo-ui-flaggedElement-';

		if ( is_string( $flags ) ) {
			// Set
			if ( !isset( $this->flags[$flags] ) ) {
				$this->flags[$flags] = true;
				$add[] = $classPrefix . $flags;
			}
		} elseif ( is_array( $flags ) ) {
			foreach ( $flags as $key => $value ) {
				if ( is_numeric( $key ) ) {
					// Set
					if ( !isset( $this->flags[$value] ) ) {
						$this->flags[$value] = true;
						$add[] = $classPrefix . $value;
					}
				} else {
					if ( $value ) {
						// Set
						if ( !isset( $this->flags[$key] ) ) {
							$this->flags[$key] = true;
							$add[] = $classPrefix . $key;
						}
					} else {
						// Remove
						if ( isset( $this->flags[$key] ) ) {
							unset( $this->flags[$key] );
							$remove[] = $classPrefix . $key;
						}
					}
				}
			}
		}

		$this->flagged
			->addClasses( $add )
			->removeClasses( $remove );

		return $this;
	}

	/**
	 * @param callable $func
	 */
	abstract public function registerConfigCallback( callable $func );
}