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 = (($) => {
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

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

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

41
42
43
  function transitionEndTest() {
    if (window.QUnit) {
      return false
fat's avatar
fat committed
44
45
    }

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

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

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

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

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

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

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

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

77
78
79
80
81
82
83
84
  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
85

86
87
88
89
90
  /**
   * --------------------------------------------------------------------------
   * Public Util Api
   * --------------------------------------------------------------------------
   */
fat's avatar
fat committed
91

92
  const Util = {
fat's avatar
fat committed
93

94
95
96
    TRANSITION_END: 'bsTransitionEnd',

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

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

110
111
112
113
114
      // if it's an ID
      if (selector.charAt(0) === '#') {
        selector = escapeId(selector)
      }

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

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

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

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

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

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

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

158
159
160
161
  setTransitionEndSupport()

  return Util

Johann-S's avatar
Johann-S committed
162
})($)
163
164

export default Util