0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-11 03:52:27 +01:00
Bootstrap/js/dist/util.js

132 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

2015-05-07 12:48:22 -07:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.1.0): util.js
2015-05-07 12:48:22 -07:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
2017-10-25 12:31:55 -07:00
var Util = function ($) {
/**
* ------------------------------------------------------------------------
* Private TransitionEnd Helpers
* ------------------------------------------------------------------------
*/
2018-03-31 13:59:37 -07:00
var TRANSITION_END = 'transitionend';
var MAX_UID = 1000000;
var MILLISECONDS_MULTIPLIER = 1000; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
2015-05-13 14:46:50 -07:00
2017-09-30 14:28:03 -07:00
function toType(obj) {
2018-02-11 14:53:29 -08:00
return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
2015-05-13 14:46:50 -07:00
}
function getSpecialTransitionEndEvent() {
return {
2018-03-31 13:59:37 -07:00
bindType: TRANSITION_END,
delegateType: TRANSITION_END,
handle: function handle(event) {
if ($(event.target).is(this)) {
2017-07-02 10:40:27 -07:00
return event.handleObj.handler.apply(this, arguments); // eslint-disable-line prefer-rest-params
}
2017-09-30 14:28:03 -07:00
2017-09-05 21:05:12 -07:00
return undefined; // eslint-disable-line no-undefined
}
};
}
2015-05-07 12:48:22 -07:00
function transitionEndEmulator(duration) {
var _this = this;
2015-05-07 12:48:22 -07:00
var called = false;
$(this).one(Util.TRANSITION_END, function () {
called = true;
});
setTimeout(function () {
if (!called) {
2015-05-07 22:26:40 -07:00
Util.triggerTransitionEnd(_this);
2015-05-07 12:48:22 -07:00
}
}, duration);
return this;
2015-05-07 12:48:22 -07:00
}
function setTransitionEndSupport() {
$.fn.emulateTransitionEnd = transitionEndEmulator;
2018-03-31 13:59:37 -07:00
$.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent();
2015-05-07 12:48:22 -07:00
}
/**
* --------------------------------------------------------------------------
* Public Util Api
* --------------------------------------------------------------------------
*/
2015-05-07 12:48:22 -07:00
2017-09-30 14:28:03 -07:00
var Util = {
TRANSITION_END: 'bsTransitionEnd',
getUID: function getUID(prefix) {
2015-08-18 20:28:28 -07:00
do {
2016-11-25 15:00:23 -08:00
// eslint-disable-next-line no-bitwise
2016-10-09 17:26:51 -07:00
prefix += ~~(Math.random() * MAX_UID); // "~~" acts like a faster Math.floor() here
2015-08-18 20:28:28 -07:00
} while (document.getElementById(prefix));
2017-09-30 14:28:03 -07:00
return prefix;
},
getSelectorFromElement: function getSelectorFromElement(element) {
var selector = element.getAttribute('data-target');
2017-09-30 14:28:03 -07:00
2017-03-19 19:03:32 -07:00
if (!selector || selector === '#') {
selector = element.getAttribute('href') || '';
}
2015-05-07 12:48:22 -07:00
2017-03-19 19:03:32 -07:00
try {
2017-09-05 21:05:12 -07:00
var $selector = $(document).find(selector);
2017-03-19 19:03:32 -07:00
return $selector.length > 0 ? selector : null;
2018-01-11 22:42:40 -08:00
} catch (err) {
2017-03-19 19:03:32 -07:00
return null;
}
},
2018-03-31 13:59:37 -07:00
getTransitionDurationFromElement: function getTransitionDurationFromElement(element) {
if (!element) {
return 0;
} // Get transition-duration of the element
var transitionDuration = $(element).css('transition-duration');
var floatTransitionDuration = parseFloat(transitionDuration); // Return 0 if element or transition duration is not found
if (!floatTransitionDuration) {
return 0;
} // If multiple durations are defined, take the first
transitionDuration = transitionDuration.split(',')[0];
return parseFloat(transitionDuration) * MILLISECONDS_MULTIPLIER;
},
reflow: function reflow(element) {
2016-10-30 15:21:53 -07:00
return element.offsetHeight;
},
2015-05-07 22:26:40 -07:00
triggerTransitionEnd: function triggerTransitionEnd(element) {
2018-03-31 13:59:37 -07:00
$(element).trigger(TRANSITION_END);
2015-05-07 22:26:40 -07:00
},
2018-03-31 13:59:37 -07:00
// TODO: Remove in v5
supportsTransitionEnd: function supportsTransitionEnd() {
2018-03-31 13:59:37 -07:00
return Boolean(TRANSITION_END);
2015-05-13 14:46:50 -07:00
},
2017-09-30 14:28:03 -07:00
isElement: function isElement(obj) {
return (obj[0] || obj).nodeType;
},
2015-05-13 14:46:50 -07:00
typeCheckConfig: function typeCheckConfig(componentName, config, configTypes) {
for (var property in configTypes) {
2017-09-05 21:05:12 -07:00
if (Object.prototype.hasOwnProperty.call(configTypes, property)) {
2015-08-18 20:28:28 -07:00
var expectedTypes = configTypes[property];
var value = config[property];
2017-09-30 14:28:03 -07:00
var valueType = value && Util.isElement(value) ? 'element' : toType(value);
2015-08-18 20:28:28 -07:00
if (!new RegExp(expectedTypes).test(valueType)) {
2017-09-30 14:28:03 -07:00
throw new Error(componentName.toUpperCase() + ": " + ("Option \"" + property + "\" provided type \"" + valueType + "\" ") + ("but expected type \"" + expectedTypes + "\"."));
2015-08-18 20:28:28 -07:00
}
2015-05-13 14:46:50 -07:00
}
}
}
};
setTransitionEndSupport();
return Util;
2017-10-15 15:51:44 -07:00
}($);
2017-04-21 23:58:09 -07:00
//# sourceMappingURL=util.js.map