OOUIHTMLForm: Implement HTMLMultiSelectField
authorBartosz Dziewoński <matma.rex@gmail.com>
Sun, 14 Jun 2015 18:14:31 +0000 (20:14 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Sat, 27 Jun 2015 13:14:25 +0000 (13:14 +0000)
Example usage in I30d401cc7c827b21eb2fb116558d0d9e764ec1f3.
Following the template set by HTMLCheckMatrix, which for some reason
was implemented first. Also removed unfulfillable @todo comment.

Also corrected HTMLCheckMatrix code not to wrap OOUI widgets in
MediaWiki UI wrappers even when 'UseMediaWikiUIEverywhere' is set to
true, and to set the correct 'value' for checkboxes.

Bug: T100955
Change-Id: Ib5d000ca9a08abc8086ee05b5122116b086242ad

includes/htmlform/HTMLCheckMatrix.php
includes/htmlform/HTMLMultiSelectField.php

index 7ccb60e..a0566a0 100644 (file)
@@ -119,9 +119,8 @@ class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable {
                        foreach ( $columns as $columnTag ) {
                                $thisTag = "$columnTag-$rowTag";
                                // Construct the checkbox
-                               $thisId = "{$this->mID}-$thisTag";
                                $thisAttribs = array(
-                                       'id' => $thisId,
+                                       'id' => "{$this->mID}-$thisTag",
                                        'value' => $thisTag,
                                );
                                $checked = in_array( $thisTag, (array)$value, true );
@@ -132,18 +131,13 @@ class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable {
                                        $checked = true;
                                        $thisAttribs['disabled'] = 1;
                                }
-                               $chkBox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs );
 
-                               if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
-                                       $chkBox = Html::openElement( 'div', array( 'class' => 'mw-ui-checkbox' ) ) .
-                                               $chkBox .
-                                               Html::element( 'label', array( 'for' => $thisId ) ) .
-                                               Html::closeElement( 'div' );
-                               }
+                               $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs );
+
                                $rowContents .= Html::rawElement(
                                        'td',
                                        array(),
-                                       $chkBox
+                                       $checkbox
                                );
                        }
                        $tableContents .= Html::rawElement( 'tr', array(), "\n$rowContents\n" );
@@ -162,11 +156,18 @@ class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable {
                        return new OOUI\CheckboxInputWidget( array(
                                'name' => "{$this->mName}[]",
                                'selected' => $checked,
-                               'value' => '1',
+                               'value' => $attribs['value'],
                        ) + $attribs );
+               } else {
+                       $checkbox = Xml::check( "{$this->mName}[]", $checked, $attribs );
+                       if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
+                               $checkbox = Html::openElement( 'div', array( 'class' => 'mw-ui-checkbox' ) ) .
+                                       $checkbox .
+                                       Html::element( 'label', array( 'for' => $attribs['id'] ) ) .
+                                       Html::closeElement( 'div' );
+                       }
+                       return $checkbox;
                }
-
-               return Xml::check( "{$this->mName}[]", $checked, $attribs );
        }
 
        protected function isTagForcedOff( $tag ) {
index 8d28b59..523f045 100644 (file)
@@ -38,34 +38,19 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable
                $html = '';
 
                $attribs = $this->getAttributes( array( 'disabled', 'tabindex' ) );
-               $elementFunc = array( 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' );
 
                foreach ( $options as $label => $info ) {
                        if ( is_array( $info ) ) {
                                $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
                                $html .= $this->formatOptions( $info, $value );
                        } else {
-                               $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
-
-                               // @todo: Make this use checkLabel for consistency purposes
-                               $checkbox = Xml::check(
-                                       $this->mName . '[]',
-                                       in_array( $info, $value, true ),
-                                       $attribs + $thisAttribs
-                               );
-                               $checkbox .= '&#160;' . call_user_func( $elementFunc,
-                                       'label',
-                                       array( 'for' => "{$this->mID}-$info" ),
-                                       $label
+                               $thisAttribs = array(
+                                       'id' => "{$this->mID}-$info",
+                                       'value' => $info,
                                );
+                               $checked = in_array( $info, $value, true );
 
-                               if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
-                                       $checkbox = Html::rawElement(
-                                               'div',
-                                               array( 'class' => 'mw-ui-checkbox' ),
-                                               $checkbox
-                                       );
-                               }
+                               $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
 
                                $html .= ' ' . Html::rawElement(
                                        'div',
@@ -78,6 +63,41 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable
                return $html;
        }
 
+       protected function getOneCheckbox( $checked, $attribs, $label ) {
+               if ( $this->mParent instanceof OOUIHTMLForm ) {
+                       if ( $this->mOptionsLabelsNotFromMessage ) {
+                               $label = new OOUI\HtmlSnippet( $label );
+                       }
+                       return new OOUI\FieldLayout(
+                               new OOUI\CheckboxInputWidget( array(
+                                       'name' => "{$this->mName}[]",
+                                       'selected' => $checked,
+                                       'value' => $attribs['value'],
+                               ) + $attribs ),
+                               array(
+                                       'label' => $label,
+                                       'align' => 'inline',
+                               )
+                       );
+               } else {
+                       $elementFunc = array( 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' );
+                       $checkbox =
+                               Xml::check( "{$this->mName}[]", $checked, $attribs ) .
+                               '&#160;' .
+                               call_user_func( $elementFunc,
+                                       'label',
+                                       array( 'for' => $attribs['id'] ),
+                                       $label
+                               );
+                       if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
+                               $checkbox = Html::openElement( 'div', array( 'class' => 'mw-ui-checkbox' ) ) .
+                                       $checkbox .
+                                       Html::closeElement( 'div' );
+                       }
+                       return $checkbox;
+               }
+       }
+
        /**
         * @param WebRequest $request
         *