Merge "Improve sorting on SpecialWanted*-Pages"
[mediawiki.git] / resources / src / mediawiki / htmlform / selectandother.js
blob95227d01946f371bdab549c9411378c8e7b7d495
1 /*
2  * HTMLForm enhancements:
3  * Add a dynamic max length to the reason field of SelectAndOther.
4  */
5 ( function ( mw, $ ) {
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,
13                 // nothing is done
14                 $root
15                         .find( '.mw-htmlform-select-and-other-field' )
16                         .each( function () {
17                                 var $this = $( this ),
18                                         // find the reason list
19                                         $reasonList = $root.find( '#' + $this.data( 'id-select' ) ),
20                                         // cache the current selection to avoid expensive lookup
21                                         currentValReasonList = $reasonList.val();
23                                 $reasonList.change( function () {
24                                         currentValReasonList = $reasonList.val();
25                                 } );
27                                 $this.byteLimit( function ( input ) {
28                                         // Should be built the same as in HTMLSelectAndOtherField::loadDataFromRequest
29                                         var comment = currentValReasonList;
30                                         if ( comment === 'other' ) {
31                                                 comment = input;
32                                         } else if ( input !== '' ) {
33                                                 // Entry from drop down menu + additional comment
34                                                 comment += colonSeparator + input;
35                                         }
36                                         return comment;
37                                 } );
38                         } );
39         } );
41 }( mediaWiki, jQuery ) );