Merge "Document the wikipage.content hook"
[mediawiki.git] / resources / mediawiki.special / mediawiki.special.createAccount.js
blob609b2dd3d9e2cb8668e423bcb3d6de8148459004
1 /**
2  * JavaScript for Create account form (Special:UserLogin?type=signup).
3  */
4 ( function ( mw, $ ) {
5         // When sending password by email, hide the password input fields.
6         // This function doesn't need to be loaded early by ResourceLoader, but is tiny.
7         function hidePasswordOnEmail() {
8                 // Always required if checked, otherwise it depends, so we use the original
9                 var $emailLabel = $( 'label[for="wpEmail"]' ),
10                         originalText = $emailLabel.text(),
11                         requiredText = mw.message( 'createacct-emailrequired' ).text(),
12                         $createByMailCheckbox = $( '#wpCreateaccountMail' ),
13                         $beforePwds = $( '.mw-row-password:first' ).prev(),
14                         $pwds;
16                 function updateForCheckbox() {
17                         var checked = $createByMailCheckbox.prop( 'checked' );
18                         if ( checked ) {
19                                 $pwds = $( '.mw-row-password' ).detach();
20                                 $emailLabel.text( requiredText );
21                         } else {
22                                 if ( $pwds ) {
23                                         $beforePwds.after( $pwds );
24                                         $pwds = null;
25                                 }
26                                 $emailLabel.text( originalText );
27                         }
28                 }
30                 $createByMailCheckbox.on( 'change', updateForCheckbox );
31                 updateForCheckbox();
32         }
34         // Move the FancyCaptcha image into a more attractive container.
35         // This function does need to be run early by ResourceLoader.
36         function adjustFancyCaptcha( $content, buttonSubmit ) {
37                 var $submit = $content.find( buttonSubmit ),
38                         tabIndex,
39                         $captchaStuff,
40                         $captchaImageContainer,
41                         // JavaScript can't yet parse the message createacct-imgcaptcha-help when it
42                         // contains a MediaWiki transclusion, so PHP parses it and sends the HTML.
43                         helpMsg = mw.config.get( 'wgCreateacctImgcaptchaHelp' ),
44                         helpHtml = '';
46                 /*
47                  * CAPTCHA
48                  * The CAPTCHA is in a div style="captcha" at the top of the form.
49                  * If it's a FancyCaptcha, then we remove it and insert it lower down,
50                  * in a customized div with just what we need (e.g. no
51                  * fancycaptcha-createaccount message).
52                  */
53                 if ( !$submit.length) {
54                         return;
55                 }
56                 tabIndex = $submit.prop( 'tabindex' ) - 1;
57                 $captchaStuff = $content.find ( '.captcha' );
59                 if ( $captchaStuff.length ) {
61                         // The FancyCaptcha has this class in the ConfirmEdit extension
62                         // after 2013-04-18.
63                         $captchaImageContainer = $captchaStuff.find( '.fancycaptcha-image-container' );
64                         if ( $captchaImageContainer.length !== 1 ) {
65                                 return;
66                         }
68                         $captchaStuff.remove();
70                         if ( helpMsg) {
71                                 helpHtml = '<small class="mw-createacct-captcha-assisted">' + helpMsg + '</small>';
72                         }
74                         // Insert another div before the submit button that will include the
75                         // repositioned FancyCaptcha div, an input field, and possible help.
76                         $submit.closest( 'div' )
77                                 .before( [
78                         '<div>',
79                                 '<label for="wpCaptchaWord">' + mw.message( 'createacct-captcha' ).escaped() + '</label>',
80                                 '<div class="mw-createacct-captcha-container">',
81                                         '<div class="mw-createacct-captcha-and-reload" />',
82                                         '<input id="wpCaptchaWord" name="wpCaptchaWord" type="text" placeholder="' +
83                                                 mw.message( 'createacct-imgcaptcha-ph' ).escaped() +
84                                                 '" tabindex="' + tabIndex + '" autocapitalize="off" autocorrect="off">',
85                                                 helpHtml,
86                                 '</div>',
87                         '</div>'
88                                         ].join( '' )
89                                 );
91                         // Stick the FancyCaptcha container inside our bordered and framed parents.
92                         $captchaImageContainer
93                                 .prependTo( $content.find( '.mw-createacct-captcha-and-reload' ) );
95                         // Find the input field, add the text (if any) of the existing CAPTCHA
96                         // field (although usually it's blanked out on every redisplay),
97                         // and after it move over the hidden field that tells the CAPTCHA
98                         // what to do.
99                         $content.find( '#wpCaptchaWord' )
100                                 .val( $captchaStuff.find( '#wpCaptchaWord' ).val() )
101                                 .after( $captchaStuff.find( '#wpCaptchaId' ) );
102                 }
103         }
105         $( function () {
106                 // Checks if the current page is Special:UserLogin
107                 var isLogin = false,
108                         $content = $( '#mw-content-text' ),
109                         buttonSubmit = '#wpCreateaccount';
111                 if ( $content.find( buttonSubmit ).length === 0 ) {
112                         buttonSubmit = '#wpLoginAttempt';
113                         isLogin = true;
114                 }
116                 adjustFancyCaptcha( $content, buttonSubmit );
118                 if ( !isLogin ) {
119                         hidePasswordOnEmail();
120                 }
122         } );
124 }( mediaWiki, jQuery ) );