4 const { byteLength, codePointLength } = require( 'mediawiki.String' );
5 const { colonSeparator } = require( './contentMessages.json' );
9 * @param {string} lengthLimiter
10 * @param {OO.ui.TextInputWidget} textInputWidget
11 * @param {number} [limit]
12 * @param {Function} [filterFunction]
14 function internalVisibleLimit( lengthLimiter, textInputWidget, limit, filterFunction ) {
15 limit = limit || +textInputWidget.$input.attr( 'maxlength' );
16 if ( !filterFunction || typeof filterFunction !== 'function' ) {
17 filterFunction = undefined;
20 const lengthFunction = lengthLimiter === 'byteLimit' ? byteLength : codePointLength;
21 function updateCount() {
22 let value = textInputWidget.getValue(),
24 if ( filterFunction ) {
25 value = filterFunction( value );
27 remaining = limit - lengthFunction( value );
28 if ( remaining > 99 ) {
31 remaining = mw.language.convertNumber( remaining );
33 textInputWidget.setLabel( remaining );
35 textInputWidget.on( 'change', updateCount );
39 // Actually enforce limit
40 textInputWidget.$input[ lengthLimiter ]( limit, filterFunction );
45 * @param {string} lengthLimiter
46 * @param {OO.ui.TextInputWidget} textInputWidget
47 * @param {OO.ui.DropdownInputWidget} dropdownInputWidget
48 * @param {number} [limit]
50 function internalVisibleLimitWithDropdown( lengthLimiter, textInputWidget, dropdownInputWidget, limit ) {
51 const filterFunction = function ( input ) {
52 let comment = dropdownInputWidget.getValue();
53 if ( comment === 'other' ) {
55 } else if ( input !== '' ) {
56 // Entry from drop down menu + additional comment
57 comment += colonSeparator + input;
62 internalVisibleLimit( lengthLimiter, textInputWidget, limit, filterFunction );
64 // Keep the remaining counter in sync when reason list changed
65 dropdownInputWidget.on( 'change', () => {
66 textInputWidget.emit( 'change' );
71 * Add a visible byte limit label to a TextInputWidget.
73 * Loaded from `mediawiki.widgets.visibleLengthLimit` module.
75 * Uses {@link module:jquery.lengthLimit.$.fn.byteLimit byteLimit} to enforce the limit.
77 * @param {OO.ui.TextInputWidget} textInputWidget
78 * @param {number} [limit] Byte limit, defaults to input's `maxlength` attribute
79 * @param {Function} [filterFunction] Function to call on the string before assessing the length
81 mw.widgets.visibleByteLimit = function ( textInputWidget, limit, filterFunction ) {
82 internalVisibleLimit( 'byteLimit', textInputWidget, limit, filterFunction );
86 * Add a visible codepoint (character) limit label to a TextInputWidget.
88 * Loaded from `mediawiki.widgets.visibleLengthLimit` module.
90 * Uses {@link module:jquery.lengthLimit.$.fn.codePointLimit codePointLimit} to enforce the limit.
92 * @param {OO.ui.TextInputWidget} textInputWidget
93 * @param {number} [limit] Code point limit, defaults to input's `maxlength` attribute
94 * @param {Function} [filterFunction] Function to call on the string before assessing the length
96 mw.widgets.visibleCodePointLimit = function ( textInputWidget, limit, filterFunction ) {
97 internalVisibleLimit( 'codePointLimit', textInputWidget, limit, filterFunction );
101 * Add a visible byte limit label to a TextInputWidget, assuming that the value of the
102 * DropdownInputWidget will be added as a prefix. MediaWiki formats the comment fields for many
103 * actions that way, e.g. for page deletion.
105 * Loaded from `mediawiki.widgets.visibleLengthLimit` module.
107 * Uses {@link module:jquery.lengthLimit.$.fn.byteLimit byteLimit} to enforce the limit.
109 * @param {OO.ui.TextInputWidget} textInputWidget
110 * @param {OO.ui.DropdownInputWidget} dropdownInputWidget
111 * @param {number} [limit] Code point limit, defaults to input's `maxlength` attribute
113 mw.widgets.visibleByteLimitWithDropdown = function ( textInputWidget, dropdownInputWidget, limit ) {
114 internalVisibleLimitWithDropdown( 'byteLimit', textInputWidget, dropdownInputWidget, limit );
118 * Add a visible codepoint (character) limit label to a TextInputWidget, assuming that the value
119 * of the DropdownInputWidget will be added as a prefix. MediaWiki formats the comment fields for
120 * many actions that way, e.g. for page deletion.
122 * Loaded from `mediawiki.widgets.visibleLengthLimit` module.
124 * Uses {@link module:jquery.lengthLimit.$.fn.codePointLimit codePointLimit} to enforce the limit.
126 * @param {OO.ui.TextInputWidget} textInputWidget
127 * @param {OO.ui.DropdownInputWidget} dropdownInputWidget
128 * @param {number} [limit] Code point limit, defaults to input's `maxlength` attribute
130 mw.widgets.visibleCodePointLimitWithDropdown = function ( textInputWidget, dropdownInputWidget, limit ) {
131 internalVisibleLimitWithDropdown( 'codePointLimit', textInputWidget, dropdownInputWidget, limit );