Merge "Avoid locking aggregated SELECT in Category::refresh"
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets.visibleLengthLimit / mediawiki.widgets.visibleLengthLimit.js
1 ( function ( mw ) {
2
3 var byteLength = require( 'mediawiki.String' ).byteLength,
4 codePointLength = require( 'mediawiki.String' ).codePointLength;
5
6 /**
7 * @class mw.widgets
8 */
9
10 /**
11 * Add a visible byte limit label to a TextInputWidget.
12 *
13 * Uses jQuery#byteLimit to enforce the limit.
14 *
15 * @param {OO.ui.TextInputWidget} textInputWidget Text input widget
16 * @param {number} [limit] Byte limit, defaults to $input's maxlength
17 */
18 mw.widgets.visibleByteLimit = function ( textInputWidget, limit ) {
19 limit = limit || +textInputWidget.$input.attr( 'maxlength' );
20
21 function updateCount() {
22 var remaining = limit - byteLength( textInputWidget.getValue() );
23 remaining = mw.language.convertNumber( remaining );
24 textInputWidget.setLabel( remaining );
25 }
26 textInputWidget.on( 'change', updateCount );
27 // Initialise value
28 updateCount();
29
30 // Actually enforce limit
31 textInputWidget.$input.byteLimit( limit );
32 };
33
34 /**
35 * Add a visible codepoint (character) limit label to a TextInputWidget.
36 *
37 * Uses jQuery#codePointLimit to enforce the limit.
38 *
39 * @param {OO.ui.TextInputWidget} textInputWidget Text input widget
40 * @param {number} [limit] Byte limit, defaults to $input's maxlength
41 */
42 mw.widgets.visibleCodePointLimit = function ( textInputWidget, limit ) {
43 limit = limit || +textInputWidget.$input.attr( 'maxlength' );
44
45 function updateCount() {
46 var remaining = limit - codePointLength( textInputWidget.getValue() );
47 remaining = mw.language.convertNumber( remaining );
48 textInputWidget.setLabel( remaining );
49 }
50 textInputWidget.on( 'change', updateCount );
51 // Initialise value
52 updateCount();
53
54 // Actually enforce limit
55 textInputWidget.$input.codePointLimit( limit );
56 };
57
58 }( mediaWiki ) );