X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/vendor/oojs/oojs-ui/php/mixins/FlaggedElement.php diff --git a/vendor/oojs/oojs-ui/php/mixins/FlaggedElement.php b/vendor/oojs/oojs-ui/php/mixins/FlaggedElement.php new file mode 100644 index 00000000..1e2b5da7 --- /dev/null +++ b/vendor/oojs/oojs-ui/php/mixins/FlaggedElement.php @@ -0,0 +1,133 @@ +flagged = isset( $config['flagged'] ) ? $config['flagged'] : $this; + + // Initialization + $this->setFlags( isset( $config['flags'] ) ? $config['flags'] : null ); + + $this->registerConfigCallback( function ( &$config ) { + if ( !empty( $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 ) { + $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; + } +}