UNPKG

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