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.0.0-beta): 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 = (() => {
fat's avatar
fat committed
11
12


13
14
15
16
17
  /**
   * ------------------------------------------------------------------------
   * Private TransitionEnd Helpers
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
18

19
  let transition = false
fat's avatar
fat committed
20

21
22
  const MAX_UID = 1000000

23
24
25
26
27
28
  const TransitionEndEvent = {
    WebkitTransition : 'webkitTransitionEnd',
    MozTransition    : 'transitionend',
    OTransition      : 'oTransitionEnd otransitionend',
    transition       : 'transitionend'
  }
fat's avatar
fat committed
29

fat's avatar
fat committed
30
31
  // shoutout AngusCroll (https://goo.gl/pxwQGp)
  function toType(obj) {
32
    return {}.toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
fat's avatar
fat committed
33
34
35
  }

  function isElement(obj) {
Jacob Thornton's avatar
Jacob Thornton committed
36
    return (obj[0] || obj).nodeType
fat's avatar
fat committed
37
38
  }

39
40
41
42
  function getSpecialTransitionEndEvent() {
    return {
      bindType: transition.end,
      delegateType: transition.end,
Jacob Thornton's avatar
Jacob Thornton committed
43
      handle(event) {
44
        if ($(event.target).is(this)) {
45
          return event.handleObj.handler.apply(this, arguments) // eslint-disable-line prefer-rest-params
46
        }
XhmikosR's avatar
XhmikosR committed
47
        return undefined // eslint-disable-line no-undefined
48
49
50
      }
    }
  }
fat's avatar
fat committed
51

52
53
54
  function transitionEndTest() {
    if (window.QUnit) {
      return false
fat's avatar
fat committed
55
56
    }

57
    const el = document.createElement('bootstrap')
fat's avatar
fat committed
58

59
    for (const name in TransitionEndEvent) {
XhmikosR's avatar
XhmikosR committed
60
      if (typeof el.style[name] !== 'undefined') {
61
62
63
        return {
          end: TransitionEndEvent[name]
        }
64
65
      }
    }
fat's avatar
fat committed
66

67
    return false
fat's avatar
fat committed
68
69
  }

70
71
  function transitionEndEmulator(duration) {
    let called = false
fat's avatar
fat committed
72

Jacob Thornton's avatar
Jacob Thornton committed
73
    $(this).one(Util.TRANSITION_END, () => {
74
75
      called = true
    })
fat's avatar
fat committed
76

77
78
    setTimeout(() => {
      if (!called) {
fat's avatar
fat committed
79
        Util.triggerTransitionEnd(this)
fat's avatar
fat committed
80
      }
81
    }, duration)
fat's avatar
fat committed
82

83
    return this
fat's avatar
fat committed
84
85
  }

86
87
88
89
  function setTransitionEndSupport() {
    transition = transitionEndTest()

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

91
92
    if (Util.supportsTransitionEnd()) {
      $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent()
fat's avatar
fat committed
93
94
95
96
    }
  }


97
98
99
100
101
  /**
   * --------------------------------------------------------------------------
   * Public Util Api
   * --------------------------------------------------------------------------
   */
fat's avatar
fat committed
102

103
  const Util = {
fat's avatar
fat committed
104

105
106
107
    TRANSITION_END: 'bsTransitionEnd',

    getUID(prefix) {
Jacob Thornton's avatar
Jacob Thornton committed
108
      do {
109
        // eslint-disable-next-line no-bitwise
110
        prefix += ~~(Math.random() * MAX_UID) // "~~" acts like a faster Math.floor() here
Jacob Thornton's avatar
Jacob Thornton committed
111
      } while (document.getElementById(prefix))
112
113
      return prefix
    },
fat's avatar
fat committed
114

115
116
    getSelectorFromElement(element) {
      let selector = element.getAttribute('data-target')
117
      if (!selector || selector === '#') {
118
119
120
        selector = element.getAttribute('href') || ''
      }

121
      try {
Johann-S's avatar
Johann-S committed
122
        const $selector = $(document).find(selector)
123
124
125
126
        return $selector.length > 0 ? selector : null
      } catch (error) {
        return null
      }
127
    },
fat's avatar
fat committed
128

129
    reflow(element) {
Ilias's avatar
Ilias committed
130
      return element.offsetHeight
131
132
    },

fat's avatar
fat committed
133
134
135
136
    triggerTransitionEnd(element) {
      $(element).trigger(transition.end)
    },

137
    supportsTransitionEnd() {
Jacob Thornton's avatar
Jacob Thornton committed
138
      return Boolean(transition)
fat's avatar
fat committed
139
140
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
146
147
          const expectedTypes = configTypes[property]
          const value         = config[property]
          const valueType     = value && 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
163
  setTransitionEndSupport()

  return Util

fat's avatar
fat committed
164
})(jQuery)
165
166

export default Util