util.js 4.26 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.1.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
  const TRANSITION_END = 'transitionend'
18
  const MAX_UID = 1000000
19
  const MILLISECONDS_MULTIPLIER = 1000
20

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

26
27
  function getSpecialTransitionEndEvent() {
    return {
28
29
      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
40
  function transitionEndEmulator(duration) {
    let called = false
fat's avatar
fat committed
41

Jacob Thornton's avatar
Jacob Thornton committed
42
    $(this).one(Util.TRANSITION_END, () => {
43
44
      called = true
    })
fat's avatar
fat committed
45

46
47
    setTimeout(() => {
      if (!called) {
fat's avatar
fat committed
48
        Util.triggerTransitionEnd(this)
fat's avatar
fat committed
49
      }
50
    }, duration)
fat's avatar
fat committed
51

52
    return this
fat's avatar
fat committed
53
54
  }

55
56
  function setTransitionEndSupport() {
    $.fn.emulateTransitionEnd = transitionEndEmulator
57
    $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent()
fat's avatar
fat committed
58
59
  }

60
61
62
63
64
  /**
   * --------------------------------------------------------------------------
   * Public Util Api
   * --------------------------------------------------------------------------
   */
fat's avatar
fat committed
65

66
  const Util = {
fat's avatar
fat committed
67

68
69
70
    TRANSITION_END: 'bsTransitionEnd',

    getUID(prefix) {
Jacob Thornton's avatar
Jacob Thornton committed
71
      do {
72
        // eslint-disable-next-line no-bitwise
73
        prefix += ~~(Math.random() * MAX_UID) // "~~" acts like a faster Math.floor() here
Jacob Thornton's avatar
Jacob Thornton committed
74
      } while (document.getElementById(prefix))
75
76
      return prefix
    },
fat's avatar
fat committed
77

78
79
    getSelectorFromElement(element) {
      let selector = element.getAttribute('data-target')
80
81
      let method = 'querySelector'

82
      if (!selector || selector === '#') {
83
84
85
86
        selector = (element.getAttribute('href') || '').trim()
      }

      const validSelector = selector
87
      if (selector.charAt(0) === '#' && selector.indexOf(',') === -1) {
88
89
        selector = selector.substr(1)
        method = 'getElementById'
90
91
      }

92
      try {
93
        return document[method](selector) ? validSelector : null
XhmikosR's avatar
XhmikosR committed
94
      } catch (err) {
95
96
        return null
      }
97
    },
fat's avatar
fat committed
98

99
    getTransitionDurationFromElement(element) {
100
101
102
103
      if (!element) {
        return 0
      }

104
105
      // Get transition-duration of the element
      let transitionDuration = $(element).css('transition-duration')
106
      const floatTransitionDuration = parseFloat(transitionDuration)
107
108

      // Return 0 if element or transition duration is not found
109
      if (!floatTransitionDuration) {
110
111
112
113
114
115
116
117
118
        return 0
      }

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

      return parseFloat(transitionDuration) * MILLISECONDS_MULTIPLIER
    },

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

fat's avatar
fat committed
123
    triggerTransitionEnd(element) {
124
      $(element).trigger(TRANSITION_END)
fat's avatar
fat committed
125
126
    },

127
    // TODO: Remove in v5
128
    supportsTransitionEnd() {
129
      return Boolean(TRANSITION_END)
fat's avatar
fat committed
130
131
    },

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

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

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

155
156
157
  setTransitionEndSupport()

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

export default Util