Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
util.js196 linesDownload Raw Back to src
1/**2 * --------------------------------------------------------------------------3 * Bootstrap (v4.6.2): util.js4 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)5 * --------------------------------------------------------------------------6 */7 8import $ from 'jquery'9 10/**11 * Private TransitionEnd Helpers12 */13 14const TRANSITION_END = 'transitionend'15const MAX_UID = 100000016const MILLISECONDS_MULTIPLIER = 100017 18// Shoutout AngusCroll (https://goo.gl/pxwQGp)19function toType(obj) {20  if (obj === null || typeof obj === 'undefined') {21    return `${obj}`22  }23 24  return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase()25}26 27function getSpecialTransitionEndEvent() {28  return {29    bindType: TRANSITION_END,30    delegateType: TRANSITION_END,31    handle(event) {32      if ($(event.target).is(this)) {33        return event.handleObj.handler.apply(this, arguments) // eslint-disable-line prefer-rest-params34      }35 36      return undefined37    }38  }39}40 41function transitionEndEmulator(duration) {42  let called = false43 44  $(this).one(Util.TRANSITION_END, () => {45    called = true46  })47 48  setTimeout(() => {49    if (!called) {50      Util.triggerTransitionEnd(this)51    }52  }, duration)53 54  return this55}56 57function setTransitionEndSupport() {58  $.fn.emulateTransitionEnd = transitionEndEmulator59  $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent()60}61 62/**63 * Public Util API64 */65 66const Util = {67  TRANSITION_END: 'bsTransitionEnd',68 69  getUID(prefix) {70    do {71      // eslint-disable-next-line no-bitwise72      prefix += ~~(Math.random() * MAX_UID) // "~~" acts like a faster Math.floor() here73    } while (document.getElementById(prefix))74 75    return prefix76  },77 78  getSelectorFromElement(element) {79    let selector = element.getAttribute('data-target')80 81    if (!selector || selector === '#') {82      const hrefAttr = element.getAttribute('href')83      selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : ''84    }85 86    try {87      return document.querySelector(selector) ? selector : null88    } catch (_) {89      return null90    }91  },92 93  getTransitionDurationFromElement(element) {94    if (!element) {95      return 096    }97 98    // Get transition-duration of the element99    let transitionDuration = $(element).css('transition-duration')100    let transitionDelay = $(element).css('transition-delay')101 102    const floatTransitionDuration = parseFloat(transitionDuration)103    const floatTransitionDelay = parseFloat(transitionDelay)104 105    // Return 0 if element or transition duration is not found106    if (!floatTransitionDuration && !floatTransitionDelay) {107      return 0108    }109 110    // If multiple durations are defined, take the first111    transitionDuration = transitionDuration.split(',')[0]112    transitionDelay = transitionDelay.split(',')[0]113 114    return (parseFloat(transitionDuration) + parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER115  },116 117  reflow(element) {118    return element.offsetHeight119  },120 121  triggerTransitionEnd(element) {122    $(element).trigger(TRANSITION_END)123  },124 125  supportsTransitionEnd() {126    return Boolean(TRANSITION_END)127  },128 129  isElement(obj) {130    return (obj[0] || obj).nodeType131  },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 null154    }155 156    // Can find the shadow root otherwise it'll return the document157    if (typeof element.getRootNode === 'function') {158      const root = element.getRootNode()159      return root instanceof ShadowRoot ? root : null160    }161 162    if (element instanceof ShadowRoot) {163      return element164    }165 166    // when we don't find a shadow root167    if (!element.parentNode) {168      return null169    }170 171    return Util.findShadowRoot(element.parentNode)172  },173 174  jQueryDetection() {175    if (typeof $ === 'undefined') {176      throw new TypeError('Bootstrap\'s JavaScript requires jQuery. jQuery must be included before Bootstrap\'s JavaScript.')177    }178 179    const version = $.fn.jquery.split(' ')[0].split('.')180    const minMajor = 1181    const ltMajor = 2182    const minMinor = 9183    const minPatch = 1184    const maxMajor = 4185 186    if (version[0] < ltMajor && version[1] < minMinor || version[0] === minMajor && version[1] === minMinor && version[2] < minPatch || version[0] >= maxMajor) {187      throw new Error('Bootstrap\'s JavaScript requires at least jQuery v1.9.1 but less than v4.0.0')188    }189  }190}191 192Util.jQueryDetection()193setTransitionEndSupport()194 195export default Util196 
codekingpro/portable-devtools · Team Ai