0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-11-29 11:24:18 +01:00
Bootstrap/js/dist/util.js

152 lines
4.1 KiB
JavaScript
Raw Normal View History

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