Skip to content

Commit c3c6f99

Browse files
authored
Merge branch '1.0.0-develop' into MQE-2194-1.0.0
2 parents 42d27ea + e03deb4 commit c3c6f99

File tree

5 files changed

+103
-33
lines changed

5 files changed

+103
-33
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
/* global grecaptcha */
7+
define([
8+
'jquery'
9+
], function ($) {
10+
'use strict';
11+
12+
var reCaptchaEntities = [],
13+
initialized = false,
14+
rendererRecaptchaId = 'recaptcha-invisible',
15+
rendererReCaptcha = null;
16+
17+
return {
18+
/**
19+
* Add reCaptcha entity to checklist.
20+
*
21+
* @param {jQuery} reCaptchaEntity
22+
* @param {Object} parameters
23+
*/
24+
add: function (reCaptchaEntity, parameters) {
25+
if (!initialized) {
26+
this.init();
27+
grecaptcha.render(rendererRecaptchaId, parameters);
28+
setInterval(this.resolveVisibility, 100);
29+
initialized = true;
30+
}
31+
32+
reCaptchaEntities.push(reCaptchaEntity);
33+
},
34+
35+
/**
36+
* Show additional reCaptcha instance if any other should be visible, otherwise hide it.
37+
*/
38+
resolveVisibility: function () {
39+
reCaptchaEntities.some(function (entity) {
40+
return entity.is(':visible') &&
41+
// 900 is some magic z-index value of modal popups.
42+
(entity.closest('[data-role=\'modal\']').length === 0 || entity.zIndex() > 900);
43+
}) ? rendererReCaptcha.show() : rendererReCaptcha.hide();
44+
},
45+
46+
/**
47+
* Initialize additional reCaptcha instance.
48+
*/
49+
init: function () {
50+
rendererReCaptcha = $('<div/>', {
51+
'id': rendererRecaptchaId
52+
});
53+
$('body').append(rendererReCaptcha);
54+
}
55+
};
56+
});

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

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
* See COPYING.txt for license details.
44
*/
55

6-
/* eslint-disable no-undef */
7-
// jscs:disable jsDoc
6+
/* global grecaptcha */
87
define(
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+
});

ReCaptchaNewsletter/etc/adminhtml/system.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<group id="type_for">
1313
<field id="newsletter" translate="label comment" type="select" sortOrder="160" showInDefault="1"
1414
showInWebsite="1" showInStore="0" canRestore="1">
15-
<label>Enable Invisible reCAPTCHA in Newsletter Subscription</label>
15+
<label>Enable for Newsletter Subscription</label>
1616
<comment>If enabled, a badge will be displayed in every page.</comment>
1717
<source_model>Magento\ReCaptchaAdminUi\Model\OptionSource\Type</source_model>
1818
</field>

ReCaptchaNewsletter/view/frontend/layout/default.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717
ifconfig="recaptcha_frontend/type_for/newsletter">
1818
<arguments>
1919
<argument name="recaptcha_for" xsi:type="string">newsletter</argument>
20-
<argument name="captcha_ui_config" xsi:type="array">
21-
<item name="invisible" xsi:type="boolean">true</item>
22-
<item name="rendering" xsi:type="array">
23-
<item name="badge" xsi:type="string">bottomright</item>
24-
<item name="size" xsi:type="string">invisible</item>
25-
</item>
26-
</argument>
2720
<argument name="jsLayout" xsi:type="array">
2821
<item name="components" xsi:type="array">
2922
<item name="recaptcha" xsi:type="array">
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
.block.newsletter {
7+
.field-recaptcha {
8+
.field {
9+
.control {
10+
&:before {
11+
content: none;
12+
}
13+
}
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)