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

123 lines
2.9 KiB
JavaScript
Raw Normal View History

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';
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'
};
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) {
$(_this).trigger(transition.end);
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) {
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
supportsTransitionEnd: function supportsTransitionEnd() {
return !!transition;
}
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-07 21:48:22 +02:00
//# sourceMappingURL=util.js.map