Merge "Title: Refactor JS/CSS page handling to be more sane"
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets.visibleByteLimit / mediawiki.widgets.visibleByteLimit.js
1 ( function ( mw ) {
2
3 var byteLength = require( 'mediawiki.String' ).byteLength;
4
5 /**
6 * @class mw.widgets
7 */
8
9 /**
10 * Add a visible byte limit label to a TextInputWidget.
11 *
12 * Uses jQuery#byteLimit to enforce the limit.
13 *
14 * @param {OO.ui.TextInputWidget} textInputWidget Text input widget
15 * @param {number} [limit] Byte limit, defaults to $input's maxlength
16 */
17 mw.widgets.visibleByteLimit = function ( textInputWidget, limit ) {
18 limit = limit || +textInputWidget.$input.attr( 'maxlength' );
19
20 function updateCount() {
21 textInputWidget.setLabel( ( limit - byteLength( textInputWidget.getValue() ) ).toString() );
22 }
23 textInputWidget.on( 'change', updateCount );
24 // Initialise value
25 updateCount();
26
27 // Actually enforce limit
28 textInputWidget.$input.byteLimit( limit );
29 };
30
31 }( mediaWiki ) );