Fix doxygen return class with namespace
[lhc/web/wiklou.git] / includes / htmlform / HTMLFormField.php
index 0c3fe44..1d8137e 100644 (file)
@@ -44,6 +44,17 @@ abstract class HTMLFormField {
         */
        abstract function getInputHTML( $value );
 
+       /**
+        * Same as getInputHTML, but returns an OOUI object.
+        * Defaults to false, which getOOUI will interpret as "use the HTML version"
+        *
+        * @param string $value
+        * @return OOUI\\Widget|false
+        */
+       function getInputOOUI( $value ) {
+               return false;
+       }
+
        /**
         * Get a translated interface message
         *
@@ -513,12 +524,20 @@ abstract class HTMLFormField {
                        'mw-htmlform-nolabel' => ( $label === '' )
                );
 
-               $field = Html::rawElement(
-                       'div',
-                       array( 'class' => $outerDivClass ) + $cellAttributes,
-                       $inputHtml . "\n$errors"
-               );
-               $divCssClasses = array( "mw-htmlform-field-$fieldType", $this->mClass, $this->mVFormClass, $errorClass );
+               $horizontalLabel = isset( $this->mParams['horizontal-label'] )
+                       ? $this->mParams['horizontal-label'] : false;
+
+               if ( $horizontalLabel ) {
+                       $field = ' ' . $inputHtml . "\n$errors";
+               } else {
+                       $field = Html::rawElement(
+                               'div',
+                               array( 'class' => $outerDivClass ) + $cellAttributes,
+                               $inputHtml . "\n$errors"
+                       );
+               }
+               $divCssClasses = array( "mw-htmlform-field-$fieldType",
+                       $this->mClass, $this->mVFormClass, $errorClass );
 
                $wrapperAttributes = array(
                        'class' => $divCssClasses,
@@ -533,6 +552,68 @@ abstract class HTMLFormField {
                return $html;
        }
 
+       /**
+        * Get the OOUI version of the div. Falls back to getDiv by default.
+        * @since 1.26
+        *
+        * @param string $value The value to set the input to.
+        *
+        * @return string
+        */
+       public function getOOUI( $value ) {
+               list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
+
+               $inputField = $this->getInputOOUI( $value );
+
+               if ( !$inputField ) {
+                       // This field doesn't have an OOUI implementation yet at all.
+                       // OK, use this trick:
+                       return $this->getDiv( $value );
+               }
+
+               $infusable = true;
+               if ( is_string( $inputField ) ) {
+                       // Mmm… We have an OOUI implementation, but it's not complete, and we got a load of HTML.
+                       // Cheat a little and wrap it in a widget! It won't be infusable, though, since client-side
+                       // JavaScript doesn't know how to rebuilt the contents.
+                       $inputField = new OOUI\Widget( array( 'content' => new OOUI\HtmlSnippet( $inputField ) ) );
+                       $infusable = false;
+               }
+
+               $fieldType = get_class( $this );
+               $helpText = $this->getHelpText();
+               $config = array(
+                       'classes' => array( "mw-htmlform-field-$fieldType", $this->mClass, $errorClass ),
+                       'align' => $this->getLabelAlignOOUI(),
+                       'label' => $this->getLabel(),
+                       'help' => $helpText !== null ? new OOUI\HtmlSnippet( $helpText ) : null,
+                       'infusable' => $infusable,
+               );
+               $field = $this->getFieldLayoutOOUI( $inputField, $config );
+
+               return $field . $errors;
+       }
+
+       /**
+        * Get label alignment when generating field for OOUI.
+        * @return string 'left', 'right', 'top' or 'inline'
+        */
+       protected function getLabelAlignOOUI() {
+               return 'top';
+       }
+
+       /**
+        * Get a FieldLayout (or subclass thereof) to wrap this field in when using OOUI output.
+        * @return OOUI\\FieldLayout|OOUI\\ActionFieldLayout
+        */
+       protected function getFieldLayoutOOUI( $inputField, $config ) {
+               if ( isset( $this->mClassWithButton ) ) {
+                       $buttonWidget = $this->mClassWithButton->getInputOOUI( '' );
+                       return new OOUI\ActionFieldLayout( $inputField, $buttonWidget, $config );
+               }
+               return new OOUI\FieldLayout( $inputField, $config );
+       }
+
        /**
         * Get the complete raw fields for the input, including help text,
         * labels, and whatever.
@@ -662,7 +743,7 @@ abstract class HTMLFormField {
        /**
         * Determine the help text to display
         * @since 1.20
-        * @return string
+        * @return string HTML
         */
        public function getHelpText() {
                $helptext = null;
@@ -713,6 +794,9 @@ abstract class HTMLFormField {
                return array( $errors, $errorClass );
        }
 
+       /**
+        * @return string
+        */
        function getLabel() {
                return is_null( $this->mLabel ) ? '' : $this->mLabel;
        }
@@ -734,6 +818,8 @@ abstract class HTMLFormField {
 
                $displayFormat = $this->mParent->getDisplayFormat();
                $html = '';
+               $horizontalLabel = isset( $this->mParams['horizontal-label'] )
+                       ? $this->mParams['horizontal-label'] : false;
 
                if ( $displayFormat === 'table' ) {
                        $html =
@@ -741,7 +827,7 @@ abstract class HTMLFormField {
                                        array( 'class' => 'mw-label' ) + $cellAttributes,
                                        Html::rawElement( 'label', $for, $labelValue ) );
                } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
-                       if ( $displayFormat === 'div' ) {
+                       if ( $displayFormat === 'div' && !$horizontalLabel ) {
                                $html =
                                        Html::rawElement( 'div',
                                                array( 'class' => 'mw-label' ) + $cellAttributes,
@@ -775,24 +861,44 @@ abstract class HTMLFormField {
                return Linker::tooltipAndAccesskeyAttribs( $this->mParams['tooltip'] );
        }
 
+       /**
+        * Get a translated key if necessary.
+        * @param array|null $mappings Array of mappings, 'original' => 'translated'
+        * @param string $key
+        * @return string
+        */
+       protected function getMappedKey( $mappings, $key ) {
+               if ( !is_array( $mappings ) ) {
+                       return $key;
+               }
+
+               if ( !empty( $mappings[$key] ) ) {
+                       return $mappings[$key];
+               }
+
+               return $key;
+       }
+
        /**
         * Returns the given attributes from the parameters
         *
         * @param array $list List of attributes to get
+        * @param array $mappings Optional - Key/value map of attribute names to use instead of the ones passed in
         * @return array Attributes
         */
-       public function getAttributes( array $list ) {
-               static $boolAttribs = array( 'disabled', 'required', 'autofocus', 'multiple', 'readonly' );
+       public function getAttributes( array $list, array $mappings = null ) {
+               static $boolAttribs = array( 'disabled', 'required', 'multiple', 'readonly' );
 
                $ret = array();
-
                foreach ( $list as $key ) {
+                       $mappedKey = $this->getMappedKey( $mappings, $key );
+
                        if ( in_array( $key, $boolAttribs ) ) {
                                if ( !empty( $this->mParams[$key] ) ) {
-                                       $ret[$key] = '';
+                                       $ret[$mappedKey] = '';
                                }
                        } elseif ( isset( $this->mParams[$key] ) ) {
-                               $ret[$key] = $this->mParams[$key];
+                               $ret[$mappedKey] = $this->mParams[$key];
                        }
                }
 
@@ -881,6 +987,29 @@ abstract class HTMLFormField {
                return $this->mOptions;
        }
 
+       /**
+        * Get options and make them into arrays suitable for OOUI.
+        * @return array Options for inclusion in a select or whatever.
+        */
+       public function getOptionsOOUI() {
+               $oldoptions = $this->getOptions();
+
+               if ( $oldoptions === null ) {
+                       return null;
+               }
+
+               $options = array();
+
+               foreach ( $oldoptions as $text => $data ) {
+                       $options[] = array(
+                               'data' => $data,
+                               'label' => $text,
+                       );
+               }
+
+               return $options;
+       }
+
        /**
         * flatten an array of options to a single array, for instance,
         * a set of "<options>" inside "<optgroups>".