util.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * --------------------------------------------------------------------------
  3. * Bootstrap (v4.0.0-alpha.2): util.js
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  5. * --------------------------------------------------------------------------
  6. */
  7. const Util = (($) => {
  8. /**
  9. * ------------------------------------------------------------------------
  10. * Private TransitionEnd Helpers
  11. * ------------------------------------------------------------------------
  12. */
  13. let transition = false
  14. const TransitionEndEvent = {
  15. WebkitTransition : 'webkitTransitionEnd',
  16. MozTransition : 'transitionend',
  17. OTransition : 'oTransitionEnd otransitionend',
  18. transition : 'transitionend'
  19. }
  20. // shoutout AngusCroll (https://goo.gl/pxwQGp)
  21. function toType(obj) {
  22. return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
  23. }
  24. function isElement(obj) {
  25. return (obj[0] || obj).nodeType
  26. }
  27. function getSpecialTransitionEndEvent() {
  28. return {
  29. bindType: transition.end,
  30. delegateType: transition.end,
  31. handle(event) {
  32. if ($(event.target).is(this)) {
  33. return event.handleObj.handler.apply(this, arguments)
  34. }
  35. }
  36. }
  37. }
  38. function transitionEndTest() {
  39. if (window.QUnit) {
  40. return false
  41. }
  42. let el = document.createElement('bootstrap')
  43. for (let name in TransitionEndEvent) {
  44. if (el.style[name] !== undefined) {
  45. return { end: TransitionEndEvent[name] }
  46. }
  47. }
  48. return false
  49. }
  50. function transitionEndEmulator(duration) {
  51. let called = false
  52. $(this).one(Util.TRANSITION_END, () => {
  53. called = true
  54. })
  55. setTimeout(() => {
  56. if (!called) {
  57. Util.triggerTransitionEnd(this)
  58. }
  59. }, duration)
  60. return this
  61. }
  62. function setTransitionEndSupport() {
  63. transition = transitionEndTest()
  64. $.fn.emulateTransitionEnd = transitionEndEmulator
  65. if (Util.supportsTransitionEnd()) {
  66. $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent()
  67. }
  68. }
  69. /**
  70. * --------------------------------------------------------------------------
  71. * Public Util Api
  72. * --------------------------------------------------------------------------
  73. */
  74. let Util = {
  75. TRANSITION_END: 'bsTransitionEnd',
  76. getUID(prefix) {
  77. do {
  78. prefix += ~~(Math.random() * 1000000) // "~~" acts like a faster Math.floor() here
  79. } while (document.getElementById(prefix))
  80. return prefix
  81. },
  82. getSelectorFromElement(element) {
  83. let selector = element.getAttribute('data-target')
  84. if (!selector) {
  85. selector = element.getAttribute('href') || ''
  86. selector = /^#[a-z]/i.test(selector) ? selector : null
  87. }
  88. return selector
  89. },
  90. reflow(element) {
  91. new Function('bs', 'return bs')(element.offsetHeight)
  92. },
  93. triggerTransitionEnd(element) {
  94. $(element).trigger(transition.end)
  95. },
  96. supportsTransitionEnd() {
  97. return Boolean(transition)
  98. },
  99. typeCheckConfig(componentName, config, configTypes) {
  100. for (let property in configTypes) {
  101. if (configTypes.hasOwnProperty(property)) {
  102. let expectedTypes = configTypes[property]
  103. let value = config[property]
  104. let valueType
  105. if (value && isElement(value)) {
  106. valueType = 'element'
  107. } else {
  108. valueType = toType(value)
  109. }
  110. if (!new RegExp(expectedTypes).test(valueType)) {
  111. throw new Error(
  112. `${componentName.toUpperCase()}: ` +
  113. `Option "${property}" provided type "${valueType}" ` +
  114. `but expected type "${expectedTypes}".`)
  115. }
  116. }
  117. }
  118. }
  119. }
  120. setTransitionEndSupport()
  121. return Util
  122. })(jQuery)
  123. export default Util