Skip to content

Commit

Permalink
Fixed lazy load
Browse files Browse the repository at this point in the history
Update readme
  • Loading branch information
hryvinskyi committed Sep 17, 2019
1 parent 8fe6ff4 commit a89d57c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -19,6 +19,11 @@ bin/magento module:enable Hryvinskyi_Base
bin/magento module:enable Hryvinskyi_InvisibleCaptcha
bin/magento setup:upgrade
````

### Features
1. Lazy Load, google page speed improvements
2. Easy to add captcha to your custom form

### Install download package
1. Download module https://github.com/hryvinskyi/magento2-base [Link](https://github.com/hryvinskyi/magento2-base/archive/v1.1.2.zip)
2. Download this module [Link](https://github.com/hryvinskyi/magento2-invisible-captcha/archive/2.0.4.zip)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -10,7 +10,7 @@
"scriptua/magento2-base": "2.0.*"
},
"type": "magento2-module",
"version": "2.1.1",
"version": "2.1.2",
"license": "GPL-3.0-or-later",
"autoload": {
"files": [ "registration.php" ],
Expand Down
18 changes: 10 additions & 8 deletions view/base/web/js/invisible-captcha.js
Expand Up @@ -11,7 +11,9 @@ define([
'./model/invisible-captcha'
], function ($, ko, Component, invisibleCaptcha) {
'use strict';

const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
};
return Component.extend({
defaults: {
template: 'Hryvinskyi_InvisibleCaptcha/invisible-captcha',
Expand Down Expand Up @@ -74,21 +76,21 @@ define([
*/
_initializeTokenField: function (element, self) {
if (invisibleCaptcha.initializedForms.indexOf(self.captchaId) === -1) {
invisibleCaptcha.initializedForms.push(self.captchaId);

var tokenField = $('<input type="hidden" name="hryvinskyi_invisible_token" />'),
siteKey = self.siteKey,
action = self.action;

tokenField.attr('data-action', action);
$(element).append(tokenField);

grecaptcha.ready(function () {
grecaptcha
.execute(siteKey, {action: action})
.then(function (token) {
tokenField.val(token);
invisibleCaptcha.initializedForms.push(self.captchaId);
});
});
tokenField.attr('data-action', action);
$(element).append(tokenField);
}
},

Expand All @@ -103,7 +105,7 @@ define([
let form = $(element).closest('form'),
needSubmit = false;

form.find(':input').on('focus blur change', $.proxy(self._loadRecaptchaScript, self));
form.on('focus blur change', ':input', $.proxy(self._loadRecaptchaScript, self));

// Disable submit form
form.on('submit', function (e) {
Expand All @@ -114,8 +116,8 @@ define([
});

// Submit form after recaptcha loaded
invisibleCaptcha.isApiLoaded.subscribe(function (newValue) {
if (needSubmit === true && newValue === true) {
invisibleCaptcha.initializedForms.subscribe(function (newValue) {
if (needSubmit === true && newValue.indexOf(self.captchaId) !== -1) {
form.submit();
}
});
Expand Down

0 comments on commit a89d57c

Please sign in to comment.