2 * HTMLForm enhancements:
3 * Add a dynamic max length to the reason field of SelectAndOther.
7 // cache the separator to avoid object creation on each keypress
8 var colonSeparator
= mw
.message( 'colon-separator' ).text();
10 mw
.hook( 'htmlform.enhance' ).add( function ( $root
) {
11 // This checks the length together with the value from the select field
12 // When the reason list is changed and the bytelimit is longer than the allowed,
15 .find( '.mw-htmlform-select-and-other-field' )
17 var $reasonList
, currentValReasonList
, maxlengthUnit
, lengthLimiter
, widget
,
19 $widget
= $this.closest( '.oo-ui-widget[data-ooui]' );
22 mw
.loader
.using( 'mediawiki.widgets.SelectWithInputWidget', function () {
23 widget
= OO
.ui
.Widget
.static.infuse( $widget
);
24 maxlengthUnit
= widget
.getData().maxlengthUnit
;
25 lengthLimiter
= maxlengthUnit
=== 'codepoints' ? 'codePointLimit' : 'byteLimit';
26 widget
.textinput
.$input
[ lengthLimiter
]( function ( input
) {
27 // Should be built the same as in HTMLSelectAndOtherField::loadDataFromRequest
28 var comment
= widget
.dropdowninput
.getValue();
29 if ( comment
=== 'other' ) {
31 } else if ( input
!== '' ) {
32 // Entry from drop down menu + additional comment
33 comment
+= colonSeparator
+ input
;
39 // find the reason list
40 $reasonList
= $root
.find( '#' + $this.data( 'id-select' ) );
41 // cache the current selection to avoid expensive lookup
42 currentValReasonList
= $reasonList
.val();
44 $reasonList
.change( function () {
45 currentValReasonList
= $reasonList
.val();
48 // Select the function for the length limit
49 maxlengthUnit
= $this.data( 'mw-maxlength-unit' );
50 lengthLimiter
= maxlengthUnit
=== 'codepoints' ? 'codePointLimit' : 'byteLimit';
51 $this[ lengthLimiter
]( function ( input
) {
52 // Should be built the same as in HTMLSelectAndOtherField::loadDataFromRequest
53 var comment
= currentValReasonList
;
54 if ( comment
=== 'other' ) {
56 } else if ( input
!== '' ) {
57 // Entry from drop down menu + additional comment
58 comment
+= colonSeparator
+ input
;
66 }( mediaWiki
, jQuery
) );