summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki/htmlform/selectandother.js
blob: fda6742063eafa3fc6a0415b94f774772ddbf268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * HTMLForm enhancements:
 * Add a dynamic max length to the reason field of SelectAndOther.
 */
( function ( mw, $ ) {

	// cache the separator to avoid object creation on each keypress
	var colonSeparator = mw.message( 'colon-separator' ).text();

	mw.hook( 'htmlform.enhance' ).add( function ( $root ) {
		// 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 $reasonList, currentValReasonList, maxlengthUnit, lengthLimiter, widget,
					$this = $( this ),
					$widget = $this.closest( '.oo-ui-widget[data-ooui]' );

				if ( $widget ) {
					mw.loader.using( 'mediawiki.widgets.SelectWithInputWidget', function () {
						widget = OO.ui.Widget.static.infuse( $widget );
						maxlengthUnit = widget.getData().maxlengthUnit;
						lengthLimiter = maxlengthUnit === 'codepoints' ? 'codePointLimit' : 'byteLimit';
						widget.textinput.$input[ lengthLimiter ]( function ( input ) {
							// Should be built the same as in HTMLSelectAndOtherField::loadDataFromRequest
							var comment = widget.dropdowninput.getValue();
							if ( comment === 'other' ) {
								comment = input;
							} else if ( input !== '' ) {
								// Entry from drop down menu + additional comment
								comment += colonSeparator + input;
							}
							return comment;
						} );
					} );
				} else {
					// 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();
					} );

					// Select the function for the length limit
					maxlengthUnit = $this.data( 'mw-maxlength-unit' );
					lengthLimiter = maxlengthUnit === 'codepoints' ? 'codePointLimit' : 'byteLimit';
					$this[ lengthLimiter ]( 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;
					} );
				}
			} );
	} );

}( mediaWiki, jQuery ) );