util.js 4.4 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.2): 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
  const TransitionEndEvent = {
    WebkitTransition : 'webkitTransitionEnd',
    transition       : 'transitionend'
  }
fat's avatar
fat committed
27

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

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

46
47
48
  function transitionEndTest() {
    if (window.QUnit) {
      return false
fat's avatar
fat committed
49
50
    }

51
    const el = document.createElement('bootstrap')
fat's avatar
fat committed
52

53
    for (const name in TransitionEndEvent) {
XhmikosR's avatar
XhmikosR committed
54
      if (typeof el.style[name] !== 'undefined') {
55
56
57
        return {
          end: TransitionEndEvent[name]
        }
58
59
      }
    }
fat's avatar
fat committed
60

61
    return false
fat's avatar
fat committed
62
63
  }

64
65
  function transitionEndEmulator(duration) {
    let called = false
fat's avatar
fat committed
66

Jacob Thornton's avatar
Jacob Thornton committed
67
    $(this).one(Util.TRANSITION_END, () => {
68
69
      called = true
    })
fat's avatar
fat committed
70

71
72
    setTimeout(() => {
      if (!called) {
fat's avatar
fat committed
73
        Util.triggerTransitionEnd(this)
fat's avatar
fat committed
74
      }
75
    }, duration)
fat's avatar
fat committed
76

77
    return this
fat's avatar
fat committed
78
79
  }

80
81
82
83
  function setTransitionEndSupport() {
    transition = transitionEndTest()

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

85
86
    if (Util.supportsTransitionEnd()) {
      $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent()
fat's avatar
fat committed
87
88
89
    }
  }

90
91
92
93
94
95
96
97
  function escapeId(selector) {
    // we escape IDs in case of special selectors (selector = '#myId:something')
    // $.escapeSelector does not exist in jQuery < 3
    selector = typeof $.escapeSelector === 'function' ? $.escapeSelector(selector).substr(1) :
      selector.replace(/(:|\.|\[|\]|,|=|@)/g, '\\$1')

    return selector
  }
fat's avatar
fat committed
98

99
100
101
102
103
  /**
   * --------------------------------------------------------------------------
   * Public Util Api
   * --------------------------------------------------------------------------
   */
fat's avatar
fat committed
104

105
  const Util = {
fat's avatar
fat committed
106

107
108
109
    TRANSITION_END: 'bsTransitionEnd',

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

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

123
124
125
126
127
      // if it's an ID
      if (selector.charAt(0) === '#') {
        selector = escapeId(selector)
      }

128
      try {
Johann-S's avatar
Johann-S committed
129
        const $selector = $(document).find(selector)
130
131
132
133
        return $selector.length > 0 ? selector : null
      } catch (error) {
        return null
      }
134
    },
fat's avatar
fat committed
135

136
    reflow(element) {
Ilias's avatar
Ilias committed
137
      return element.offsetHeight
138
139
    },

fat's avatar
fat committed
140
141
142
143
    triggerTransitionEnd(element) {
      $(element).trigger(transition.end)
    },

144
    supportsTransitionEnd() {
Jacob Thornton's avatar
Jacob Thornton committed
145
      return Boolean(transition)
fat's avatar
fat committed
146
147
    },

148
149
150
151
    isElement(obj) {
      return (obj[0] || obj).nodeType
    },

fat's avatar
fat committed
152
    typeCheckConfig(componentName, config, configTypes) {
153
      for (const property in configTypes) {
XhmikosR's avatar
XhmikosR committed
154
        if (Object.prototype.hasOwnProperty.call(configTypes, property)) {
155
156
          const expectedTypes = configTypes[property]
          const value         = config[property]
157
          const valueType     = value && Util.isElement(value) ?
158
                                'element' : toType(value)
Jacob Thornton's avatar
Jacob Thornton committed
159
160
161
162
163
164
165

          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
166
167
        }
      }
168
    }
fat's avatar
fat committed
169
170
  }

171
172
173
174
  setTransitionEndSupport()

  return Util

Johann-S's avatar
Johann-S committed
175
})($)
176
177

export default Util