Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to non-jQuery JavaScript #6

Open
xiaohutai opened this issue Mar 20, 2018 · 2 comments
Open

Refactor to non-jQuery JavaScript #6

xiaohutai opened this issue Mar 20, 2018 · 2 comments

Comments

@xiaohutai
Copy link
Owner

xiaohutai commented Mar 20, 2018

In BoltForms v4.2.1, some changes have been made with AJAX submissions. No more JQuery it seems.

AJAX

This solution works for AJAX + non-ReCaptcha forms [but doesn't work nice when errors are present, so that's why keep the forms simple as possible):

var forms = document.querySelectorAll(".is-useful-wrapper > .expand > .boltform > form");
Array.prototype.forEach.call(forms, function(el) {
    el.addEventListener("submit", function(event) {
        var wrapper = el
            .parentElement // .boltform
            .parentElement // .expand
            .parentElement // .is-useful-wrapper
        ;
        wrapper.innerHTML = "<span class='is-useful-success'>" + wrapper.getAttribute("data-thanks") + "</span>";
    });
});
@xiaohutai
Copy link
Owner Author

xiaohutai commented Jul 19, 2018

AJAX + ReCaptcha

function invisibleRecaptchaOnLoad() {
  function createHiddenElement(name, value) {
    var input = document.createElement("input");
    input.setAttribute("type", "hidden");
    input.setAttribute("name", name);
    input.setAttribute("value", value);
    return input;
  }

  var els = document.getElementsByClassName('g-recaptcha-button');
  for (var i = 0; i < els.length; ++i) {
    var buttonElement = els[i];
    grecaptcha.render(buttonElement, {
      sitekey: buttonElement.getAttribute('data-sitekey'),
      size: 'invisible',
      callback: function (token) {
        if (token) {
          var form = buttonElement.closest('form');
          form.appendChild(createHiddenElement('g-recaptcha-response', token));

          var XHR = new XMLHttpRequest();
          var data = new FormData(form);
          XHR.addEventListener("load", function () {
            var wrapper = form
              .parentElement // .boltform
              .parentElement // .expand
              .parentElement // .is-useful-wrapper
            ;
            wrapper.innerHTML = "<span class='is-useful-success'>" + wrapper.getAttribute("data-thanks") + "</span>";
          });
          XHR.addEventListener("error", function () {
            boltFormError(form, this);
          });
          XHR.open("POST", form.action);
          XHR.send(data);
        }
      }
    });
  }
}

and invisibleRecaptchaOnLoad must be in the global scope.

@xiaohutai
Copy link
Owner Author

xiaohutai commented Jul 19, 2018

AJAX + ReCaptcha + other non-AJAX forms

function invisibleRecaptchaOnLoad() {
  function createHiddenElement(name, value) {
    var input = document.createElement("input");
    input.setAttribute("type", "hidden");
    input.setAttribute("name", name);
    input.setAttribute("value", value);
    return input;
  }

  var els = document.getElementsByClassName('g-recaptcha-button');
  for (var i = 0; i < els.length; ++i) {
    var buttonElement = els[i];
    grecaptcha.render(buttonElement, {
      sitekey: buttonElement.getAttribute('data-sitekey'),
      size: 'invisible',
      callback: function (token) {
        if (token) {
          var form = buttonElement.closest('form');
          form.appendChild(createHiddenElement('g-recaptcha-response', token));

          if (form.action.indexOf('async') !== -1 ) {
              // -- ajax forms
              var XHR = new XMLHttpRequest();
              var data = new FormData(form);
              XHR.addEventListener("load", function () {
                var wrapper = form
                  .parentElement // .boltform
                  .parentElement // .expand
                  .parentElement // .is-useful-wrapper
                ;
                wrapper.innerHTML = "<span class='is-useful-success'>" + wrapper.getAttribute("data-thanks") + "</span>";
              });
              XHR.addEventListener("error", function () {
                boltFormError(form, this);
              });
              XHR.open("POST", form.action);
              XHR.send(data);
          } else {
            // -- normal forms
            form.submit();
          }
        }
      }
    });
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant