Passed
Push — master ( 554ad1...33081a )
by Paul
04:37
created

Recaptcha.render_   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 12
rs 9.4285
1
/** global: GLSR, grecaptcha */
2
;(function() {
3
4
	'use strict';
5
6
	var Recaptcha = function( form ) { // Form object
7
		this.Form = form;
8
		this.addListeners = this.addListeners_;
9
		this.execute = this.execute_;
10
		this.render = this.render_;
11
		this.reset = this.reset_;
12
	};
13
14
	Recaptcha.prototype = {
15
16
		/** @return void */
17
		addListeners_: function() {
18
			var overlayEl = this.getOverlay_();
19
			if( overlayEl === -1 )return;
20
			overlayEl.addEventListener( 'click', this.Form.enableButton );
21
			window.addEventListener( 'keyup', this.onKeyup_.bind( this, overlayEl ));
22
		},
23
24
		/** @return void */
25
		execute_: function() {
26
			var recaptchaId = this.getId_();
27
			if( recaptchaId !== -1 ) {
28
				grecaptcha.execute( recaptchaId );
29
				return;
30
			}
31
			// recaptcha ID not found so pass through an error
32
			this.Form.submitForm_( false );
33
		},
34
35
		/** @return string|int (-1) */
36
		getId_: function() {
37
			return this.search_( function( value, id ) {
38
				if( Object.prototype.toString.call( value ) !== '[object HTMLDivElement]' )return;
39
				if( value.closest( 'form' ) === this.Form.form ) {
40
					return id;
41
				}
42
			});
43
		},
44
45
		/** @return HTMLDivElement|int (-1) */
46
		getOverlay_: function() {
47
			return this.search_( function( value ) {
48
				if( Object.prototype.toString.call( value ) !== '[object Object]' )return;
49
				for( var obj in value) {
50
					if( !value.hasOwnProperty( obj ) || Object.prototype.toString.call( value[obj] ) !== '[object HTMLDivElement]' )continue;
51
					if( value[obj].className === '' ) {
52
						return value[obj].firstChild;
53
					}
54
				}
55
				return false;
56
			});
57
		},
58
59
		/** @return void */
60
		onKeyup_: function( ev ) { // KeyboardEvent
61
			if( ev.keyCode !== 27 )return;
62
			this.Form.enableButton();
63
			this.removeListeners_( ev.target );
64
		},
65
66
		/** @return void */
67
		removeListeners_: function( overlayEl ) { // HTMLDivElement
68
			overlayEl.removeEventListener( 'click', this.Form.enableButton );
69
			window.removeEventListener( 'keyup', this.onKeyup_ );
70
		},
71
72
		/** @return void */
73
		render_: function() {
74
			this.Form.form.onsubmit = null;
75
			var recaptchaEl = this.Form.form.querySelector( '.glsr-recaptcha-holder' );
76
			if( !recaptchaEl )return;
77
			recaptchaEl.innerHTML = '';
78
			var id = grecaptcha.render( recaptchaEl, {
79
				callback: this.submitForm_.bind( this ),
80
				'expired-callback': function() {
81
					grecaptcha.reset( id );
82
				},
83
			}, true );
84
		},
85
86
		/** @return void */
87
		reset_: function() {
88
			var recaptchaId = this.getId_();
89
			if( recaptchaId !== -1 ) {
90
				grecaptcha.reset( recaptchaId );
91
			}
92
		},
93
94
		/** @return mixed|int (-1) */
95
		search_: function( callback ) { // function
96
			var result = -1;
97
			if( window.hasOwnProperty( '___grecaptcha_cfg' )) {
98
				var clients = window.___grecaptcha_cfg.clients;
99
				var i, key;
100
				for( i in clients ) {
101
					for( key in clients[i] ) {
102
						if( !( result = callback( clients[i][key], i ).bind( this )))continue;
103
						return result;
104
					}
105
				}
106
			}
107
			return result;
108
		},
109
110
		/** @return void */
111
		submitForm_: function( token ) { // string
112
			this.Form.form.submitForm( token );
113
		},
114
	};
115
116
	GLSR.Recaptcha = Recaptcha;
117
})();
118