Merge "Avoid exceptions by first checking language code validity"
[lhc/web/wiklou.git] / resources / mediawiki.special / mediawiki.special.createAccount.js
1 /**
2 * JavaScript for Create account form (Special:UserLogin?type=signup).
3 */
4 ( function ( mw, $ ) {
5
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 $( '#wpCreateaccountMail' )
10 .on( 'change', function() {
11 $( '.mw-row-password' ).toggle( !$( this ).attr( 'checked' ) );
12 } )
13 .trigger( 'change' );
14 }
15
16 // Move the FancyCaptcha image into a more attractive container.
17 // This function does need to be run early by ResourceLoader.
18 function adjustFancyCaptcha( $, mw ) {
19 var $content = $( '#mw-content-text' ),
20 $submit = $content.find( '#wpCreateaccount' ),
21 tabIndex,
22 $captchaStuff,
23 $captchaImageContainer,
24 // JavaScript can't yet parse the message createacct-imgcaptcha-help when it
25 // contains a MediaWiki transclusion, so PHP parses it and sends the HTML.
26 helpMsg = mw.config.get( 'wgCreateacctImgcaptchaHelp' ),
27 helpHtml = '';
28
29 /*
30 * CAPTCHA
31 * The CAPTCHA is in a div style="captcha" at the top of the form.
32 * If it's a FancyCaptcha, then we remove it and insert it lower down,
33 * in a customized div with just what we need (e.g. no
34 * fancycaptcha-createaccount message).
35 */
36 if ( !$submit.length) {
37 return;
38 }
39 tabIndex = $submit.prop( 'tabindex' ) - 1;
40 $captchaStuff = $content.find ( '.captcha' );
41
42 if ( $captchaStuff.length ) {
43
44 // The FancyCaptcha has this class in the ConfirmEdit extension
45 // after 2013-04-18.
46 $captchaImageContainer = $captchaStuff.find( '.fancycaptcha-image-container' );
47 if ( $captchaImageContainer.length !== 1 ) {
48 return;
49 }
50
51 $captchaStuff.remove();
52
53 if ( helpMsg) {
54 helpHtml = '<small class="mw-createacct-captcha-assisted">' + helpMsg + '</small>';
55 }
56
57 // Insert another div before the submit button that will include the
58 // repositioned FancyCaptcha div, an input field, and possible help.
59 $submit.closest( 'div' )
60 .before( [
61 '<div>',
62 '<label for="wpCaptchaWord">' + mw.message( 'createacct-captcha' ).escaped() + '</label>',
63 '<div class="mw-createacct-captcha-container">',
64 '<div class="mw-createacct-captcha-and-reload" />',
65 '<input id="wpCaptchaWord" name="wpCaptchaWord" type="text" placeholder="' +
66 mw.message( 'createacct-imgcaptcha-ph' ).escaped() +
67 '" tabindex="' + tabIndex + '" autocapitalize="off" autocorrect="off">',
68 helpHtml,
69 '</div>',
70 '</div>'
71 ].join( '' )
72 );
73
74 // Stick the FancyCaptcha container inside our bordered and framed parents.
75 $captchaImageContainer
76 .prependTo( $content.find( '.mw-createacct-captcha-and-reload' ) );
77
78 // Find the input field, add the text (if any) of the existing CAPTCHA
79 // field (although usually it's blanked out on every redisplay),
80 // and after it move over the hidden field that tells the CAPTCHA
81 // what to do.
82 $content.find( '#wpCaptchaWord' )
83 .val( $captchaStuff.find( '#wpCaptchaWord' ).val() )
84 .after( $captchaStuff.find( '#wpCaptchaId' ) );
85 }
86 }
87
88 $( document ).ready( function( $ ) {
89 adjustFancyCaptcha( $, mw);
90 hidePasswordOnEmail( $ );
91 } );
92
93 }( mediaWiki, jQuery ) );