diff --git a/roles/mediawiki/files/InputWidget.php b/roles/mediawiki/files/InputWidget.php new file mode 100644 index 0000000000..e177f22a8e --- /dev/null +++ b/roles/mediawiki/files/InputWidget.php @@ -0,0 +1,172 @@ +input = $this->getInputElement( $config ); + + // Traits + $this->initializeFlaggedElement( array_merge( $config, [ 'flagged' => $this ] ) ); + $this->initializeTabIndexedElement( + array_merge( $config, [ 'tabIndexed' => $this->input ] ) ); + $this->initializeTitledElement( + array_merge( $config, [ 'titled' => $this->input ] ) ); + $this->initializeAccessKeyedElement( + array_merge( $config, [ 'accessKeyed' => $this->input ] ) ); + + // Initialization + if ( isset( $config['name'] ) ) { + $this->input->setAttributes( [ 'name' => $config['name'] ] ); + } + if ( $this->isDisabled() ) { + $this->input->setAttributes( [ 'disabled' => 'disabled' ] ); + } + $this + ->addClasses( [ 'oo-ui-inputWidget' ] ) + ->appendContent( $this->input ); + $this->input->addClasses( [ 'oo-ui-inputWidget-input' ] ); + $this->setValue( $config['value'] ?? null ); + if ( isset( $config['dir'] ) ) { + $this->setDir( $config['dir'] ); + } + if ( isset( $config['inputId'] ) ) { + $this->setInputId( $config['inputId'] ); + } + } + + /** + * Get input element. + * + * @param array $config Configuration options + * @return Tag Input element + */ + protected function getInputElement( $config ) { + return new Tag( 'input' ); + } + + /** + * Get the value of the input. + * + * @return string Input value + */ + public function getValue() { + return $this->value; + } + + /** + * Set the directionality of the input. + * + * @param string $dir Text directionality: 'ltr', 'rtl' or 'auto' + * @return $this + */ + public function setDir( $dir ) { + $this->input->setAttributes( [ 'dir' => $dir ] ); + return $this; + } + + /** + * Set the value of the input. + * + * @param string $value New value + * @return $this + */ + public function setValue( $value ) { + $this->value = $this->cleanUpValue( $value ); + $this->input->setValue( $this->value ); + return $this; + } + + /** + * Clean up incoming value. + * + * Ensures value is a string, and converts null to empty string. + * + * @param string $value Original value + * @return string Cleaned up value + */ + protected function cleanUpValue( $value ) { + if ( $value === null ) { + return ''; + } else { + return (string)$value; + } + } + + public function setDisabled( $state ) { + parent::setDisabled( $state ); + if ( isset( $this->input ) ) { + if ( $this->isDisabled() ) { + $this->input->setAttributes( [ 'disabled' => 'disabled' ] ); + } else { + $this->input->removeAttributes( [ 'disabled' ] ); + } + } + return $this; + } + + /** + * Set the 'id' attribute of the `` element. + * + * @param string $id The ID of the input element + * @return $this + */ + public function setInputId( $id ) { + $this->input->setAttributes( [ 'id' => $id ] ); + return $this; + } + + public function getConfig( &$config ) { + $name = $this->input->getAttribute( 'name' ); + if ( $name !== null ) { + $config['name'] = $name; + } + if ( $this->value !== '' ) { + $config['value'] = $this->value; + } + $id = $this->input->getAttribute( 'id' ); + if ( $id !== null ) { + $config['inputId'] = $id; + } + return parent::getConfig( $config ); + } +}