2016-02-02 16:16:29 +00:00
|
|
|
(function(Modules) {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
Modules.Autofocus = function() {
|
2019-04-02 11:38:27 +01:00
|
|
|
this.start = function(component) {
|
2020-09-15 22:09:03 +01:00
|
|
|
var $component = $(component),
|
2020-10-03 19:47:39 +01:00
|
|
|
forceFocus = $component.data('forceFocus');
|
2016-02-02 16:16:29 +00:00
|
|
|
|
2019-03-28 16:45:36 +00:00
|
|
|
// if the page loads with a scroll position, we can't assume the item to focus onload
|
|
|
|
|
// is still where users intend to start
|
|
|
|
|
if (($(window).scrollTop() > 0) && !forceFocus) { return; }
|
|
|
|
|
|
2021-11-04 11:28:31 +00:00
|
|
|
// See if the component itself is something we want to send focus to
|
|
|
|
|
var target = $component.filter('input, textarea, select');
|
|
|
|
|
|
|
|
|
|
// Otherwise look inside the component to see if there are any elements
|
|
|
|
|
// we want to send focus to
|
|
|
|
|
if (target.length === 0) {
|
|
|
|
|
target = $('input, textarea, select', $component);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
target.eq(0).trigger('focus');
|
2020-09-15 22:09:03 +01:00
|
|
|
|
2016-02-02 16:16:29 +00:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})(window.GOVUK.Modules);
|