Warn on account creation when username is adjusted
[lhc/web/wiklou.git] / resources / mediawiki.special / mediawiki.special.userlogin.signup.js
1 /**
2 * JavaScript for signup form.
3 */
4 ( function ( mw, $ ) {
5 // When sending password by email, hide the password input fields.
6 $( function () {
7 // Always required if checked, otherwise it depends, so we use the original
8 var $emailLabel = $( 'label[for="wpEmail"]' ),
9 originalText = $emailLabel.text(),
10 requiredText = mw.message( 'createacct-emailrequired' ).text(),
11 $createByMailCheckbox = $( '#wpCreateaccountMail' ),
12 $beforePwds = $( '.mw-row-password:first' ).prev(),
13 $pwds;
14
15 function updateForCheckbox() {
16 var checked = $createByMailCheckbox.prop( 'checked' );
17 if ( checked ) {
18 $pwds = $( '.mw-row-password' ).detach();
19 $emailLabel.text( requiredText );
20 } else {
21 if ( $pwds ) {
22 $beforePwds.after( $pwds );
23 $pwds = null;
24 }
25 $emailLabel.text( originalText );
26 }
27 }
28
29 $createByMailCheckbox.on( 'change', updateForCheckbox );
30 updateForCheckbox();
31 } );
32
33 // Show username normalisation warning
34 $( function () {
35 var
36 // All of these are apparently required to be sure we detect any changes.
37 events = 'keyup keydown change mouseup cut paste focus blur',
38 $input = $( '#wpName2' ),
39 $warningContainer = $( '#mw-createacct-status-area' ),
40 api = new mw.Api(),
41 currentRequest,
42 tweakedUsername;
43
44 // Hide any warnings / errors.
45 function cleanup() {
46 $warningContainer.slideUp( function () {
47 $warningContainer
48 .removeAttr( 'class' )
49 .empty();
50 } );
51 }
52
53 function updateUsernameStatus() {
54 var
55 // Leading/trailing/multiple whitespace characters are never accepted in usernames and users
56 // know that, don't warn if someone accidentally types it. We do warn about underscores.
57 username = $.trim( $input.val().replace( /\s+/g, ' ' ) ),
58 currentRequestInternal;
59
60 // Abort any pending requests.
61 if ( currentRequest ) {
62 currentRequest.abort();
63 }
64
65 if ( username === '' ) {
66 cleanup();
67 return;
68 }
69
70 currentRequest = currentRequestInternal = api.get( {
71 action: 'query',
72 list: 'users',
73 ususers: username // '|' in usernames is handled below
74 } ).done( function ( resp ) {
75 var userinfo, state;
76
77 // Another request was fired in the meantime, the result we got here is no longer current.
78 // This shouldn't happen as we abort pending requests, but you never know.
79 if ( currentRequest !== currentRequestInternal ) {
80 return;
81 }
82
83 tweakedUsername = undefined;
84
85 userinfo = resp.query.users[0];
86
87 if ( resp.query.users.length !== 1 ) {
88 // Happens if the user types '|' into the field
89 state = 'invalid';
90 } else if ( userinfo.invalid !== undefined ) {
91 state = 'invalid';
92 } else if ( userinfo.userid !== undefined ) {
93 state = 'taken';
94 } else if ( username !== userinfo.name ) {
95 state = 'tweaked';
96 } else {
97 state = 'ok';
98 }
99
100 if ( state === 'ok' ) {
101 cleanup();
102 } else if ( state === 'tweaked' ) {
103 $warningContainer
104 .attr( 'class', 'warningbox' )
105 .text( mw.message( 'createacct-normalization', username, userinfo.name ).text() )
106 .slideDown();
107
108 tweakedUsername = userinfo.name;
109 } else {
110 $warningContainer
111 .attr( 'class', 'errorbox' )
112 .empty()
113 .append(
114 $( '<strong>' ).text( mw.message( 'createacct-error' ).text() ),
115 $( '<br>' ) // Ugh
116 );
117
118 if ( state === 'invalid' ) {
119 $warningContainer
120 .attr( 'class', 'errorbox' )
121 .append( document.createTextNode( mw.message( 'noname' ).text() ) )
122 .slideDown();
123 } else if ( state === 'taken' ) {
124 $warningContainer
125 .attr( 'class', 'errorbox' )
126 .append( document.createTextNode( mw.message( 'userexists' ).text() ) )
127 .slideDown();
128 }
129
130 $warningContainer.slideDown();
131 }
132 } ).fail( function () {
133 cleanup();
134 } );
135 }
136
137 $input.on( events, $.debounce( 250, updateUsernameStatus ) );
138
139 $input.closest( 'form' ).on( 'submit', function () {
140 // If the username has to be adjusted before it's accepted, server-side check will force the
141 // form to be resubmitted. Let's prevent that.
142 if ( tweakedUsername !== undefined ) {
143 $input.val( tweakedUsername );
144 }
145 } );
146 } );
147 }( mediaWiki, jQuery ) );