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

153 lines
3.8 KiB
JavaScript
Raw Normal View History

2015-08-19 04:22:46 +02:00
2015-05-07 21:48:22 +02:00
/**
* --------------------------------------------------------------------------
* Bootstrap (v4.0.0): util.js
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
'use strict';
2015-05-08 02:07:38 +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
var TransitionEndEvent = {
WebkitTransition: 'webkitTransitionEnd',
MozTransition: 'transitionend',
OTransition: 'oTransitionEnd otransitionend',
transition: 'transitionend'
};
2015-05-13 23:46:50 +02:00
// shoutout AngusCroll (https://goo.gl/pxwQGp)
function toType(obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}
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)) {
return event.handleObj.handler.apply(this, arguments);
}
}
};
}
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
for (var name in TransitionEndEvent) {
if (el.style[name] !== undefined) {
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 04:22:46 +02:00
do { prefix += ~ ~(Math.random() * 1000000); } while (document.getElementById(prefix));
return prefix;
},
2015-05-07 21:48:22 +02:00
getSelectorFromElement: function getSelectorFromElement(element) {
var selector = element.getAttribute('data-target');
if (!selector) {
selector = element.getAttribute('href') || '';
selector = /^#[a-z]/i.test(selector) ? selector : null;
}
2015-05-07 21:48:22 +02:00
return selector;
},
2015-05-07 21:48:22 +02:00
reflow: function reflow(element) {
new Function('bs', 'return bs')(element.offsetHeight);
},
2015-05-07 21:48:22 +02:00
2015-05-08 07:26:40 +02:00
triggerTransitionEnd: function triggerTransitionEnd(element) {
$(element).trigger(transition.end);
},
supportsTransitionEnd: function supportsTransitionEnd() {
return !!transition;
2015-05-13 23:46:50 +02:00
},
typeCheckConfig: function typeCheckConfig(componentName, config, configTypes) {
for (var property in configTypes) {
var expectedTypes = configTypes[property];
var value = config[property];
var valueType = undefined;
if (value && isElement(value)) valueType = 'element';else valueType = toType(value);
if (!new RegExp(expectedTypes).test(valueType)) {
2015-08-13 06:12:03 +02:00
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
};
2015-05-07 21:48:22 +02:00
setTransitionEndSupport();
2015-05-07 21:48:22 +02:00
return Util;
2015-05-08 02:07:38 +02:00
})(jQuery);
2015-08-13 06:12:03 +02:00
//# sourceMappingURL=util.js.map