util.js 4.2 KB

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