Debian Codegraph @

resources/src/mediawiki.special.createaccount/signup.js

raw symbols SCIP
1 /*!
2 * JavaScript for signup form.
3 */
4 const HtmlformChecker = require( './HtmlformChecker.js' );
5
6 // When sending password by email, hide the password input fields.
7 $( () => {
8 // Always required if checked, otherwise it depends, so we use the original
9 const $emailLabel = $( 'label[for="wpEmail"] .cdx-label__label__text' ),
10 originalText = $emailLabel.text(),
11 requiredText = mw.msg( 'createacct-emailrequired' ),
12 $createByMailCheckbox = $( '#wpCreateaccountMail' ),
13 $beforePwds = $( '.mw-row-password' ).first().prev();
14 let $pwds;
15
16 function updateForCheckbox() {
17 const checked = $createByMailCheckbox.prop( 'checked' );
18 if ( checked ) {
19 $pwds = $( '.mw-row-password' ).detach();
20 // TODO when this uses the optional flag, show/hide that instead of changing the text
21 $emailLabel.text( requiredText );
22 } else {
23 if ( $pwds ) {
24 $beforePwds.after( $pwds );
25 $pwds = null;
26 }
27 $emailLabel.text( originalText );
28 }
29 }
30
31 $createByMailCheckbox.on( 'change', updateForCheckbox );
32 updateForCheckbox();
33 } );
34
35 // Check if the username is invalid or already taken; show username normalisation warning
36 mw.hook( 'htmlform.enhance' ).add( ( $root ) => {
37 const $usernameInput = $root.find( '#wpName2' ),
38 $passwordInput = $root.find( '#wpPassword2' ),
39 $emailInput = $root.find( '#wpEmail' ),
40 $realNameInput = $root.find( '#wpRealName' ),
41 api = new mw.Api();
42
43 function checkUsername( username ) {
44 // We could just use .then() if we didn't have to pass on .abort()…
45
46 // Leading/trailing/multiple whitespace characters are always stripped in usernames,
47 // this should not require a warning. We do warn about underscores.
48 username = username.replace( / +/g, ' ' ).trim();
49
50 const d = $.Deferred();
51 const apiPromise = api.get( {
52 action: 'query',
53 list: 'users',
54 ususers: username,
55 usprop: 'cancreate',
56 formatversion: 2,
57 errorformat: 'html',
58 errorsuselocal: true,
59 uselang: mw.config.get( 'wgUserLanguage' )
60 } )
61 .done( ( resp ) => {
62 const userinfo = resp.query.users[ 0 ];
63
64 if ( resp.query.users.length !== 1 || userinfo.invalid ) {
65 d.resolve( { valid: false, messages: [ mw.message( 'noname' ).parseDom() ] } );
66 } else if ( userinfo.userid !== undefined ) {
67 d.resolve( { valid: false, messages: [ mw.message( 'userexists' ).parseDom() ] } );
68 } else if ( !userinfo.cancreate ) {
69 d.resolve( {
70 valid: false,
71 messages: userinfo.cancreateerror ? userinfo.cancreateerror.map( ( m ) => m.html ) : []
72 } );
73 } else if ( userinfo.name !== username ) {
74 d.resolve( { valid: true, messages: [
75 mw.message( 'createacct-normalization', username, userinfo.name ).parseDom()
76 ] } );
77 } else {
78 d.resolve( { valid: true, messages: [] } );
79 }
80 } )
81 .fail( d.reject );
82
83 return d.promise( { abort: apiPromise.abort } );
84 }
85
86 function checkPassword() {
87 // We could just use .then() if we didn't have to pass on .abort()…
88 const d = $.Deferred();
89
90 if ( $usernameInput.val().trim() === '' ) {
91 d.resolve( { valid: true, messages: [] } );
92 return d.promise();
93 }
94
95 const apiPromise = api.post( {
96 action: 'validatepassword',
97 user: $usernameInput.val(),
98 password: $passwordInput.val(),
99 email: $emailInput.val() || '',
100 realname: $realNameInput.val() || '',
101 formatversion: 2,
102 errorformat: 'html',
103 errorsuselocal: true,
104 uselang: mw.config.get( 'wgUserLanguage' )
105 } )
106 .done( ( resp ) => {
107 const pwinfo = resp.validatepassword || {};
108
109 d.resolve( {
110 valid: pwinfo.validity === 'Good',
111 messages: pwinfo.validitymessages ? pwinfo.validitymessages.map( ( m ) => m.html ) : []
112 } );
113 } )
114 .fail( d.reject );
115
116 return d.promise( { abort: apiPromise.abort } );
117 }
118
119 const usernameChecker = new HtmlformChecker( $usernameInput, checkUsername );
120 usernameChecker.attach();
121
122 const passwordChecker = new HtmlformChecker( $passwordInput, checkPassword );
123 passwordChecker.attach( $usernameInput.add( $emailInput ).add( $realNameInput ) );
124 } );