3 var byteLength
= require( 'mediawiki.String' ).byteLength
,
4 codePointLength
= require( 'mediawiki.String' ).codePointLength
;
11 * Add a visible byte limit label to a TextInputWidget.
13 * Uses jQuery#byteLimit to enforce the limit.
15 * @param {OO.ui.TextInputWidget} textInputWidget Text input widget
16 * @param {number} [limit] Byte limit, defaults to $input's maxlength
17 * @param {Function} [filterFunction] Function to call on the string before assessing the length.
19 mw
.widgets
.visibleByteLimit = function ( textInputWidget
, limit
, filterFunction
) {
20 limit
= limit
|| +textInputWidget
.$input
.attr( 'maxlength' );
21 if ( !filterFunction
|| typeof filterFunction
!== 'function' ) {
22 filterFunction
= undefined;
25 function updateCount() {
26 var value
= textInputWidget
.getValue(),
28 if ( filterFunction
) {
29 value
= filterFunction( value
);
31 remaining
= limit
- byteLength( value
);
32 if ( remaining
> 99 ) {
35 remaining
= mw
.language
.convertNumber( remaining
);
37 textInputWidget
.setLabel( remaining
);
39 textInputWidget
.on( 'change', updateCount
);
43 // Actually enforce limit
44 textInputWidget
.$input
.byteLimit( limit
, filterFunction
);
48 * Add a visible codepoint (character) limit label to a TextInputWidget.
50 * Uses jQuery#codePointLimit to enforce the limit.
52 * @param {OO.ui.TextInputWidget} textInputWidget Text input widget
53 * @param {number} [limit] Code point limit, defaults to $input's maxlength
54 * @param {Function} [filterFunction] Function to call on the string before assessing the length.
56 mw
.widgets
.visibleCodePointLimit = function ( textInputWidget
, limit
, filterFunction
) {
57 limit
= limit
|| +textInputWidget
.$input
.attr( 'maxlength' );
58 if ( !filterFunction
|| typeof filterFunction
!== 'function' ) {
59 filterFunction
= undefined;
62 function updateCount() {
63 var value
= textInputWidget
.getValue(),
65 if ( filterFunction
) {
66 value
= filterFunction( value
);
68 remaining
= limit
- codePointLength( value
);
69 if ( remaining
> 99 ) {
72 remaining
= mw
.language
.convertNumber( remaining
);
74 textInputWidget
.setLabel( remaining
);
76 textInputWidget
.on( 'change', updateCount
);
80 // Actually enforce limit
81 textInputWidget
.$input
.codePointLimit( limit
, filterFunction
);