Layout von Websiten mit Bootstrap und Foundation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * --------------------------------------------------------------------------
  3. * Bootstrap (v4.5.0): util.js
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  5. * --------------------------------------------------------------------------
  6. */
  7. import $ from 'jquery'
  8. /**
  9. * ------------------------------------------------------------------------
  10. * Private TransitionEnd Helpers
  11. * ------------------------------------------------------------------------
  12. */
  13. const TRANSITION_END = 'transitionend'
  14. const MAX_UID = 1000000
  15. const MILLISECONDS_MULTIPLIER = 1000
  16. // Shoutout AngusCroll (https://goo.gl/pxwQGp)
  17. function toType(obj) {
  18. if (obj === null || typeof obj === 'undefined') {
  19. return `${obj}`
  20. }
  21. return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase()
  22. }
  23. function getSpecialTransitionEndEvent() {
  24. return {
  25. bindType: TRANSITION_END,
  26. delegateType: TRANSITION_END,
  27. handle(event) {
  28. if ($(event.target).is(this)) {
  29. return event.handleObj.handler.apply(this, arguments) // eslint-disable-line prefer-rest-params
  30. }
  31. return undefined
  32. }
  33. }
  34. }
  35. function transitionEndEmulator(duration) {
  36. let called = false
  37. $(this).one(Util.TRANSITION_END, () => {
  38. called = true
  39. })
  40. setTimeout(() => {
  41. if (!called) {
  42. Util.triggerTransitionEnd(this)
  43. }
  44. }, duration)
  45. return this
  46. }
  47. function setTransitionEndSupport() {
  48. $.fn.emulateTransitionEnd = transitionEndEmulator
  49. $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent()
  50. }
  51. /**
  52. * --------------------------------------------------------------------------
  53. * Public Util Api
  54. * --------------------------------------------------------------------------
  55. */
  56. const Util = {
  57. TRANSITION_END: 'bsTransitionEnd',
  58. getUID(prefix) {
  59. do {
  60. // eslint-disable-next-line no-bitwise
  61. prefix += ~~(Math.random() * MAX_UID) // "~~" acts like a faster Math.floor() here
  62. } while (document.getElementById(prefix))
  63. return prefix
  64. },
  65. getSelectorFromElement(element) {
  66. let selector = element.getAttribute('data-target')
  67. if (!selector || selector === '#') {
  68. const hrefAttr = element.getAttribute('href')
  69. selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : ''
  70. }
  71. try {
  72. return document.querySelector(selector) ? selector : null
  73. } catch (err) {
  74. return null
  75. }
  76. },
  77. getTransitionDurationFromElement(element) {
  78. if (!element) {
  79. return 0
  80. }
  81. // Get transition-duration of the element
  82. let transitionDuration = $(element).css('transition-duration')
  83. let transitionDelay = $(element).css('transition-delay')
  84. const floatTransitionDuration = parseFloat(transitionDuration)
  85. const floatTransitionDelay = parseFloat(transitionDelay)
  86. // Return 0 if element or transition duration is not found
  87. if (!floatTransitionDuration && !floatTransitionDelay) {
  88. return 0
  89. }
  90. // If multiple durations are defined, take the first
  91. transitionDuration = transitionDuration.split(',')[0]
  92. transitionDelay = transitionDelay.split(',')[0]
  93. return (parseFloat(transitionDuration) + parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER
  94. },
  95. reflow(element) {
  96. return element.offsetHeight
  97. },
  98. triggerTransitionEnd(element) {
  99. $(element).trigger(TRANSITION_END)
  100. },
  101. // TODO: Remove in v5
  102. supportsTransitionEnd() {
  103. return Boolean(TRANSITION_END)
  104. },
  105. isElement(obj) {
  106. return (obj[0] || obj).nodeType
  107. },
  108. typeCheckConfig(componentName, config, configTypes) {
  109. for (const property in configTypes) {
  110. if (Object.prototype.hasOwnProperty.call(configTypes, property)) {
  111. const expectedTypes = configTypes[property]
  112. const value = config[property]
  113. const valueType = value && Util.isElement(value)
  114. ? 'element' : toType(value)
  115. if (!new RegExp(expectedTypes).test(valueType)) {
  116. throw new Error(
  117. `${componentName.toUpperCase()}: ` +
  118. `Option "${property}" provided type "${valueType}" ` +
  119. `but expected type "${expectedTypes}".`)
  120. }
  121. }
  122. }
  123. },
  124. findShadowRoot(element) {
  125. if (!document.documentElement.attachShadow) {
  126. return null
  127. }
  128. // Can find the shadow root otherwise it'll return the document
  129. if (typeof element.getRootNode === 'function') {
  130. const root = element.getRootNode()
  131. return root instanceof ShadowRoot ? root : null
  132. }
  133. if (element instanceof ShadowRoot) {
  134. return element
  135. }
  136. // when we don't find a shadow root
  137. if (!element.parentNode) {
  138. return null
  139. }
  140. return Util.findShadowRoot(element.parentNode)
  141. },
  142. jQueryDetection() {
  143. if (typeof $ === 'undefined') {
  144. throw new TypeError('Bootstrap\'s JavaScript requires jQuery. jQuery must be included before Bootstrap\'s JavaScript.')
  145. }
  146. const version = $.fn.jquery.split(' ')[0].split('.')
  147. const minMajor = 1
  148. const ltMajor = 2
  149. const minMinor = 9
  150. const minPatch = 1
  151. const maxMajor = 4
  152. if (version[0] < ltMajor && version[1] < minMinor || version[0] === minMajor && version[1] === minMinor && version[2] < minPatch || version[0] >= maxMajor) {
  153. throw new Error('Bootstrap\'s JavaScript requires at least jQuery v1.9.1 but less than v4.0.0')
  154. }
  155. }
  156. }
  157. Util.jQueryDetection()
  158. setTransitionEndSupport()
  159. export default Util