'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handleBlur = handleBlur;
exports.handleFocus = handleFocus;
exports.markForFocusLater = markForFocusLater;
exports.returnFocus = returnFocus;
exports.setupScopedFocus = setupScopedFocus;
exports.teardownScopedFocus = teardownScopedFocus;
var _tabbable = require('../helpers/tabbable');
var _tabbable2 = _interopRequireDefault(_tabbable);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var focusLaterElements = [];
var modalElement = null;
var needToFocus = false;
function handleBlur() {
needToFocus = true;
}
function handleFocus() {
Iif (needToFocus) {
needToFocus = false;
if (!modalElement) {
return;
}
// need to see how jQuery shims document.on('focusin') so we don't need the
// setTimeout, firefox doesn't support focusin, if it did, we could focus
// the element outside of a setTimeout. Side-effect of this implementation
// is that the document.body gets focus, and then we focus our element right
// after, seems fine.
setTimeout(function () {
if (modalElement.contains(document.activeElement)) {
return;
}
var el = (0, _tabbable2.default)(modalElement)[0] || modalElement;
el.focus();
}, 0);
}
}
function markForFocusLater() {
focusLaterElements.push(document.activeElement);
}
/* eslint-disable no-console */
function returnFocus() {
var toFocus = null;
try {
toFocus = focusLaterElements.pop();
toFocus.focus();
return;
} catch (e) {
console.warn(['You tried to return focus to', toFocus, 'but it is not in the DOM anymore'].join(" "));
}
}
/* eslint-enable no-console */
function setupScopedFocus(element) {
modalElement = element;
Eif (window.addEventListener) {
window.addEventListener('blur', handleBlur, false);
document.addEventListener('focus', handleFocus, true);
} else {
window.attachEvent('onBlur', handleBlur);
document.attachEvent('onFocus', handleFocus);
}
}
function teardownScopedFocus() {
modalElement = null;
Eif (window.addEventListener) {
window.removeEventListener('blur', handleBlur);
document.removeEventListener('focus', handleFocus);
} else {
window.detachEvent('onBlur', handleBlur);
document.detachEvent('onFocus', handleFocus);
}
}
|