From 5fa25c17424d807e02b52601edb9c96ae867334d Mon Sep 17 00:00:00 2001 From: Riccardo Tempesta Date: Tue, 11 Jul 2017 14:11:37 +0200 Subject: [PATCH] Fix issue #6 + Fix reCaptcha renewal --- etc/module.xml | 2 +- view/frontend/layout/checkout_index_index.xml | 23 +++++++ view/frontend/requirejs-config.js | 28 +++++++++ view/frontend/web/js/reCaptcha.js | 61 ++++++++++--------- view/frontend/web/js/registry.js | 27 ++++++++ view/frontend/web/js/ui-messages-mixin.js | 40 ++++++++++++ 6 files changed, 151 insertions(+), 30 deletions(-) create mode 100644 view/frontend/requirejs-config.js create mode 100644 view/frontend/web/js/registry.js create mode 100644 view/frontend/web/js/ui-messages-mixin.js diff --git a/etc/module.xml b/etc/module.xml index cb61293..d2ebe9a 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -21,7 +21,7 @@ --> - + diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index 14de7d1..aa0ee54 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -31,6 +31,29 @@ + + + + + + + + + + MSP_ReCaptcha/js/reCaptcha + additional-login-form-fields + checkoutConfig + msp-recaptcha-checkout-inline-login + + + + + + + + + + diff --git a/view/frontend/requirejs-config.js b/view/frontend/requirejs-config.js new file mode 100644 index 0000000..78d8a88 --- /dev/null +++ b/view/frontend/requirejs-config.js @@ -0,0 +1,28 @@ +/** + * MageSpecialist + * + * NOTICE OF LICENSE + * + * This source file is subject to the Open Software License (OSL 3.0) + * that is bundled with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://opensource.org/licenses/osl-3.0.php + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to info@magespecialist.it so we can send you a copy immediately. + * + * @category MSP + * @package MSP_ReCaptcha + * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ + +var config = { + config: { + mixins: { + 'Magento_Ui/js/view/messages': { + 'MSP_ReCaptcha/js/ui-messages-mixin': true + } + } + } +}; \ No newline at end of file diff --git a/view/frontend/web/js/reCaptcha.js b/view/frontend/web/js/reCaptcha.js index 7b19919..6170c40 100644 --- a/view/frontend/web/js/reCaptcha.js +++ b/view/frontend/web/js/reCaptcha.js @@ -20,35 +20,38 @@ /*browser:true jquery:true*/ /*global define*/ define( - [ - 'uiComponent', - 'https://www.google.com/recaptcha/api.js' - ], - function (Component, reCaptcha) { - 'use strict'; + [ + 'uiComponent', + 'MSP_ReCaptcha/js/registry', + 'https://www.google.com/recaptcha/api.js' + ], + function (Component, registry, reCaptcha) { + 'use strict'; - return Component.extend({ - defaults: { - template: 'MSP_ReCaptcha/reCaptcha' - }, - getIsVisible: function() { - return window.mspReCaptchaConfig.enabled; - }, - getSiteKey: function() { - return window.mspReCaptchaConfig.siteKey; - }, - renderReCaptcha: function () { - grecaptcha.render(this.getReCaptchaId(), { - 'sitekey': this.getSiteKey() - }); - }, - getReCaptchaId: function () { - if (!this.reCaptchaId) { - return 'msp-recaptcha'; - } + return Component.extend({ + defaults: { + template: 'MSP_ReCaptcha/reCaptcha' + }, + getIsVisible: function () { + return window.mspReCaptchaConfig.enabled; + }, + getSiteKey: function () { + return window.mspReCaptchaConfig.siteKey; + }, + renderReCaptcha: function () { + registry.ids.push(this.getReCaptchaId()); - return this.reCaptchaId; - } - }); - } + registry.captchaList.push(grecaptcha.render(this.getReCaptchaId(), { + 'sitekey': this.getSiteKey() + })); + }, + getReCaptchaId: function () { + if (!this.reCaptchaId) { + return 'msp-recaptcha'; + } + + return this.reCaptchaId; + } + }); + } ); diff --git a/view/frontend/web/js/registry.js b/view/frontend/web/js/registry.js new file mode 100644 index 0000000..5819d9e --- /dev/null +++ b/view/frontend/web/js/registry.js @@ -0,0 +1,27 @@ +/** + * MageSpecialist + * + * NOTICE OF LICENSE + * + * This source file is subject to the Open Software License (OSL 3.0) + * that is bundled with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://opensource.org/licenses/osl-3.0.php + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to info@magespecialist.it so we can send you a copy immediately. + * + * @category MSP + * @package MSP_ReCaptcha + * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ + +define(['ko'], function (ko) { + 'use strict'; + + return { + ids: ko.observableArray([]), + captchaList: ko.observableArray([]) + }; +}); \ No newline at end of file diff --git a/view/frontend/web/js/ui-messages-mixin.js b/view/frontend/web/js/ui-messages-mixin.js new file mode 100644 index 0000000..2574ad0 --- /dev/null +++ b/view/frontend/web/js/ui-messages-mixin.js @@ -0,0 +1,40 @@ +/** + * MageSpecialist + * + * NOTICE OF LICENSE + * + * This source file is subject to the Open Software License (OSL 3.0) + * that is bundled with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * http://opensource.org/licenses/osl-3.0.php + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to info@magespecialist.it so we can send you a copy immediately. + * + * @category MSP + * @package MSP_ReCaptcha + * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ + +define(['MSP_ReCaptcha/js/registry'], function (registry) { + 'use strict'; + + return function (originalComponent) { + 'use strict'; + + return originalComponent.extend({ + initialize: function () { + this._super(); + + this.messageContainer.errorMessages.subscribe(function (newValue) { + registry.captchaList().forEach(function (captcha) { + grecaptcha.reset(captcha); + }); + }, null, "arrayChange"); + + return this; + } + }); + }; +}); \ No newline at end of file