|
| 1 | +/* |
| 2 | + * Project: PIMCORE FormBuilder |
| 3 | + * Extension: Friendly Captcha Injector |
| 4 | + * Since: 1.1.0 |
| 5 | + * Author: DACHCOM.DIGITAL |
| 6 | + * License: GPLv3 |
| 7 | +
|
| 8 | +*/ |
| 9 | +(function (global, factory) { |
| 10 | + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('jquery'), window) : |
| 11 | + typeof define === 'function' && define.amd ? define(['exports', 'jquery', 'window'], factory) : |
| 12 | + (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory({}, global.jQuery, window)); |
| 13 | +}(this, (function (exports, $, window) { |
| 14 | + 'use strict'; |
| 15 | + |
| 16 | + var clName = 'FriendlyCaptcha'; |
| 17 | + |
| 18 | + function FriendlyCaptcha(form, options) { |
| 19 | + this.siteKey = null; |
| 20 | + this.dataSolutionFieldName = null; |
| 21 | + this.start = null; |
| 22 | + this.lang = null; |
| 23 | + this.callback = null; |
| 24 | + this.puzzleEndpoint = null; |
| 25 | + this.$form = $(form); |
| 26 | + this.options = $.extend({}, $.fn.formBuilderFriendlyCaptcha.defaults, options); |
| 27 | + this.init(); |
| 28 | + } |
| 29 | + |
| 30 | + $.extend(FriendlyCaptcha.prototype, { |
| 31 | + |
| 32 | + init: function () { |
| 33 | + |
| 34 | + this.$friendlyCaptchaField = this.$form.find(this.options.friendlyCaptchaFieldClass); |
| 35 | + |
| 36 | + if (this.$friendlyCaptchaField.length === 0) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + if (this.$friendlyCaptchaField.length > 1) { |
| 41 | + alert('Form has invalid amount of friendly captcha fields. There should be only one dedicated captcha field!'); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + this.siteKey = this.$friendlyCaptchaField.data('sitekey'); |
| 46 | + this.dataSolutionFieldName = this.$friendlyCaptchaField.data('data-solution-field-name'); |
| 47 | + this.start = this.$friendlyCaptchaField.data('start'); |
| 48 | + this.lang = this.$friendlyCaptchaField.data('lang'); |
| 49 | + this.callback = this.$friendlyCaptchaField.data('callback'); |
| 50 | + this.puzzleEndpoint = this.$friendlyCaptchaField.data('puzzle-endpoint'); |
| 51 | + |
| 52 | + this.$form.on('formbuilder.success', this.onReset.bind(this)); |
| 53 | + this.$form.on('formbuilder.fail', this.onReset.bind(this)); |
| 54 | + |
| 55 | + this.bindDependency(); |
| 56 | + }, |
| 57 | + |
| 58 | + bindDependency: function () { |
| 59 | + |
| 60 | + var widgetUrl = 'https://cdn.jsdelivr.net/npm/friendly-challenge/widget.min.js'; |
| 61 | + |
| 62 | + if (this.options.autoWidgetVersionToLoad !== null) { |
| 63 | + widgetUrl = `https://cdn.jsdelivr.net/npm/friendly-challenge@${this.options.autoWidgetVersionToLoad}/widget.min.js`; |
| 64 | + } |
| 65 | + |
| 66 | + if (this.options.useAutoWidget === false) { |
| 67 | + |
| 68 | + if (typeof this.options.setupField === 'function') { |
| 69 | + this.options.setupField(this.$friendlyCaptchaField, this.$form, { |
| 70 | + sitekey: this.siteKey, |
| 71 | + dataSolutionFieldName: this.dataSolutionFieldName, |
| 72 | + start: this.start, |
| 73 | + lang: this.lang, |
| 74 | + callback: this.callback, |
| 75 | + puzzleEndpoint: this.puzzleEndpoint, |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + if (typeof window.friendlyChallenge !== 'undefined') { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + $.getScript(widgetUrl); |
| 87 | + }, |
| 88 | + |
| 89 | + onReset: function () { |
| 90 | + |
| 91 | + if (typeof window.friendlyChallenge === 'undefined') { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (this.$friendlyCaptchaField.length === 0) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + window.friendlyChallenge.autoWidget.reset(); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + $.fn.formBuilderFriendlyCaptcha = function (options) { |
| 104 | + this.each(function () { |
| 105 | + if (!$.data(this, 'fb-' + clName)) { |
| 106 | + $.data(this, 'fb-' + clName, new FriendlyCaptcha(this, options)); |
| 107 | + } |
| 108 | + }); |
| 109 | + return this; |
| 110 | + }; |
| 111 | + |
| 112 | + $.fn.formBuilderFriendlyCaptcha.defaults = { |
| 113 | + useAutoWidget: true, |
| 114 | + autoWidgetVersionToLoad: null, |
| 115 | + setupField: null, |
| 116 | + friendlyCaptchaFieldClass: 'div.form-builder-friendly-captcha', |
| 117 | + }; |
| 118 | +}))); |
0 commit comments