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),
|
|
|
|
|
forceFocus = $component.data('forceFocus'),
|
|
|
|
|
labelText = $('label[for="' + $component.attr('id') + '"]').eq(0).text().trim(),
|
|
|
|
|
clearAriaLabel = evt => {
|
|
|
|
|
$component.removeAttr('aria-label');
|
|
|
|
|
$component.off('blur', clearAriaLabel);
|
|
|
|
|
};
|
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; }
|
|
|
|
|
|
2020-09-15 22:09:03 +01:00
|
|
|
// screenreaders announce the page title when a new page loads
|
|
|
|
|
// this will be lost when focus is moved to our form control so add it to the label instead
|
|
|
|
|
$component.attr('aria-label', document.title + ' - ' + labelText);
|
|
|
|
|
|
|
|
|
|
$component.filter('input, textarea, select').eq(0).trigger('focus');
|
|
|
|
|
|
|
|
|
|
// the page title prefix is only needed on page load so remove once focus has shifted
|
|
|
|
|
$component.on('blur', clearAriaLabel);
|
2016-02-02 16:16:29 +00:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})(window.GOVUK.Modules);
|