33 * See COPYING.txt for license details.
44 */
55
6- /* eslint-disable no-undef */
7- // jscs:disable jsDoc
6+ /* global grecaptcha */
87define (
98 [
109 'uiComponent' ,
1110 'jquery' ,
1211 'ko' ,
12+ 'underscore' ,
1313 '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 ) {
1717 'use strict' ;
1818
1919 return Component . extend ( {
@@ -22,8 +22,10 @@ define(
2222 template : 'Magento_ReCaptchaFrontendUi/reCaptcha' ,
2323 reCaptchaId : 'recaptcha'
2424 } ,
25- _isApiRegistered : undefined ,
2625
26+ /**
27+ * @inheritdoc
28+ */
2729 initialize : function ( ) {
2830 this . _super ( ) ;
2931 this . _loadApi ( ) ;
@@ -75,8 +77,7 @@ define(
7577 * Initialize reCAPTCHA after first rendering
7678 */
7779 initCaptcha : function ( ) {
78- var me = this ,
79- $parentForm ,
80+ var $parentForm ,
8081 $wrapper ,
8182 $reCaptcha ,
8283 widgetId ,
@@ -102,21 +103,24 @@ define(
102103 $reCaptcha . attr ( 'id' , this . getReCaptchaId ( ) ) ;
103104
104105 $parentForm = $wrapper . parents ( 'form' ) ;
105- me = this ;
106106
107107 parameters = _ . extend (
108108 {
109109 '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 ) ,
113113 'expired-callback' : function ( ) {
114- me . validateReCaptcha ( false ) ;
115- }
114+ this . validateReCaptcha ( false ) ;
115+ } . bind ( this )
116116 } ,
117117 this . settings . rendering
118118 ) ;
119119
120+ if ( parameters . size === 'invisible' && parameters . badge !== 'inline' ) {
121+ nonInlineReCaptchaRenderer . add ( $reCaptcha , parameters ) ;
122+ }
123+
120124 // eslint-disable-next-line no-undef
121125 widgetId = grecaptcha . render ( this . getReCaptchaId ( ) , parameters ) ;
122126 this . initParentForm ( $parentForm , widgetId ) ;
@@ -134,18 +138,17 @@ define(
134138 * @param {String } widgetId
135139 */
136140 initParentForm : function ( parentForm , widgetId ) {
137- var me = this ,
138- listeners ;
141+ var listeners ;
139142
140143 if ( this . getIsInvisibleRecaptcha ( ) && parentForm . length > 0 ) {
141144 parentForm . submit ( function ( event ) {
142- if ( ! me . tokenField . value ) {
145+ if ( ! this . tokenField . value ) {
143146 // eslint-disable-next-line no-undef
144147 grecaptcha . execute ( widgetId ) ;
145148 event . preventDefault ( event ) ;
146149 event . stopImmediatePropagation ( ) ;
147150 }
148- } ) ;
151+ } . bind ( this ) ) ;
149152
150153 // Move our (last) handler topmost. We need this to avoid submit bindings with ko.
151154 listeners = $ . _data ( parentForm [ 0 ] , 'events' ) . submit ;
@@ -160,6 +163,11 @@ define(
160163 }
161164 } ,
162165
166+ /**
167+ * Validates reCAPTCHA
168+ * @param {* } state
169+ * @returns {jQuery }
170+ */
163171 validateReCaptcha : function ( state ) {
164172 if ( ! this . getIsInvisibleRecaptcha ( ) ) {
165173 return $ ( document ) . find ( 'input[type=checkbox].required-captcha' ) . prop ( 'checked' , state ) ;
@@ -170,14 +178,12 @@ define(
170178 * Render reCAPTCHA
171179 */
172180 renderReCaptcha : function ( ) {
173- var me = this ;
174-
175181 if ( window . grecaptcha && window . grecaptcha . render ) { // Check if reCAPTCHA is already loaded
176- me . initCaptcha ( ) ;
182+ this . initCaptcha ( ) ;
177183 } else { // Wait for reCAPTCHA to be loaded
178184 $ ( window ) . on ( 'recaptchaapiready' , function ( ) {
179- me . initCaptcha ( ) ;
180- } ) ;
185+ this . initCaptcha ( ) ;
186+ } . bind ( this ) ) ;
181187 }
182188 } ,
183189
@@ -189,5 +195,4 @@ define(
189195 return this . reCaptchaId ;
190196 }
191197 } ) ;
192- }
193- ) ;
198+ } ) ;
0 commit comments