util.js 4.05 KB
Newer Older
1
2
import $ from 'jquery'

fat's avatar
fat committed
3
4
/**
 * --------------------------------------------------------------------------
Mark Otto's avatar
Mark Otto committed
5
 * Bootstrap (v4.1.3): util.js
fat's avatar
fat committed
6
7
8
9
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

10
const Util = (($) => {
11
12
13
14
15
  /**
   * ------------------------------------------------------------------------
   * Private TransitionEnd Helpers
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
16

17
  const TRANSITION_END = 'transitionend'
18
  const MAX_UID = 1000000
19
  const MILLISECONDS_MULTIPLIER = 1000
20

XhmikosR's avatar
XhmikosR committed
21
  // Shoutout AngusCroll (https://goo.gl/pxwQGp)
fat's avatar
fat committed
22
  function toType(obj) {
denisx's avatar
denisx committed
23
    return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase()
fat's avatar
fat committed
24
25
  }

26
27
  function getSpecialTransitionEndEvent() {
    return {
28
29
      bindType: TRANSITION_END,
      delegateType: TRANSITION_END,
Jacob Thornton's avatar
Jacob Thornton committed
30
      handle(event) {
31
        if ($(event.target).is(this)) {
32
          return event.handleObj.handler.apply(this, arguments) // eslint-disable-line prefer-rest-params
33
        }
XhmikosR's avatar
XhmikosR committed
34
        return undefined // eslint-disable-line no-undefined
35
36
37
      }
    }
  }
fat's avatar
fat committed
38

39
40
  function transitionEndEmulator(duration) {
    let called = false
fat's avatar
fat committed
41

Jacob Thornton's avatar
Jacob Thornton committed
42
    $(this).one(Util.TRANSITION_END, () => {
43
44
      called = true
    })
fat's avatar
fat committed
45

46
47
    setTimeout(() => {
      if (!called) {
fat's avatar
fat committed
48
        Util.triggerTransitionEnd(this)
fat's avatar
fat committed
49
      }
50
    }, duration)
fat's avatar
fat committed
51

52
    return this
fat's avatar
fat committed
53
54
  }

55
56
  function setTransitionEndSupport() {
    $.fn.emulateTransitionEnd = transitionEndEmulator
57
    $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent()
fat's avatar
fat committed
58
59
  }

60
61
62
63
64
  /**
   * --------------------------------------------------------------------------
   * Public Util Api
   * --------------------------------------------------------------------------
   */
fat's avatar
fat committed
65

66
  const Util = {
fat's avatar
fat committed
67

68
69
70
    TRANSITION_END: 'bsTransitionEnd',

    getUID(prefix) {
Jacob Thornton's avatar
Jacob Thornton committed
71
      do {
72
        // eslint-disable-next-line no-bitwise
73
        prefix += ~~(Math.random() * MAX_UID) // "~~" acts like a faster Math.floor() here
Jacob Thornton's avatar
Jacob Thornton committed
74
      } while (document.getElementById(prefix))
75
76
      return prefix
    },
fat's avatar
fat committed
77

78
79
    getSelectorFromElement(element) {
      let selector = element.getAttribute('data-target')
80

81
      if (!selector || selector === '#') {
82
83
        const hrefAttr = element.getAttribute('href')
        selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : ''
84
85
      }

86
      return selector && document.querySelector(selector) ? selector : null
87
    },
fat's avatar
fat committed
88

89
    getTransitionDurationFromElement(element) {
90
91
92
93
      if (!element) {
        return 0
      }

94
95
      // Get transition-duration of the element
      let transitionDuration = $(element).css('transition-duration')
96
      const floatTransitionDuration = parseFloat(transitionDuration)
97
98

      // Return 0 if element or transition duration is not found
99
      if (!floatTransitionDuration) {
100
101
102
103
104
105
106
107
108
        return 0
      }

      // If multiple durations are defined, take the first
      transitionDuration = transitionDuration.split(',')[0]

      return parseFloat(transitionDuration) * MILLISECONDS_MULTIPLIER
    },

109
    reflow(element) {
Ilias's avatar
Ilias committed
110
      return element.offsetHeight
111
112
    },

fat's avatar
fat committed
113
    triggerTransitionEnd(element) {
114
      $(element).trigger(TRANSITION_END)
fat's avatar
fat committed
115
116
    },

117
    // TODO: Remove in v5
118
    supportsTransitionEnd() {
119
      return Boolean(TRANSITION_END)
fat's avatar
fat committed
120
121
    },

122
123
124
125
    isElement(obj) {
      return (obj[0] || obj).nodeType
    },

fat's avatar
fat committed
126
    typeCheckConfig(componentName, config, configTypes) {
127
      for (const property in configTypes) {
XhmikosR's avatar
XhmikosR committed
128
        if (Object.prototype.hasOwnProperty.call(configTypes, property)) {
129
130
          const expectedTypes = configTypes[property]
          const value         = config[property]
XhmikosR's avatar
XhmikosR committed
131
132
          const valueType     = value && Util.isElement(value)
            ? 'element' : toType(value)
Jacob Thornton's avatar
Jacob Thornton committed
133
134
135
136
137
138
139

          if (!new RegExp(expectedTypes).test(valueType)) {
            throw new Error(
              `${componentName.toUpperCase()}: ` +
              `Option "${property}" provided type "${valueType}" ` +
              `but expected type "${expectedTypes}".`)
          }
fat's avatar
fat committed
140
141
        }
      }
142
    }
fat's avatar
fat committed
143
144
  }

145
146
147
  setTransitionEndSupport()

  return Util
Johann-S's avatar
Johann-S committed
148
})($)
149
150

export default Util