util.js 4.25 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.0.0): 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
  let transition = false
fat's avatar
fat committed
18

19
  const MAX_UID = 1000000
20
  const MILLISECONDS_MULTIPLIER = 1000
21

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

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

40
  function transitionEndTest() {
41
    if (typeof window !== 'undefined' && window.QUnit) {
42
      return false
fat's avatar
fat committed
43
44
    }

45
46
    return {
      end: 'transitionend'
47
    }
fat's avatar
fat committed
48
49
  }

50
51
  function transitionEndEmulator(duration) {
    let called = false
fat's avatar
fat committed
52

Jacob Thornton's avatar
Jacob Thornton committed
53
    $(this).one(Util.TRANSITION_END, () => {
54
55
      called = true
    })
fat's avatar
fat committed
56

57
58
    setTimeout(() => {
      if (!called) {
fat's avatar
fat committed
59
        Util.triggerTransitionEnd(this)
fat's avatar
fat committed
60
      }
61
    }, duration)
fat's avatar
fat committed
62

63
    return this
fat's avatar
fat committed
64
65
  }

66
67
68
69
  function setTransitionEndSupport() {
    transition = transitionEndTest()

    $.fn.emulateTransitionEnd = transitionEndEmulator
fat's avatar
fat committed
70

71
72
    if (Util.supportsTransitionEnd()) {
      $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent()
fat's avatar
fat committed
73
74
75
    }
  }

76
77
78
79
80
  /**
   * --------------------------------------------------------------------------
   * Public Util Api
   * --------------------------------------------------------------------------
   */
fat's avatar
fat committed
81

82
  const Util = {
fat's avatar
fat committed
83

84
85
86
    TRANSITION_END: 'bsTransitionEnd',

    getUID(prefix) {
Jacob Thornton's avatar
Jacob Thornton committed
87
      do {
88
        // eslint-disable-next-line no-bitwise
89
        prefix += ~~(Math.random() * MAX_UID) // "~~" acts like a faster Math.floor() here
Jacob Thornton's avatar
Jacob Thornton committed
90
      } while (document.getElementById(prefix))
91
92
      return prefix
    },
fat's avatar
fat committed
93

94
95
    getSelectorFromElement(element) {
      let selector = element.getAttribute('data-target')
96
      if (!selector || selector === '#') {
97
98
99
        selector = element.getAttribute('href') || ''
      }

100
      try {
Johann-S's avatar
Johann-S committed
101
        const $selector = $(document).find(selector)
102
        return $selector.length > 0 ? selector : null
XhmikosR's avatar
XhmikosR committed
103
      } catch (err) {
104
105
        return null
      }
106
    },
fat's avatar
fat committed
107

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    getTransitionDurationFromElement(element) {
      // Get transition-duration of the element
      let transitionDuration = $(element).css('transition-duration')

      // Return 0 if element or transition duration is not found
      if (!transitionDuration) {
        return 0
      }

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

      // jQuery always converts transition durations into seconds,
      // so multiply by 1000
      return parseFloat(transitionDuration) * MILLISECONDS_MULTIPLIER
    },

125
    reflow(element) {
Ilias's avatar
Ilias committed
126
      return element.offsetHeight
127
128
    },

fat's avatar
fat committed
129
130
131
132
    triggerTransitionEnd(element) {
      $(element).trigger(transition.end)
    },

133
    supportsTransitionEnd() {
Jacob Thornton's avatar
Jacob Thornton committed
134
      return Boolean(transition)
fat's avatar
fat committed
135
136
    },

137
138
139
140
    isElement(obj) {
      return (obj[0] || obj).nodeType
    },

fat's avatar
fat committed
141
    typeCheckConfig(componentName, config, configTypes) {
142
      for (const property in configTypes) {
XhmikosR's avatar
XhmikosR committed
143
        if (Object.prototype.hasOwnProperty.call(configTypes, property)) {
144
145
          const expectedTypes = configTypes[property]
          const value         = config[property]
XhmikosR's avatar
XhmikosR committed
146
147
          const valueType     = value && Util.isElement(value)
            ? 'element' : toType(value)
Jacob Thornton's avatar
Jacob Thornton committed
148
149
150
151
152
153
154

          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
155
156
        }
      }
157
    }
fat's avatar
fat committed
158
159
  }

160
161
162
  setTransitionEndSupport()

  return Util
Johann-S's avatar
Johann-S committed
163
})($)
164
165

export default Util