Source: scripts/helper/_features.js

'use strict';

/**
 * Identifies browser features.
 * @namespace features
 * @memberof UCASDesignFramework
 * @param {object} global - UCASDesignFramework object.
 * @param {object} _u - UCASUtilities object.
 */
(function (global, _u) {
  /**
   * 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')
    // 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
      document.documentElement.classList.add('user-is-tabbing')
      window.removeEventListener('keydown', handleFirstTab, true)
      window.addEventListener('mousedown', handleFirstClick, true)
    }
  }

  function handleFirstClick (e) {
    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
  }
})(UCASDesignFramework, UCASUtilities)