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

19
20
  const MAX_UID = 1000000

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

26
27
28
29
  function getSpecialTransitionEndEvent() {
    return {
      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
  function transitionEndTest() {
40
    if (typeof window !== 'undefined' && window.QUnit) {
41
      return false
fat's avatar
fat committed
42
43
    }

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

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

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

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

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

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

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

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

75
  function escapeId(selector) {
XhmikosR's avatar
XhmikosR committed
76
    // We escape IDs in case of special selectors (selector = '#myId:something')
77
    // $.escapeSelector does not exist in jQuery < 3
XhmikosR's avatar
XhmikosR committed
78
79
    selector = typeof $.escapeSelector === 'function' ? $.escapeSelector(selector).substr(1)
      : selector.replace(/(:|\.|\[|\]|,|=|@)/g, '\\$1')
80
81
82

    return selector
  }
fat's avatar
fat committed
83

84
85
86
87
88
  /**
   * --------------------------------------------------------------------------
   * Public Util Api
   * --------------------------------------------------------------------------
   */
fat's avatar
fat committed
89

90
  const Util = {
fat's avatar
fat committed
91

92
93
94
    TRANSITION_END: 'bsTransitionEnd',

    getUID(prefix) {
Jacob Thornton's avatar
Jacob Thornton committed
95
      do {
96
        // eslint-disable-next-line no-bitwise
97
        prefix += ~~(Math.random() * MAX_UID) // "~~" acts like a faster Math.floor() here
Jacob Thornton's avatar
Jacob Thornton committed
98
      } while (document.getElementById(prefix))
99
100
      return prefix
    },
fat's avatar
fat committed
101

102
103
    getSelectorFromElement(element) {
      let selector = element.getAttribute('data-target')
104
      if (!selector || selector === '#') {
105
106
107
        selector = element.getAttribute('href') || ''
      }

XhmikosR's avatar
XhmikosR committed
108
      // If it's an ID
109
110
111
112
      if (selector.charAt(0) === '#') {
        selector = escapeId(selector)
      }

113
      try {
Johann-S's avatar
Johann-S committed
114
        const $selector = $(document).find(selector)
115
        return $selector.length > 0 ? selector : null
XhmikosR's avatar
XhmikosR committed
116
      } catch (err) {
117
118
        return null
      }
119
    },
fat's avatar
fat committed
120

121
    reflow(element) {
Ilias's avatar
Ilias committed
122
      return element.offsetHeight
123
124
    },

fat's avatar
fat committed
125
126
127
128
    triggerTransitionEnd(element) {
      $(element).trigger(transition.end)
    },

129
    supportsTransitionEnd() {
Jacob Thornton's avatar
Jacob Thornton committed
130
      return Boolean(transition)
fat's avatar
fat committed
131
132
    },

133
134
135
136
    isElement(obj) {
      return (obj[0] || obj).nodeType
    },

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

          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
151
152
        }
      }
153
    }
fat's avatar
fat committed
154
155
  }

156
157
158
  setTransitionEndSupport()

  return Util
Johann-S's avatar
Johann-S committed
159
})($)
160
161

export default Util