JAVASCRIPT   13

TransitionEnd until

Guest on 6th July 2022 01:03:30 AM

  1. /**
  2.  * --------------------------------------------------------------------------
  3.  * Bootstrap (v4.3.1): util.js
  4.  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  5.  * --------------------------------------------------------------------------
  6.  */
  7.  
  8. import $ from 'jquery'
  9.  
  10. /**
  11.  * ------------------------------------------------------------------------
  12.  * Private TransitionEndTransitionEnd Helpers
  13.  * ------------------------------------------------------------------------
  14.  */
  15.  
  16. const TRANSITION_END = 'transitionend'
  17. const MAX_UID = 1000000
  18. const MILLISECONDS_MULTIPLIER = 1000
  19.  
  20. // Shoutout AngusCroll (https://goo.gl/pxwQGp)
  21. function toType(obj) {
  22.   return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase()
  23. }
  24.  
  25. function getSpecialTransitionEndEvent() {
  26.   return {
  27.     bindType: TRANSITION_END,
  28.     delegateType: TRANSITION_END,
  29.     handle(event) {
  30.       if ($(event.target).is(this)) {
  31.         return event.handleObj.handler.apply(this, arguments) // eslint-disable-line prefer-rest-params
  32.       }
  33.       return undefined // eslint-disable-line no-undefined
  34.     }
  35.   }
  36. }
  37.  
  38. function transitionEndEmulator(duration) {
  39.   let called = false
  40.  
  41.   $(this).one(Util.TRANSITION_END, () => {
  42.     called = true
  43.   })
  44.  
  45.   setTimeout(() => {
  46.     if (!called) {
  47.       Util.triggerTransitionEnd(this)
  48.     }
  49.   }, duration)
  50.  
  51.   return this
  52. }
  53.  
  54. function setTransitionEndSupport() {
  55.   $.fn.emulateTransitionEnd = transitionEndEmulator
  56.   $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent()
  57. }
  58.  
  59. /**
  60.  * --------------------------------------------------------------------------
  61.  * Public Util Api
  62.  * --------------------------------------------------------------------------
  63.  */
  64.  
  65. const Util = {
  66.  
  67.   TRANSITION_END: 'bsTransitionEnd',
  68.  
  69.   getUID(prefix) {
  70.     do {
  71.       // eslint-disable-next-line no-bitwise
  72.       prefix += ~~(Math.random() * MAX_UID) // "~~" acts like a faster Math.floor() here
  73.     } while (document.getElementById(prefix))
  74.     return prefix
  75.   },
  76.  
  77.   getSelectorFromElement(element) {
  78.     let selector = element.getAttribute('data-target')
  79.  
  80.     if (!selector || selector === '#') {
  81.       const hrefAttr = element.getAttribute('href')
  82.       selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : ''
  83.     }
  84.  
  85.     try {
  86.       return document.querySelector(selector) ? selector : null
  87.     } catch (err) {
  88.       return null
  89.     }
  90.   },
  91.  
  92.   getTransitionDurationFromElement(element) {
  93.     if (!element) {
  94.       return 0
  95.     }
  96.  
  97.     // Get transition-duration of the element
  98.     let transitionDuration = $(element).css('transition-duration')
  99.     let transitionDelay = $(element).css('transition-delay')
  100.  
  101.     const floatTransitionDuration = parseFloat(transitionDuration)
  102.     const floatTransitionDelay = parseFloat(transitionDelay)
  103.  
  104.     // Return 0 if element or transition duration is not found
  105.     if (!floatTransitionDuration && !floatTransitionDelay) {
  106.       return 0
  107.     }
  108.  
  109.     // If multiple durations are defined, take the first
  110.     transitionDuration = transitionDuration.split(',')[0]
  111.     transitionDelay = transitionDelay.split(',')[0]
  112.  
  113.     return (parseFloat(transitionDuration) + parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER
  114.   },
  115.  
  116.   reflow(element) {
  117.     return element.offsetHeight
  118.   },
  119.  
  120.   triggerTransitionEnd(element) {
  121.     $(element).trigger(TRANSITION_END)
  122.   },
  123.  
  124.   // TODO: Remove in v5
  125.   supportsTransitionEnd() {
  126.     return Boolean(TRANSITION_END)
  127.   },
  128.  
  129.   isElement(obj) {
  130.     return (obj[0] || obj).nodeType
  131.   },
  132.  
  133.   typeCheckConfig(componentName, config, configTypes) {
  134.     for (const property in configTypes) {
  135.       if (Object.prototype.hasOwnProperty.call(configTypes, property)) {
  136.         const expectedTypes = configTypes[property]
  137.         const value         = config[property]
  138.         const valueType     = value && Util.isElement(value)
  139.           ? 'element' : toType(value)
  140.  
  141.         if (!new RegExp(expectedTypes).test(valueType)) {
  142.           throw new Error(
  143.             `${componentName.toUpperCase()}: ` +
  144.             `Option "${property}" provided type "${valueType}" ` +
  145.             `but expected type "${expectedTypes}".`)
  146.         }
  147.       }
  148.     }
  149.   },
  150.  
  151.   findShadowRoot(element) {
  152.     if (!document.documentElement.attachShadow) {
  153.       return null
  154.     }
  155.  
  156.     // Can find the shadow root otherwise it'll return the document
  157.     if (typeof element.getRootNode === 'function') {
  158.       const root = element.getRootNode()
  159.       return root instanceof ShadowRoot ? root : null
  160.     }
  161.  
  162.     if (element instanceof ShadowRoot) {
  163.       return element
  164.     }
  165.  
  166.     // when we don't find a shadow root
  167.     if (!element.parentNode) {
  168.       return null
  169.     }
  170.  
  171.     return Util.findShadowRoot(element.parentNode)
  172.   }
  173. }
  174.  
  175. setTransitionEndSupport()
  176.  
  177. export default Util

Raw Paste


Login or Register to edit or fork this paste. It's free.