Completed
Push — master ( 2e96ee...51b9e4 )
by Rain
01:37
created

plugins/recaptcha/js/recaptcha.js   B

Complexity

Total Complexity 36
Complexity/F 4.5

Size

Lines of Code 95
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
wmc 36
nc 3840
mnd 5
bc 21
fnc 8
dl 0
loc 95
rs 8.8
bpm 2.625
cpm 4.5
noi 1
c 0
b 0
f 0
1
(function ($, window) {
2
3
	$(function () {
4
5
		var
6
			nId = null,
7
			bStarted = false
8
		;
9
10
		function ShowRecaptcha()
11
		{
12
			if (window.grecaptcha)
13
			{
14
				if (null === nId)
15
				{
16
					var
17
						oEl = null,
18
						oLink = $('.plugin-mark-Login-BottomControlGroup')
19
					;
20
21
					if (oLink && oLink[0])
22
					{
23
						oEl = $('<div class="controls"></div>');
24
25
						$(oLink[0]).after(oEl);
26
27
						nId = window.grecaptcha.render(oEl[0], {
28
							'sitekey': window.rl.pluginSettingsGet('recaptcha', 'public_key'),
29
							'theme': window.rl.pluginSettingsGet('recaptcha', 'theme')
30
						});
31
					}
32
				}
33
			}
34
		}
35
36
		window.__globalShowRecaptcha = ShowRecaptcha;
37
38
		function StartRecaptcha()
39
		{
40
			if (!window.grecaptcha && window.rl)
41
			{
42
				$.getScript('https://www.google.com/recaptcha/api.js?onload=__globalShowRecaptcha&render=explicit&hl=' + window.rl.settingsGet('Language'));
43
			}
44
			else
45
			{
46
				ShowRecaptcha();
47
			}
48
		}
49
50
		if (window.rl)
51
		{
52
			window.rl.addHook('user-login-submit', function (fSubmitResult) {
53
				if (null !== nId && !window.grecaptcha.getResponse(nId))
54
				{
55
					fSubmitResult(105);
56
				}
57
			});
58
59
			window.rl.addHook('view-model-on-show', function (sName, oViewModel) {
60
				if (!bStarted && oViewModel &&
61
					('View:RainLoop:Login' === sName || 'View/App/Login' === sName || 'LoginViewModel' === sName || 'LoginAppView' === sName) &&
62
					window.rl.pluginSettingsGet('recaptcha', 'show_captcha_on_login'))
63
				{
64
					bStarted = true;
65
					StartRecaptcha();
66
				}
67
			});
68
69
			window.rl.addHook('ajax-default-request', function (sAction, oParameters) {
70
				if ('Login' === sAction && oParameters && null !== nId && window.grecaptcha)
71
				{
72
					oParameters['RecaptchaResponse'] = window.grecaptcha.getResponse(nId);
73
				}
74
			});
75
76
			window.rl.addHook('ajax-default-response', function (sAction, oData, sType) {
77
				if ('Login' === sAction)
78
				{
79
					if (!oData || 'success' !== sType || !oData['Result'])
80
					{
81
						if (null !== nId && window.grecaptcha)
82
						{
83
							window.grecaptcha.reset(nId);
84
						}
85
						else if (oData && oData['Captcha'])
86
						{
87
							StartRecaptcha();
88
						}
89
					}
90
				}
91
			});
92
		}
93
	});
94
95
}($, window));