summaryrefslogtreecommitdiff
path: root/www/wiki/resources/src/mediawiki.special/mediawiki.special.changecredentials.js
blob: ad8a4f4f06c9800c47928f8ca8c2c2d0b243a05e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*!
 * JavaScript for change credentials form.
 */
( function ( mw, $, OO ) {
	mw.hook( 'htmlform.enhance' ).add( function ( $root ) {
		var api = new mw.Api();

		$root.find( '.mw-changecredentials-validate-password.oo-ui-fieldLayout' ).each( function () {
			var currentApiPromise,
				self = OO.ui.FieldLayout.static.infuse( $( this ) );

			self.getField().setValidation( function ( password ) {
				var d;

				if ( currentApiPromise ) {
					currentApiPromise.abort();
					currentApiPromise = undefined;
				}

				password = password.trim();

				if ( password === '' ) {
					self.setErrors( [] );
					return true;
				}

				d = $.Deferred();
				currentApiPromise = api.post( {
					action: 'validatepassword',
					password: password,
					formatversion: 2,
					errorformat: 'html',
					errorsuselocal: true,
					uselang: mw.config.get( 'wgUserLanguage' )
				} ).done( function ( resp ) {
					var pwinfo = resp.validatepassword,
						good = pwinfo.validity === 'Good',
						errors = [];

					currentApiPromise = undefined;

					if ( !good ) {
						pwinfo.validitymessages.map( function ( m ) {
							errors.push( new OO.ui.HtmlSnippet( m.html ) );
						} );
					}
					self.setErrors( errors );
					d.resolve( good );
				} ).fail( d.reject );

				return d.promise( { abort: currentApiPromise.abort } );
			} );
		} );
	} );
}( mediaWiki, jQuery, OO ) );