util.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * --------------------------------------------------------------------------
  3. * Bootstrap (v4.1.1): util.js
  4. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  5. * --------------------------------------------------------------------------
  6. */
  7. var Util = function ($) {
  8. /**
  9. * ------------------------------------------------------------------------
  10. * Private TransitionEnd Helpers
  11. * ------------------------------------------------------------------------
  12. */
  13. var TRANSITION_END = 'transitionend';
  14. var MAX_UID = 1000000;
  15. var MILLISECONDS_MULTIPLIER = 1000; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
  16. function toType(obj) {
  17. return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
  18. }
  19. function getSpecialTransitionEndEvent() {
  20. return {
  21. bindType: TRANSITION_END,
  22. delegateType: TRANSITION_END,
  23. handle: function handle(event) {
  24. if ($(event.target).is(this)) {
  25. return event.handleObj.handler.apply(this, arguments); // eslint-disable-line prefer-rest-params
  26. }
  27. return undefined; // eslint-disable-line no-undefined
  28. }
  29. };
  30. }
  31. function transitionEndEmulator(duration) {
  32. var _this = this;
  33. var called = false;
  34. $(this).one(Util.TRANSITION_END, function () {
  35. called = true;
  36. });
  37. setTimeout(function () {
  38. if (!called) {
  39. Util.triggerTransitionEnd(_this);
  40. }
  41. }, duration);
  42. return this;
  43. }
  44. function setTransitionEndSupport() {
  45. $.fn.emulateTransitionEnd = transitionEndEmulator;
  46. $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent();
  47. }
  48. /**
  49. * --------------------------------------------------------------------------
  50. * Public Util Api
  51. * --------------------------------------------------------------------------
  52. */
  53. var Util = {
  54. TRANSITION_END: 'bsTransitionEnd',
  55. getUID: function getUID(prefix) {
  56. do {
  57. // eslint-disable-next-line no-bitwise
  58. prefix += ~~(Math.random() * MAX_UID); // "~~" acts like a faster Math.floor() here
  59. } while (document.getElementById(prefix));
  60. return prefix;
  61. },
  62. getSelectorFromElement: function getSelectorFromElement(element) {
  63. var selector = element.getAttribute('data-target');
  64. if (!selector || selector === '#') {
  65. selector = element.getAttribute('href') || '';
  66. }
  67. try {
  68. var $selector = $(document).find(selector);
  69. return $selector.length > 0 ? selector : null;
  70. } catch (err) {
  71. return null;
  72. }
  73. },
  74. getTransitionDurationFromElement: function getTransitionDurationFromElement(element) {
  75. if (!element) {
  76. return 0;
  77. } // Get transition-duration of the element
  78. var transitionDuration = $(element).css('transition-duration');
  79. var floatTransitionDuration = parseFloat(transitionDuration); // Return 0 if element or transition duration is not found
  80. if (!floatTransitionDuration) {
  81. return 0;
  82. } // If multiple durations are defined, take the first
  83. transitionDuration = transitionDuration.split(',')[0];
  84. return parseFloat(transitionDuration) * MILLISECONDS_MULTIPLIER;
  85. },
  86. reflow: function reflow(element) {
  87. return element.offsetHeight;
  88. },
  89. triggerTransitionEnd: function triggerTransitionEnd(element) {
  90. $(element).trigger(TRANSITION_END);
  91. },
  92. // TODO: Remove in v5
  93. supportsTransitionEnd: function supportsTransitionEnd() {
  94. return Boolean(TRANSITION_END);
  95. },
  96. isElement: function isElement(obj) {
  97. return (obj[0] || obj).nodeType;
  98. },
  99. typeCheckConfig: function typeCheckConfig(componentName, config, configTypes) {
  100. for (var property in configTypes) {
  101. if (Object.prototype.hasOwnProperty.call(configTypes, property)) {
  102. var expectedTypes = configTypes[property];
  103. var value = config[property];
  104. var valueType = value && Util.isElement(value) ? 'element' : toType(value);
  105. if (!new RegExp(expectedTypes).test(valueType)) {
  106. throw new Error(componentName.toUpperCase() + ": " + ("Option \"" + property + "\" provided type \"" + valueType + "\" ") + ("but expected type \"" + expectedTypes + "\"."));
  107. }
  108. }
  109. }
  110. }
  111. };
  112. setTransitionEndSupport();
  113. return Util;
  114. }($);
  115. //# sourceMappingURL=util.js.map