diff options
author | Func <Funcer@outlook.com> | 2021-12-18 03:47:45 +0000 |
---|---|---|
committer | Bartosz DziewoĆski <matma.rex@gmail.com> | 2022-01-07 22:14:23 +0100 |
commit | 2f3b8a4c9b58c886419badc2004bc9c408f5a40b (patch) | |
tree | 37982f668848b60a8f1345f96816f58a61983948 /includes/htmlform | |
parent | 11c1cacf000d2e196369f294423529906b5d77fc (diff) | |
download | mediawikicore-2f3b8a4c9b58c886419badc2004bc9c408f5a40b.tar.gz mediawikicore-2f3b8a4c9b58c886419badc2004bc9c408f5a40b.zip |
Add cond-state classes in the server-side
There is only one use case of cond-state classes passed to the client-side, and it can be done more natively.
Change-Id: Ib6835bb334bd65c6176f0a5c3881b20265901af9
Diffstat (limited to 'includes/htmlform')
-rw-r--r-- | includes/htmlform/HTMLFormElement.php | 7 | ||||
-rw-r--r-- | includes/htmlform/HTMLFormField.php | 32 | ||||
-rw-r--r-- | includes/htmlform/fields/HTMLAutoCompleteSelectField.php | 1 | ||||
-rw-r--r-- | includes/htmlform/fields/HTMLCheckMatrix.php | 4 | ||||
-rw-r--r-- | includes/htmlform/fields/HTMLFormFieldCloner.php | 2 |
5 files changed, 23 insertions, 23 deletions
diff --git a/includes/htmlform/HTMLFormElement.php b/includes/htmlform/HTMLFormElement.php index 1a1228860b74..366fdf6b4bbc 100644 --- a/includes/htmlform/HTMLFormElement.php +++ b/includes/htmlform/HTMLFormElement.php @@ -16,13 +16,10 @@ trait HTMLFormElement { public function initializeHTMLFormElement( array $config = [] ) { // Properties - $this->condState = $config['condState'] ?? [ 'class' => [] ]; + $this->condState = $config['condState'] ?? []; $this->modules = $config['modules'] ?? []; // Initialization - if ( $this->condState['class'] ) { - $this->addClasses( $this->condState['class'] ); - } if ( $this->modules ) { // JS code must be able to read this before infusing (before OOUI is even loaded), // so we put this in a separate attribute (not with the rest of the config). @@ -30,7 +27,7 @@ trait HTMLFormElement { $this->setAttributes( [ 'data-mw-modules' => implode( ',', $this->modules ) ] ); } $this->registerConfigCallback( function ( &$config ) { - if ( $this->condState['class'] ) { + if ( $this->condState ) { $config['condState'] = $this->condState; } } ); diff --git a/includes/htmlform/HTMLFormField.php b/includes/htmlform/HTMLFormField.php index 386d02cb5fe6..f5f57f6a7059 100644 --- a/includes/htmlform/HTMLFormField.php +++ b/includes/htmlform/HTMLFormField.php @@ -28,7 +28,8 @@ abstract class HTMLFormField { /** * @var array Array to hold params for 'hide-if' or 'disable-if' statements */ - protected $mCondState = [ 'class' => [] ]; + protected $mCondState = []; + protected $mCondStateClass = []; /** * @var bool If true will generate an empty div element with no label @@ -275,7 +276,7 @@ abstract class HTMLFormField { * @return bool */ public function isHidden( $alldata ) { - if ( !$this->mCondState['class'] || !isset( $this->mCondState['hide'] ) ) { + if ( !( $this->mCondState && isset( $this->mCondState['hide'] ) ) ) { return false; } @@ -469,13 +470,13 @@ abstract class HTMLFormField { if ( isset( $params['hide-if'] ) && $params['hide-if'] ) { $this->mCondState['hide'] = $params['hide-if']; - $this->mCondState['class'][] = 'mw-htmlform-hide-if'; + $this->mCondStateClass[] = 'mw-htmlform-hide-if'; } if ( !( isset( $params['disabled'] ) && $params['disabled'] ) && isset( $params['disable-if'] ) && $params['disable-if'] ) { $this->mCondState['disable'] = $params['disable-if']; - $this->mCondState['class'][] = 'mw-htmlform-disable-if'; + $this->mCondStateClass[] = 'mw-htmlform-disable-if'; } } @@ -512,9 +513,9 @@ abstract class HTMLFormField { $inputHtml . "\n$errors" ); - if ( $this->mCondState['class'] ) { + if ( $this->mCondState ) { $rowAttributes['data-cond-state'] = FormatJson::encode( $this->mCondState ); - $rowClasses .= implode( ' ', $this->mCondState['class'] ); + $rowClasses .= implode( ' ', $this->mCondStateClass ); } if ( $verticalLabel ) { @@ -577,9 +578,9 @@ abstract class HTMLFormField { $wrapperAttributes = [ 'class' => $divCssClasses, ]; - if ( $this->mCondState['class'] ) { + if ( $this->mCondState ) { $wrapperAttributes['data-cond-state'] = FormatJson::encode( $this->mCondState ); - $wrapperAttributes['class'] = array_merge( $wrapperAttributes['class'], $this->mCondState['class'] ); + $wrapperAttributes['class'] = array_merge( $wrapperAttributes['class'], $this->mCondStateClass ); } $html = Html::rawElement( 'div', $wrapperAttributes, $label . $field ); $html .= $helptext; @@ -643,6 +644,9 @@ abstract class HTMLFormField { $preloadModules = true; $config['classes'][] = 'mw-htmlform-autoinfuse'; } + if ( $this->mCondState ) { + $config['classes'] = array_merge( $config['classes'], $this->mCondStateClass ); + } // the element could specify, that the label doesn't need to be added $label = $this->getLabel(); @@ -650,7 +654,7 @@ abstract class HTMLFormField { $config['label'] = new OOUI\HtmlSnippet( $label ); } - if ( $this->mCondState['class'] ) { + if ( $this->mCondState ) { $preloadModules = true; $config['condState'] = $this->mCondState; } @@ -800,9 +804,9 @@ abstract class HTMLFormField { } $rowAttributes = []; - if ( $this->mCondState['class'] ) { + if ( $this->mCondState ) { $rowAttributes['data-cond-state'] = FormatJson::encode( $this->mCondState ); - $rowAttributes['class'] = $this->mCondState['class']; + $rowAttributes['class'] = $this->mCondStateClass; } $tdClasses = [ 'htmlform-tip' ]; @@ -834,9 +838,9 @@ abstract class HTMLFormField { if ( $this->mHelpClass !== false ) { $wrapperAttributes['class'][] = $this->mHelpClass; } - if ( $this->mCondState['class'] ) { + if ( $this->mCondState ) { $wrapperAttributes['data-cond-state'] = FormatJson::encode( $this->mCondState ); - $wrapperAttributes['class'] = array_merge( $wrapperAttributes['class'], $this->mCondState['class'] ); + $wrapperAttributes['class'] = array_merge( $wrapperAttributes['class'], $this->mCondStateClass ); } $div = Html::rawElement( 'div', $wrapperAttributes, $helptext ); @@ -1237,7 +1241,7 @@ abstract class HTMLFormField { * @since 1.29 */ public function needsJSForHtml5FormValidation() { - if ( $this->mCondState['class'] ) { + if ( $this->mCondState ) { // This is probably more restrictive than it needs to be, but better safe than sorry return true; } diff --git a/includes/htmlform/fields/HTMLAutoCompleteSelectField.php b/includes/htmlform/fields/HTMLAutoCompleteSelectField.php index 5acb4b4e0520..c4fa2f7a8afc 100644 --- a/includes/htmlform/fields/HTMLAutoCompleteSelectField.php +++ b/includes/htmlform/fields/HTMLAutoCompleteSelectField.php @@ -136,7 +136,6 @@ class HTMLAutoCompleteSelectField extends HTMLTextField { if ( $this->getOptions() ) { $attribs['data-cond-state'] = FormatJson::encode( [ 'hide' => [ '!==', $this->mName . '-select', 'other' ], - 'class' => 'mw-htmlform-hide-if', ] ); } diff --git a/includes/htmlform/fields/HTMLCheckMatrix.php b/includes/htmlform/fields/HTMLCheckMatrix.php index a9eb35e0cbb1..6713a38ffc41 100644 --- a/includes/htmlform/fields/HTMLCheckMatrix.php +++ b/includes/htmlform/fields/HTMLCheckMatrix.php @@ -215,9 +215,9 @@ class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable { $moreClass = ''; $moreAttributes = []; - if ( $this->mCondState['class'] ) { + if ( $this->mCondState ) { $moreAttributes['data-cond-state'] = FormatJson::encode( $this->mCondState ); - $moreClass = implode( ' ', $this->mCondState['class'] ); + $moreClass = implode( ' ', $this->mCondStateClass ); } $label = $this->getLabelHtml( $cellAttributes ); diff --git a/includes/htmlform/fields/HTMLFormFieldCloner.php b/includes/htmlform/fields/HTMLFormFieldCloner.php index 655d97cf17bc..959846fa4b24 100644 --- a/includes/htmlform/fields/HTMLFormFieldCloner.php +++ b/includes/htmlform/fields/HTMLFormFieldCloner.php @@ -104,7 +104,7 @@ class HTMLFormFieldCloner extends HTMLFormField { } // Copy the hide-if and disable-if rules to "child" fields, so that the JavaScript code handling them // (resources/src/mediawiki.htmlform/cond-state.js) doesn't have to handle nested fields. - if ( $this->mCondState['class'] ) { + if ( $this->mCondState ) { foreach ( [ 'hide', 'disable' ] as $type ) { if ( !isset( $this->mCondState[$type] ) ) { continue; |