summaryrefslogtreecommitdiff
path: root/resources/mediawiki.special/mediawiki.special.createAccount.js
blob: 5cbb1ee00185c90aaa8d1cd9b4b9c32b4758aa5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
 * JavaScript for Create account form (Special:UserLogin?type=signup).
 */
( function ( mw, $ ) {

	// When sending password by email, hide the password input fields.
	// This function doesn't need to be loaded early by ResourceLoader, but is tiny.
	function hidePasswordOnEmail() {
		// Always required if checked, otherwise it depends, so we use the original
		var $emailLabel = $( 'label[for="wpEmail"]' ),
			originalText = $emailLabel.text(),
			requiredText = mw.message( 'createacct-emailrequired' ).text(),
			$createByMailCheckbox = $( '#wpCreateaccountMail' ),
			$beforePwds = $( '.mw-row-password:first' ).prev(),
			$pwds;

		function updateForCheckbox() {
			var checked = $createByMailCheckbox.prop( 'checked' );
			if ( checked ) {
				$pwds = $( '.mw-row-password' ).detach();
				$emailLabel.text( requiredText );
			} else {
				if ( $pwds ) {
					$beforePwds.after( $pwds );
					$pwds = null;
				}
				$emailLabel.text( originalText );
			}
		}

		$createByMailCheckbox.on( 'change', updateForCheckbox );
		updateForCheckbox();
	}

	// Move the FancyCaptcha image into a more attractive container.
	// This function does need to be run early by ResourceLoader.
	function adjustFancyCaptcha() {
		var $content = $( '#mw-content-text' ),
			$submit = $content.find( '#wpCreateaccount' ),
			tabIndex,
			$captchaStuff,
			$captchaImageContainer,
			// JavaScript can't yet parse the message createacct-imgcaptcha-help when it
			// contains a MediaWiki transclusion, so PHP parses it and sends the HTML.
			helpMsg = mw.config.get( 'wgCreateacctImgcaptchaHelp' ),
			helpHtml = '';

		/*
		 * CAPTCHA
		 * The CAPTCHA is in a div style="captcha" at the top of the form.
		 * If it's a FancyCaptcha, then we remove it and insert it lower down,
		 * in a customized div with just what we need (e.g. no
		 * fancycaptcha-createaccount message).
		 */
		if ( !$submit.length) {
			return;
		}
		tabIndex = $submit.prop( 'tabindex' ) - 1;
		$captchaStuff = $content.find ( '.captcha' );

		if ( $captchaStuff.length ) {

			// The FancyCaptcha has this class in the ConfirmEdit extension
			// after 2013-04-18.
			$captchaImageContainer = $captchaStuff.find( '.fancycaptcha-image-container' );
			if ( $captchaImageContainer.length !== 1 ) {
				return;
			}

			$captchaStuff.remove();

			if ( helpMsg) {
				helpHtml = '<small class="mw-createacct-captcha-assisted">' + helpMsg + '</small>';
			}

			// Insert another div before the submit button that will include the
			// repositioned FancyCaptcha div, an input field, and possible help.
			$submit.closest( 'div' )
				.before( [
			'<div>',
				'<label for="wpCaptchaWord">' + mw.message( 'createacct-captcha' ).escaped() + '</label>',
				'<div class="mw-createacct-captcha-container">',
					'<div class="mw-createacct-captcha-and-reload" />',
					'<input id="wpCaptchaWord" name="wpCaptchaWord" type="text" placeholder="' +
						mw.message( 'createacct-imgcaptcha-ph' ).escaped() +
						'" tabindex="' + tabIndex + '" autocapitalize="off" autocorrect="off">',
						helpHtml,
				'</div>',
			'</div>'
					].join( '' )
				);

			// Stick the FancyCaptcha container inside our bordered and framed parents.
			$captchaImageContainer
				.prependTo( $content.find( '.mw-createacct-captcha-and-reload' ) );

			// Find the input field, add the text (if any) of the existing CAPTCHA
			// field (although usually it's blanked out on every redisplay),
			// and after it move over the hidden field that tells the CAPTCHA
			// what to do.
			$content.find( '#wpCaptchaWord' )
				.val( $captchaStuff.find( '#wpCaptchaWord' ).val() )
				.after( $captchaStuff.find( '#wpCaptchaId' ) );
		}
	}

	$( function () {
		adjustFancyCaptcha();
		hidePasswordOnEmail();
	} );

}( mediaWiki, jQuery ) );