resources: Strip '$' and 'mw' from file closures
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets.visibleLengthLimit / mediawiki.widgets.visibleLengthLimit.js
1 ( function () {
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 if ( remaining > 99 ) {
24 remaining = '';
25 } else {
26 remaining = mw.language.convertNumber( remaining );
27 }
28 textInputWidget.setLabel( remaining );
29 }
30 textInputWidget.on( 'change', updateCount );
31 // Initialise value
32 updateCount();
33
34 // Actually enforce limit
35 textInputWidget.$input.byteLimit( limit );
36 };
37
38 /**
39 * Add a visible codepoint (character) limit label to a TextInputWidget.
40 *
41 * Uses jQuery#codePointLimit to enforce the limit.
42 *
43 * @param {OO.ui.TextInputWidget} textInputWidget Text input widget
44 * @param {number} [limit] Byte limit, defaults to $input's maxlength
45 */
46 mw.widgets.visibleCodePointLimit = function ( textInputWidget, limit ) {
47 limit = limit || +textInputWidget.$input.attr( 'maxlength' );
48
49 function updateCount() {
50 var remaining = limit - codePointLength( textInputWidget.getValue() );
51 if ( remaining > 99 ) {
52 remaining = '';
53 } else {
54 remaining = mw.language.convertNumber( remaining );
55 }
56 textInputWidget.setLabel( remaining );
57 }
58 textInputWidget.on( 'change', updateCount );
59 // Initialise value
60 updateCount();
61
62 // Actually enforce limit
63 textInputWidget.$input.codePointLimit( limit );
64 };
65
66 }() );