'use strict';
/**
* Identifies browser features.
* @namespace features
* @memberof UCASDesignFramework
* @param {object} global - UCASDesignFramework object.
* @param {object} _u - UCASUtilities object.
*/
(function (global, _u) {
var userIsTabbing
/**
* Initialise features plugin.
* @function init
* @memberof! UCASDesignFramework.features
* @public
*/
function init () {
// Touch detection.
'ontouchstart' in document.documentElement ? document.documentElement.classList.add('touch') : document.documentElement.classList.add('no-touch')
userIsTabbing = false
// Tab key detection.
window.addEventListener('keydown', handleFirstTab, true)
// Click detection.
window.addEventListener('mousedown', handleFirstClick, true)
}
// https://medium.com/hackernoon/removing-that-ugly-focus-ring-and-keeping-it-too-6c8727fefcd2
function handleFirstTab (e) {
if (e.keyCode === 9) { // the "I am a keyboard user" key
userIsTabbing = true
document.documentElement.classList.add('user-is-tabbing')
window.removeEventListener('keydown', handleFirstTab, true)
window.addEventListener('mousedown', handleFirstClick, true)
}
}
function handleFirstClick (e) {
// Make sure it is a genuine click and not an event trigered by NVDA, for example.
if (e.screenX > 0 && e.screenY > 0) {
userIsTabbing = false
document.documentElement.classList.remove('user-is-tabbing')
window.removeEventListener('mousedown', handleFirstClick, true)
window.addEventListener('keydown', handleFirstTab, true)
}
}
// Expose public methods.
global.features = {
init: init,
initOnLoad: true,
/**
* @function userIsTabbing
* @memberof! UCASDesignFramework.features
* @return {bool} - whether the user is tabbing or not
*/
get userIsTabbing () {
return userIsTabbing
}
}
})(UCASDesignFramework, UCASUtilities)