X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/vendor/oojs/oojs-ui/php/widgets/RadioSelectInputWidget.php diff --git a/vendor/oojs/oojs-ui/php/widgets/RadioSelectInputWidget.php b/vendor/oojs/oojs-ui/php/widgets/RadioSelectInputWidget.php new file mode 100644 index 00000000..30492c6d --- /dev/null +++ b/vendor/oojs/oojs-ui/php/widgets/RadioSelectInputWidget.php @@ -0,0 +1,126 @@ + …, 'label' => … ]` + */ + public function __construct( array $config = [] ) { + // Parent constructor + parent::__construct( $config ); + + if ( isset( $config['name'] ) ) { + $this->name = $config['name']; + } + + // Initialization + $this->setOptions( isset( $config['options'] ) ? $config['options'] : [] ); + $this->addClasses( [ 'oo-ui-radioSelectInputWidget' ] ); + } + + protected function getInputElement( $config ) { + // Actually unused + return new Tag( 'unused' ); + } + + public function setValue( $value ) { + $this->value = $this->cleanUpValue( $value ); + foreach ( $this->fields as &$field ) { + $field->getField()->setSelected( $field->getField()->getValue() === $this->value ); + } + return $this; + } + + /** + * Set the options available for this input. + * + * @param array[] $options Array of menu options in the format + * `[ 'data' => …, 'label' => … ]` + * @return $this + */ + public function setOptions( $options ) { + $value = $this->getValue(); + $isValueAvailable = false; + $this->fields = []; + + // Rebuild the radio buttons + $this->clearContent(); + // Need a unique name, otherwise more than one radio will be selectable + // Note: This is not going in the ID attribute, not that it matters + $name = $this->name ?: Tag::generateElementId(); + foreach ( $options as $opt ) { + $optValue = $this->cleanUpValue( $opt['data'] ); + $field = new FieldLayout( + new RadioInputWidget( [ + 'name' => $name, + 'value' => $optValue, + 'disabled' => $this->isDisabled(), + ] ), + [ + 'label' => isset( $opt['label'] ) ? $opt['label'] : $optValue, + 'align' => 'inline', + ] + ); + + if ( $value === $optValue ) { + $isValueAvailable = true; + } + + $this->fields[] = $field; + $this->appendContent( $field ); + } + + // Restore the previous value, or reset to something sensible + if ( $isValueAvailable ) { + // Previous value is still available + $this->setValue( $value ); + } else { + // No longer valid, reset + if ( count( $options ) ) { + $this->setValue( $options[0]['data'] ); + } + } + + return $this; + } + + public function setDisabled( $state ) { + parent::setDisabled( $state ); + foreach ( $this->fields as $field ) { + $field->getField()->setDisabled( $this->isDisabled() ); + } + return $this; + } + + public function getConfig( &$config ) { + $o = []; + foreach ( $this->fields as $field ) { + $label = $field->getLabel(); + $data = $field->getField()->getValue(); + $o[] = [ 'data' => $data, 'label' => $label ]; + } + $config['options'] = $o; + return parent::getConfig( $config ); + } +}