Skip to content

Commit 4ff78d9

Browse files
#224: Recaptcha badge is not shown on checkout login form if 'Invisible Badge Position' is 'Bottom Left/Right'
1 parent 674b8db commit 4ff78d9

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

ReCaptchaFrontendUi/view/frontend/web/js/nonInlineReCaptchaRenderer.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,39 @@ define([
1414
rendererReCaptcha = null;
1515

1616
return {
17+
/**
18+
* Add reCaptcha entity to checklist.
19+
*
20+
* @param {jQuery} reCaptchaEntity
21+
* @param {Object} parameters
22+
*/
1723
add: function (reCaptchaEntity, parameters) {
18-
if (parameters.size === 'invisible' && parameters.badge !== 'inline') {
19-
if (!initialized) {
20-
this._init();
21-
grecaptcha.render(rendererRecaptchaId, parameters);
22-
setInterval(this._resolveVisibility, 100);
23-
initialized = true;
24-
}
25-
26-
reCaptchaEntities.push(reCaptchaEntity);
24+
if (!initialized) {
25+
this.init();
26+
grecaptcha.render(rendererRecaptchaId, parameters);
27+
setInterval(this.resolveVisibility, 100);
28+
initialized = true;
2729
}
30+
31+
reCaptchaEntities.push(reCaptchaEntity);
2832
},
2933

30-
_resolveVisibility: function () {
34+
/**
35+
* Show additional reCaptcha instance if any other should be visible, otherwise hide it.
36+
*/
37+
resolveVisibility: function () {
3138
reCaptchaEntities.some(
3239
(entity) => {
3340
return entity.is(":visible")
3441
// 900 is some magic z-index value of modal popups.
35-
&& (entity.closest("[data-role='modal']").length == 0 || entity.zIndex() > 900)
42+
&& (entity.closest("[data-role='modal']").length === 0 || entity.zIndex() > 900)
3643
}) ? rendererReCaptcha.show() : rendererReCaptcha.hide();
3744
},
3845

39-
_init: function () {
46+
/**
47+
* Initialize additional reCaptcha instance.
48+
*/
49+
init: function () {
4050
rendererReCaptcha = $('<div/>', {
4151
'id': rendererRecaptchaId
4252
});

ReCaptchaFrontendUi/view/frontend/web/js/reCaptcha.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ define(
1414
'Magento_ReCaptchaFrontendUi/js/reCaptchaScriptLoader',
1515
'Magento_ReCaptchaFrontendUi/js/nonInlineReCaptchaRenderer',
1616
],
17-
function (Component, $, ko, registry, reCaptchaLoader,nonInlineReCaptchaRenderer, undefined) {
17+
function (Component, $, ko, registry, reCaptchaLoader,nonInlineReCaptchaRenderer) {
1818
'use strict';
1919

2020
return Component.extend({
@@ -23,7 +23,6 @@ define(
2323
template: 'Magento_ReCaptchaFrontendUi/reCaptcha',
2424
reCaptchaId: 'recaptcha'
2525
},
26-
_isApiRegistered: undefined,
2726

2827
initialize: function () {
2928
this._super();
@@ -76,8 +75,7 @@ define(
7675
* Initialize reCAPTCHA after first rendering
7776
*/
7877
initCaptcha: function () {
79-
var me = this,
80-
$parentForm,
78+
var $parentForm,
8179
$wrapper,
8280
$reCaptcha,
8381
widgetId,
@@ -103,22 +101,23 @@ define(
103101
$reCaptcha.attr('id', this.getReCaptchaId());
104102

105103
$parentForm = $wrapper.parents('form');
106-
me = this;
107104

108105
parameters = _.extend(
109106
{
110107
'callback': function (token) { // jscs:ignore jsDoc
111-
me.reCaptchaCallback(token);
112-
me.validateReCaptcha(true);
113-
},
108+
this.reCaptchaCallback(token);
109+
this.validateReCaptcha(true);
110+
}.bind(this),
114111
'expired-callback': function () {
115-
me.validateReCaptcha(false);
116-
}
112+
this.validateReCaptcha(false);
113+
}.bind(this)
117114
},
118115
this.settings.rendering
119116
);
120117

121-
nonInlineReCaptchaRenderer.add($reCaptcha, parameters);
118+
if (parameters.size === 'invisible' && parameters.badge !== 'inline') {
119+
nonInlineReCaptchaRenderer.add($reCaptcha, parameters)
120+
}
122121

123122
// eslint-disable-next-line no-undef
124123
widgetId = grecaptcha.render(this.getReCaptchaId(), parameters);
@@ -137,18 +136,17 @@ define(
137136
* @param {String} widgetId
138137
*/
139138
initParentForm: function (parentForm, widgetId) {
140-
var me = this,
141-
listeners;
139+
var listeners;
142140

143141
if (this.getIsInvisibleRecaptcha() && parentForm.length > 0) {
144142
parentForm.submit(function (event) {
145-
if (!me.tokenField.value) {
143+
if (!this.tokenField.value) {
146144
// eslint-disable-next-line no-undef
147145
grecaptcha.execute(widgetId);
148146
event.preventDefault(event);
149147
event.stopImmediatePropagation();
150148
}
151-
});
149+
}.bind(this));
152150

153151
// Move our (last) handler topmost. We need this to avoid submit bindings with ko.
154152
listeners = $._data(parentForm[0], 'events').submit;
@@ -173,14 +171,12 @@ define(
173171
* Render reCAPTCHA
174172
*/
175173
renderReCaptcha: function () {
176-
var me = this;
177-
178174
if (window.grecaptcha && window.grecaptcha.render) { // Check if reCAPTCHA is already loaded
179-
me.initCaptcha();
175+
this.initCaptcha();
180176
} else { // Wait for reCAPTCHA to be loaded
181177
$(window).on('recaptchaapiready', function () {
182-
me.initCaptcha();
183-
});
178+
this.initCaptcha();
179+
}.bind(this));
184180
}
185181
},
186182

0 commit comments

Comments
 (0)