twbs--bootstrap/js/dist/util.js

142 lines
4.2 KiB
JavaScript
Raw Normal View History

2015-05-07 19:48:22 +00:00
/**
* --------------------------------------------------------------------------
2017-10-19 16:40:37 +00:00
* Bootstrap (v4.0.0-beta.2): util.js
2015-05-07 19:48:22 +00:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
2017-09-06 04:05:12 +00:00
var Util = function () {
/**
* ------------------------------------------------------------------------
* Private TransitionEnd Helpers
* ------------------------------------------------------------------------
*/
var transition = false;
2016-06-05 01:21:15 +00:00
var MAX_UID = 1000000;
var TransitionEndEvent = {
WebkitTransition: 'webkitTransitionEnd',
MozTransition: 'transitionend',
OTransition: 'oTransitionEnd otransitionend',
2017-09-30 21:28:03 +00:00
transition: 'transitionend' // shoutout AngusCroll (https://goo.gl/pxwQGp)
2017-09-30 21:28:03 +00:00
};
2015-05-13 21:46:50 +00:00
2017-09-30 21:28:03 +00:00
function toType(obj) {
return {}.toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
2015-05-13 21:46:50 +00:00
}
function getSpecialTransitionEndEvent() {
return {
bindType: transition.end,
delegateType: transition.end,
handle: function handle(event) {
if ($(event.target).is(this)) {
2017-07-02 17:40:27 +00:00
return event.handleObj.handler.apply(this, arguments); // eslint-disable-line prefer-rest-params
}
2017-09-30 21:28:03 +00:00
2017-09-06 04:05:12 +00:00
return undefined; // eslint-disable-line no-undefined
}
};
}
2015-05-07 19:48:22 +00:00
function transitionEndTest() {
if (window.QUnit) {
return false;
2015-05-07 19:48:22 +00:00
}
var el = document.createElement('bootstrap');
2015-05-07 19:48:22 +00:00
2016-10-10 00:26:51 +00:00
for (var name in TransitionEndEvent) {
2017-09-06 04:05:12 +00:00
if (typeof el.style[name] !== 'undefined') {
2016-11-25 23:00:23 +00:00
return {
end: TransitionEndEvent[name]
};
}
}
2015-05-07 19:48:22 +00:00
return false;
2015-05-07 19:48:22 +00:00
}
function transitionEndEmulator(duration) {
var _this = this;
2015-05-07 19:48:22 +00:00
var called = false;
$(this).one(Util.TRANSITION_END, function () {
called = true;
});
setTimeout(function () {
if (!called) {
2015-05-08 05:26:40 +00:00
Util.triggerTransitionEnd(_this);
2015-05-07 19:48:22 +00:00
}
}, duration);
return this;
2015-05-07 19:48:22 +00:00
}
function setTransitionEndSupport() {
transition = transitionEndTest();
$.fn.emulateTransitionEnd = transitionEndEmulator;
if (Util.supportsTransitionEnd()) {
$.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent();
2015-05-07 19:48:22 +00:00
}
}
/**
* --------------------------------------------------------------------------
* Public Util Api
* --------------------------------------------------------------------------
*/
2015-05-07 19:48:22 +00:00
2017-09-30 21:28:03 +00:00
var Util = {
TRANSITION_END: 'bsTransitionEnd',
getUID: function getUID(prefix) {
2015-08-19 03:28:28 +00:00
do {
2016-11-25 23:00:23 +00:00
// eslint-disable-next-line no-bitwise
2016-10-10 00:26:51 +00:00
prefix += ~~(Math.random() * MAX_UID); // "~~" acts like a faster Math.floor() here
2015-08-19 03:28:28 +00:00
} while (document.getElementById(prefix));
2017-09-30 21:28:03 +00:00
return prefix;
},
getSelectorFromElement: function getSelectorFromElement(element) {
var selector = element.getAttribute('data-target');
2017-09-30 21:28:03 +00:00
2017-03-20 02:03:32 +00:00
if (!selector || selector === '#') {
selector = element.getAttribute('href') || '';
}
2015-05-07 19:48:22 +00:00
2017-03-20 02:03:32 +00:00
try {
2017-09-06 04:05:12 +00:00
var $selector = $(document).find(selector);
2017-03-20 02:03:32 +00:00
return $selector.length > 0 ? selector : null;
} catch (error) {
return null;
}
},
reflow: function reflow(element) {
2016-10-30 22:21:53 +00:00
return element.offsetHeight;
},
2015-05-08 05:26:40 +00:00
triggerTransitionEnd: function triggerTransitionEnd(element) {
$(element).trigger(transition.end);
},
supportsTransitionEnd: function supportsTransitionEnd() {
2015-08-19 03:28:28 +00:00
return Boolean(transition);
2015-05-13 21:46:50 +00:00
},
2017-09-30 21:28:03 +00:00
isElement: function isElement(obj) {
return (obj[0] || obj).nodeType;
},
2015-05-13 21:46:50 +00:00
typeCheckConfig: function typeCheckConfig(componentName, config, configTypes) {
for (var property in configTypes) {
2017-09-06 04:05:12 +00:00
if (Object.prototype.hasOwnProperty.call(configTypes, property)) {
2015-08-19 03:28:28 +00:00
var expectedTypes = configTypes[property];
var value = config[property];
2017-09-30 21:28:03 +00:00
var valueType = value && Util.isElement(value) ? 'element' : toType(value);
2015-08-19 03:28:28 +00:00
if (!new RegExp(expectedTypes).test(valueType)) {
2017-09-30 21:28:03 +00:00
throw new Error(componentName.toUpperCase() + ": " + ("Option \"" + property + "\" provided type \"" + valueType + "\" ") + ("but expected type \"" + expectedTypes + "\"."));
2015-08-19 03:28:28 +00:00
}
2015-05-13 21:46:50 +00:00
}
}
}
};
setTransitionEndSupport();
return Util;
2017-10-15 22:51:44 +00:00
}($);
2017-04-22 06:58:09 +00:00
//# sourceMappingURL=util.js.map