From b18197dd81794e06ab2f930171019cf5196fb3cb Mon Sep 17 00:00:00 2001 From: umherirrender Date: Sat, 26 Jul 2014 23:56:37 +0200 Subject: [PATCH] Allow maxlength attribute on HTMLSelectAndOtherField A reason on a SelectAndOtherField can have two parts, one of the scroll down box and a free text field. The free text field is actually unlimited. This patch allows the maxlength on that field. To respect the concat of the two parts, also javascript code is added, which adds a dynamic maxlength to respect also the text from the scroll down box. The HTMLSelectAndOtherField is only used on Special:Block, where the maxlength attribute is now set to 255 (length of the database field ipb_reason). Change-Id: I5c164b41ab047e7ecf9d92db6eddcc980e2db048 --- includes/htmlform/HTMLSelectAndOtherField.php | 8 +++-- includes/specials/SpecialBlock.php | 1 + resources/Resources.php | 7 +++- resources/src/mediawiki/mediawiki.htmlform.js | 34 ++++++++++++++++++- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/includes/htmlform/HTMLSelectAndOtherField.php b/includes/htmlform/HTMLSelectAndOtherField.php index 65176dd7ac..1175bd3b51 100644 --- a/includes/htmlform/HTMLSelectAndOtherField.php +++ b/includes/htmlform/HTMLSelectAndOtherField.php @@ -39,10 +39,12 @@ class HTMLSelectAndOtherField extends HTMLSelectField { $textAttribs = array( 'id' => $this->mID . '-other', 'size' => $this->getSize(), + 'class' => array( 'mw-htmlform-select-and-other-field' ), + 'data-id-select' => $this->mID, ); if ( $this->mClass !== '' ) { - $textAttribs['class'] = $this->mClass; + $textAttribs['class'][] = $this->mClass; } $allowedParams = array( @@ -50,7 +52,8 @@ class HTMLSelectAndOtherField extends HTMLSelectField { 'autofocus', 'multiple', 'disabled', - 'tabindex' + 'tabindex', + 'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js ); $textAttribs += $this->getAttributes( $allowedParams ); @@ -71,6 +74,7 @@ class HTMLSelectAndOtherField extends HTMLSelectField { $list = $request->getText( $this->mName ); $text = $request->getText( $this->mName . '-other' ); + // Should be built the same as in mediawiki.htmlform.js if ( $list == 'other' ) { $final = $text; } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) { diff --git a/includes/specials/SpecialBlock.php b/includes/specials/SpecialBlock.php index cf82b869ca..084336dbd3 100644 --- a/includes/specials/SpecialBlock.php +++ b/includes/specials/SpecialBlock.php @@ -147,6 +147,7 @@ class SpecialBlock extends FormSpecialPage { ), 'Reason' => array( 'type' => 'selectandother', + 'maxlength' => 255, 'label-message' => 'ipbreason', 'options-message' => 'ipbreason-dropdown', ), diff --git a/resources/Resources.php b/resources/Resources.php index ca90efa8dc..dc7b6f4fda 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -894,8 +894,13 @@ return array( 'scripts' => 'resources/src/mediawiki/mediawiki.htmlform.js', 'dependencies' => array( 'jquery.mwExtension', + 'jquery.byteLimit', + ), + 'messages' => array( + 'htmlform-chosen-placeholder', + // @todo Load this message in content language + 'colon-separator', ), - 'messages' => array( 'htmlform-chosen-placeholder' ), ), 'mediawiki.icon' => array( 'styles' => 'resources/src/mediawiki/mediawiki.icon.less', diff --git a/resources/src/mediawiki/mediawiki.htmlform.js b/resources/src/mediawiki/mediawiki.htmlform.js index 594800e15d..db8d8b19fe 100644 --- a/resources/src/mediawiki/mediawiki.htmlform.js +++ b/resources/src/mediawiki/mediawiki.htmlform.js @@ -237,7 +237,9 @@ } ); function enhance( $root ) { - var $matrixTooltips, $autocomplete; + var $matrixTooltips, $autocomplete, + // cache the separator to avoid object creation on each keypress + colonSeparator = mw.message( 'colon-separator' ).text(); /** * @ignore @@ -261,6 +263,36 @@ handleSelectOrOther.call( this, true ); } ); + // Add a dynamic max length to the reason field of SelectAndOther + // This checks the length together with the value from the select field + // When the reason list is changed and the bytelimit is longer than the allowed, + // nothing is done + $root + .find( '.mw-htmlform-select-and-other-field' ) + .each( function () { + var $this = $( this ), + // find the reason list + $reasonList = $root.find( '#' + $this.data( 'id-select' ) ), + // cache the current selection to avoid expensive lookup + currentValReasonList = $reasonList.val(); + + $reasonList.change( function () { + currentValReasonList = $reasonList.val(); + } ); + + $this.byteLimit( function ( input ) { + // Should be built the same as in HTMLSelectAndOtherField::loadDataFromRequest + var comment = currentValReasonList; + if ( comment === 'other' ) { + comment = input; + } else if ( input !== '' ) { + // Entry from drop down menu + additional comment + comment += colonSeparator + input; + } + return comment; + } ); + } ); + // Set up hide-if elements $root.find( '.mw-htmlform-hide-if' ).each( function () { var v, $fields, test, func, -- 2.20.1