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