Skip to content
Snippets Groups Projects
Unverified Commit 55b7d232 authored by Saurabh Kumar's avatar Saurabh Kumar Committed by GitHub
Browse files

Performance improvement on application form (#3577)


This PR optimizes the code and logic responsible for unlocking
the application form on detecting human activity.

- makes the unlocking action DRY.
- move away from using jquery
- prevent the unlock action from firing on every mouse move, the unlock
  action is performed only once.
- Use more standard key names instead of keycodes.

---------

Co-authored-by: default avatarFredrik Jonsson <frjo@xdeb.org>
parent cdcbda5e
No related branches found
No related tags found
2 merge requests!23Add ReviewOpinion delete functionality to review sidebar (#3565),!20Merge in v4.7.0 and v4.8.0
This commit is part of merge request !20. Comments created here will be created in the context of that merge request.
(function ($) { (function () {
"use strict"; "use strict";
$(".application-form").each(function () { const form = document.querySelector(".application-form");
var $application_form = $(this); const button = form.querySelector("[type=submit]");
var $application_form_button = $application_form.find( const required = form.querySelectorAll("input[required]");
'button[type="submit"]' const groups = form.querySelectorAll(".form__group");
); const errors = form.querySelectorAll(".form__error");
// set aria-required attribute true for required fields // Set aria-required attribute true for required fields.
$application_form required.forEach(function (field) {
.find("input[required]") field.setAttribute("aria-required", true);
.each(function (index, input_field) { });
input_field.setAttribute("aria-required", true);
});
// add label_id as aria-describedby to help texts
$application_form
.find(".form__group")
.each(function (index, form_group) {
var label = form_group.querySelector("label");
if (label) {
var label_id = label.getAttribute("for");
if (form_group.querySelector(".form__help")) {
form_group
.querySelector(".form__help")
.setAttribute("aria-describedby", label_id);
}
}
});
// set aria-invalid for field with errors
var $error_fields = $application_form.find(".form__error");
if ($error_fields.length) {
// set focus to the first error field
$error_fields[0].querySelector("input").focus();
$error_fields.each(function (index, error_field) { // Add label_id as aria-describedby to help text.
const inputEl = error_field.querySelector("input, textarea"); groups.forEach(function (group) {
if (inputEl) { const label = group.querySelector("label");
inputEl.setAttribute("aria-invalid", true); if (label) {
} const label_id = label.getAttribute("for");
}); if (group.querySelector(".form__help")) {
group
.querySelector(".form__help")
.setAttribute("aria-describedby", label_id);
}
} }
});
// Remove the "no javascript" messages if (errors.length) {
$(".message-no-js").detach(); // Set focus to the first error field.
const first_error = errors[0].querySelector("input");
if (first_error) {
first_error.focus();
}
// Wait for a mouse to move, indicating they are human. // Set aria-invalid for field with errors.
$("body").mousemove(function () { errors.forEach(function (error) {
// Unlock the form. const input = error.querySelector("input, textarea");
$application_form.attr("action", ""); if (input) {
$application_form_button.attr("disabled", false); input.setAttribute("aria-invalid", true);
}
}); });
}
// Wait for a touch move event, indicating that they are human. // Remove the "no javascript" messages
$("body").on("touchmove", function () { document.querySelector(".message-no-js").remove();
// Unlock the form.
$application_form.attr("action", "");
$application_form_button.attr("disabled", false);
});
// A tab or enter key pressed can also indicate they are human. const unlockApplicationForm = function () {
$("body").keydown(function (e) { form.setAttribute("action", "");
if (e.keyCode === 9 || e.keyCode === 13) { button.removeAttribute("disabled");
// Unlock the form. };
$application_form.attr("action", "");
$application_form_button.attr("disabled", false); // Unlock form on
} // 1. mouse move
}); // 2. touch move
// 3. tab or enter key pressed
document.body.addEventListener("mousemove", unlockApplicationForm, {
once: true,
});
document.body.addEventListener("touchmove", unlockApplicationForm, {
once: true,
}); });
})(jQuery); document.body.addEventListener(
"keydown",
function (e) {
if (e.key === "Tab" || e.key === "Enter") {
unlockApplicationForm();
}
},
{ once: true }
);
})();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment