Source: scripts/helper/_focus.js

'use strict';

/**
 * Helps with any focus issues.
 * @namespace focus
 * @memberof UCASDesignFramework
 * @param {object} global - UCASDesignFramework object.
 * @param {object} _u - UCASUtilities object.
 */
(function (global, _u) {
  /**
   * Initialise focus plugin.
   * @function init
   * @memberof! UCASDesignFramework.focus
   * @public
   * @param  {Node} [context=document] - DOM element we're limiting selection to
   */
  function init (context) {
    context = context || document
    var items = context.querySelectorAll('[data-focus-element]')
    items.forEach(function (item) {
      var id = item.getAttribute('data-focus-element')
      var focusElement = document.getElementById(id)
      if (focusElement) {
        item.addEventListener('focus', function () {
          if (Element.prototype.scrollIntoViewIfNeeded) {
            focusElement.scrollIntoViewIfNeeded()
          } else {
            // @todo DF-1229
          }
        })
      }
    })
  }

  // Expose public methods.
  global.focus = {
    init: init
  }
})(UCASDesignFramework, UCASUtilities)