Source: scripts/helper/_transitions.js

'use strict';

/**
 * Transition helpers.
 * @namespace transitions
 * @memberof UCASDesignFramework
 * @param {object} global - UCASDesignFramework object.
 * @param {object} _u - UCASUtilities object.
 */
(function (global, _u) {
  /**
   * @function init
   * @memberof! UCASDesignFramework.transitions
   * @public
   */
  function init () {
    if (document.querySelector('.transition-trigger') && document.querySelector('.transition--hover-stop')) {
      if (!('ontouchstart' in document.documentElement)) {
        document.addEventListener('mouseenter', transitionMouseEnterHandler, true)
        document.documentElement.setAttribute('data-transition-initialised', 'true')
      }
    }
  }

  /**
   * Mouse enter handler.
   * @param {Event} e - DOM event
   */
  function transitionMouseEnterHandler (e) {
    var target = e.target

    if (target.classList !== undefined && target.classList.contains('transition--hover-stop')) {
      var parent = target.parentElement

      for (; parent && ((parent !== document)); parent = parent.parentNode) {
        if (parent.classList.contains('transition-trigger')) {
          break
        }
      }

      if (parent.classList.contains('transition-trigger')) {
        parent.setAttribute('data-transition-stopped', 'true')

        target.addEventListener('mouseleave', function (e) {
          parent.removeAttribute('data-transition-stopped')
        }, { once: true })
      }
    }
  }

  /**
   * Remove the event listener and data attribute.
   * @function destroy
   * @memberof! UCASDesignFramework.transitions
   * @public
   */
  function destroy () {
    document.removeEventListener('mouseenter', transitionMouseEnterHandler, true)
    document.documentElement.removeAttribute('data-transition-initialised')
  }

  // Expose public methods and properties.
  global.transitions = {
    init: init,
    destroy: destroy
  }
})(UCASDesignFramework, UCASUtilities)