util.js 4.03 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
      if (!selector || selector === '#') {
81
82
83
        selector = element.getAttribute('href') || ''
      }

84
      try {
85
        return document.querySelector(selector) ? selector : null
XhmikosR's avatar
XhmikosR committed
86
      } catch (err) {
87
88
        return null
      }
89
    },
fat's avatar
fat committed
90

91
    getTransitionDurationFromElement(element) {
92
93
94
95
      if (!element) {
        return 0
      }

96
97
      // Get transition-duration of the element
      let transitionDuration = $(element).css('transition-duration')
98
      const floatTransitionDuration = parseFloat(transitionDuration)
99
100

      // Return 0 if element or transition duration is not found
101
      if (!floatTransitionDuration) {
102
103
104
105
106
107
108
109
110
        return 0
      }

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

      return parseFloat(transitionDuration) * MILLISECONDS_MULTIPLIER
    },

111
    reflow(element) {
Ilias's avatar
Ilias committed
112
      return element.offsetHeight
113
114
    },

fat's avatar
fat committed
115
    triggerTransitionEnd(element) {
116
      $(element).trigger(TRANSITION_END)
fat's avatar
fat committed
117
118
    },

119
    // TODO: Remove in v5
120
    supportsTransitionEnd() {
121
      return Boolean(TRANSITION_END)
fat's avatar
fat committed
122
123
    },

124
125
126
127
    isElement(obj) {
      return (obj[0] || obj).nodeType
    },

fat's avatar
fat committed
128
    typeCheckConfig(componentName, config, configTypes) {
129
      for (const property in configTypes) {
XhmikosR's avatar
XhmikosR committed
130
        if (Object.prototype.hasOwnProperty.call(configTypes, property)) {
131
132
          const expectedTypes = configTypes[property]
          const value         = config[property]
XhmikosR's avatar
XhmikosR committed
133
134
          const valueType     = value && Util.isElement(value)
            ? 'element' : toType(value)
Jacob Thornton's avatar
Jacob Thornton committed
135
136
137
138
139
140
141

          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
142
143
        }
      }
144
    }
fat's avatar
fat committed
145
146
  }

147
148
149
  setTransitionEndSupport()

  return Util
Johann-S's avatar
Johann-S committed
150
})($)
151
152

export default Util