3
3
* See COPYING.txt for license details.
4
4
*/
5
5
6
- /* eslint-disable no-undef */
7
- // jscs:disable jsDoc
6
+ /* global grecaptcha */
8
7
define (
9
8
[
10
9
'uiComponent' ,
11
10
'jquery' ,
12
11
'ko' ,
12
+ 'underscore' ,
13
13
'Magento_ReCaptchaFrontendUi/js/registry' ,
14
- 'Magento_ReCaptchaFrontendUi/js/reCaptchaScriptLoader'
15
- ] ,
16
- function ( Component , $ , ko , registry , reCaptchaLoader , undefined ) {
14
+ 'Magento_ReCaptchaFrontendUi/js/reCaptchaScriptLoader' ,
15
+ 'Magento_ReCaptchaFrontendUi/js/nonInlineReCaptchaRenderer'
16
+ ] , function ( Component , $ , ko , _ , registry , reCaptchaLoader , nonInlineReCaptchaRenderer ) {
17
17
'use strict' ;
18
18
19
19
return Component . extend ( {
@@ -22,8 +22,10 @@ define(
22
22
template : 'Magento_ReCaptchaFrontendUi/reCaptcha' ,
23
23
reCaptchaId : 'recaptcha'
24
24
} ,
25
- _isApiRegistered : undefined ,
26
25
26
+ /**
27
+ * @inheritdoc
28
+ */
27
29
initialize : function ( ) {
28
30
this . _super ( ) ;
29
31
this . _loadApi ( ) ;
@@ -75,8 +77,7 @@ define(
75
77
* Initialize reCAPTCHA after first rendering
76
78
*/
77
79
initCaptcha : function ( ) {
78
- var me = this ,
79
- $parentForm ,
80
+ var $parentForm ,
80
81
$wrapper ,
81
82
$reCaptcha ,
82
83
widgetId ,
@@ -102,21 +103,24 @@ define(
102
103
$reCaptcha . attr ( 'id' , this . getReCaptchaId ( ) ) ;
103
104
104
105
$parentForm = $wrapper . parents ( 'form' ) ;
105
- me = this ;
106
106
107
107
parameters = _ . extend (
108
108
{
109
109
'callback' : function ( token ) { // jscs:ignore jsDoc
110
- me . reCaptchaCallback ( token ) ;
111
- me . validateReCaptcha ( true ) ;
112
- } ,
110
+ this . reCaptchaCallback ( token ) ;
111
+ this . validateReCaptcha ( true ) ;
112
+ } . bind ( this ) ,
113
113
'expired-callback' : function ( ) {
114
- me . validateReCaptcha ( false ) ;
115
- }
114
+ this . validateReCaptcha ( false ) ;
115
+ } . bind ( this )
116
116
} ,
117
117
this . settings . rendering
118
118
) ;
119
119
120
+ if ( parameters . size === 'invisible' && parameters . badge !== 'inline' ) {
121
+ nonInlineReCaptchaRenderer . add ( $reCaptcha , parameters ) ;
122
+ }
123
+
120
124
// eslint-disable-next-line no-undef
121
125
widgetId = grecaptcha . render ( this . getReCaptchaId ( ) , parameters ) ;
122
126
this . initParentForm ( $parentForm , widgetId ) ;
@@ -134,18 +138,17 @@ define(
134
138
* @param {String } widgetId
135
139
*/
136
140
initParentForm : function ( parentForm , widgetId ) {
137
- var me = this ,
138
- listeners ;
141
+ var listeners ;
139
142
140
143
if ( this . getIsInvisibleRecaptcha ( ) && parentForm . length > 0 ) {
141
144
parentForm . submit ( function ( event ) {
142
- if ( ! me . tokenField . value ) {
145
+ if ( ! this . tokenField . value ) {
143
146
// eslint-disable-next-line no-undef
144
147
grecaptcha . execute ( widgetId ) ;
145
148
event . preventDefault ( event ) ;
146
149
event . stopImmediatePropagation ( ) ;
147
150
}
148
- } ) ;
151
+ } . bind ( this ) ) ;
149
152
150
153
// Move our (last) handler topmost. We need this to avoid submit bindings with ko.
151
154
listeners = $ . _data ( parentForm [ 0 ] , 'events' ) . submit ;
@@ -160,6 +163,11 @@ define(
160
163
}
161
164
} ,
162
165
166
+ /**
167
+ * Validates reCAPTCHA
168
+ * @param {* } state
169
+ * @returns {jQuery }
170
+ */
163
171
validateReCaptcha : function ( state ) {
164
172
if ( ! this . getIsInvisibleRecaptcha ( ) ) {
165
173
return $ ( document ) . find ( 'input[type=checkbox].required-captcha' ) . prop ( 'checked' , state ) ;
@@ -170,14 +178,12 @@ define(
170
178
* Render reCAPTCHA
171
179
*/
172
180
renderReCaptcha : function ( ) {
173
- var me = this ;
174
-
175
181
if ( window . grecaptcha && window . grecaptcha . render ) { // Check if reCAPTCHA is already loaded
176
- me . initCaptcha ( ) ;
182
+ this . initCaptcha ( ) ;
177
183
} else { // Wait for reCAPTCHA to be loaded
178
184
$ ( window ) . on ( 'recaptchaapiready' , function ( ) {
179
- me . initCaptcha ( ) ;
180
- } ) ;
185
+ this . initCaptcha ( ) ;
186
+ } . bind ( this ) ) ;
181
187
}
182
188
} ,
183
189
@@ -189,5 +195,4 @@ define(
189
195
return this . reCaptchaId ;
190
196
}
191
197
} ) ;
192
- }
193
- ) ;
198
+ } ) ;
0 commit comments