Passed
Push — master ( 8fe6ff...a89d57 )
by Володимир
05:41
created

view/base/web/js/invisible-captcha.js (1 issue)

Labels
Severity
1
/*
2
 * Copyright (c) 2019. Volodymyr Hryvinskyi.  All rights reserved.
3
 * @author: <mailto:[email protected]>
4
 * @github: <https://github.com/hryvinskyi>
5
 */
6
7
define([
8
    'jquery',
9
    'ko',
10
    'uiComponent',
11
    './model/invisible-captcha'
12
], function ($, ko, Component, invisibleCaptcha) {
13
    'use strict';
14
15
    return Component.extend({
16
        defaults: {
17
            template: 'Hryvinskyi_InvisibleCaptcha/invisible-captcha',
18
            action: '',
19
            captchaId: '',
20
            lazyLoad: false
21
        },
22
        _initializedForms: [],
23
24
        /**
25
         * Initialization
26
         */
27
        initialize: function () {
28
            this._super();
29
            this._loadGoogleApi();
30
        },
31
32
        /**
33
         * Initialize Google ReCaptca Script
34
         *
35
         * @private
36
         */
37
        _loadGoogleApi: function () {
38
            var self = this;
39
40
            if (invisibleCaptcha.isApiLoad() === true) {
41
                $(window).trigger('recaptcha_api_ready_' + self.captchaId);
42
43
                return;
44
            }
45
46
            window.onloadCallbackGoogleRecapcha = function () {
47
                invisibleCaptcha.isApiLoaded(true);
48
                invisibleCaptcha.initializeForms.each(function (item) {
49
                    self._initializeTokenField(item.element, item.self);
50
                });
51
52
                $(window).trigger('recaptcha_api_ready_' + self.captchaId);
53
            };
54
55
            if (self.lazyLoad === false) {
56
                self._loadRecaptchaScript();
57
            }
58
        },
59
60
        _loadRecaptchaScript: function () {
61
            if (invisibleCaptcha.isApiLoaded() === false) {
62
                require([
63
                    'https://www.google.com/recaptcha/api.js?onload=onloadCallbackGoogleRecapcha&render=' + this.siteKey
64
                ]);
65
66
                invisibleCaptcha.isApiLoad(true);
67
            }
68
        },
69
70
        /**
71
         * Loads google API and triggers event, when loaded
72
         *
73
         * @private
74
         */
75
        _initializeTokenField: function (element, self) {
76
            if (invisibleCaptcha.initializedForms.indexOf(self.captchaId) === -1) {
77
                invisibleCaptcha.initializedForms.push(self.captchaId);
78
79
                var tokenField = $('<input type="hidden" name="hryvinskyi_invisible_token" />'),
80
                    siteKey = self.siteKey,
81
                    action = self.action;
82
83
                grecaptcha.ready(function () {
0 ignored issues
show
The variable grecaptcha seems to be never declared. If this is a global, consider adding a /** global: grecaptcha */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
84
                    grecaptcha
85
                        .execute(siteKey, {action: action})
86
                        .then(function (token) {
87
                            tokenField.val(token);
88
                        });
89
                });
90
                tokenField.attr('data-action', action);
91
                $(element).append(tokenField);
92
            }
93
        },
94
95
        /**
96
         * Initialize recaptcha
97
         *
98
         * @param {Dom} element
99
         * @param {Object} self
100
         */
101
        initializeCaptcha: function (element, self) {
102
            if (self.lazyLoad === true) {
103
                let form = $(element).closest('form'),
104
                    needSubmit = false;
105
106
                form.find(':input').on('focus blur change', $.proxy(self._loadRecaptchaScript, self));
107
108
                // Disable submit form
109
                form.on('submit', function (e) {
110
                    if (invisibleCaptcha.isApiLoaded() === false) {
111
                        needSubmit = true;
112
                        e.preventDefault();
113
                    }
114
                });
115
116
                // Submit form after recaptcha loaded
117
                invisibleCaptcha.isApiLoaded.subscribe(function (newValue) {
118
                    if (needSubmit === true && newValue === true) {
119
                        form.submit();
120
                    }
121
                });
122
            }
123
124
            if (invisibleCaptcha.isApiLoad() === true && invisibleCaptcha.isApiLoaded() !== true) {
125
                invisibleCaptcha.initializeForms.push({'element': element, self: self});
126
            } else if (invisibleCaptcha.isApiLoaded() === true) {
127
                self._initializeTokenField(element, self);
128
            } else {
129
                $(window).on('recaptcha_api_ready_' + self.captchaId, function () {
130
                    self._initializeTokenField(element, self);
131
                });
132
            }
133
        }
134
    });
135
});