diff options
| author | fat <[email protected]> | 2015-05-07 12:48:22 -0700 |
|---|---|---|
| committer | fat <[email protected]> | 2015-05-07 12:57:31 -0700 |
| commit | 0724bd91ff81b5eca0addce0c336c72b3ec10be5 (patch) | |
| tree | e379980598b1662ff14ff5543c825887148bc0c4 /js/src/util.js | |
| parent | d1fbe200f46002431cdeebf965c4b789ef7ed267 (diff) | |
| download | bootstrap-0724bd91ff81b5eca0addce0c336c72b3ec10be5.tar.xz bootstrap-0724bd91ff81b5eca0addce0c336c72b3ec10be5.zip | |
es6 alert :|
Diffstat (limited to 'js/src/util.js')
| -rw-r--r-- | js/src/util.js | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/js/src/util.js b/js/src/util.js new file mode 100644 index 000000000..e9542149e --- /dev/null +++ b/js/src/util.js @@ -0,0 +1,118 @@ +/** + * -------------------------------------------------------------------------- + * Bootstrap (v4.0.0): util.js + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) + * -------------------------------------------------------------------------- + */ + + +/** + * -------------------------------------------------------------------------- + * Public Util Api + * -------------------------------------------------------------------------- + */ + +var Util = { + + TRANSITION_END: 'bsTransitionEnd', + + getUID(prefix) { + do prefix += ~~(Math.random() * 1000000) + while (document.getElementById(prefix)) + return prefix + }, + + getSelectorFromElement(element) { + let selector = element.getAttribute('data-target') + + if (!selector) { + selector = element.getAttribute('href') || '' + selector = /^#[a-z]/i.test(selector) ? selector : null + } + + return selector + }, + + reflow(element) { + new Function('bs', 'return bs')(element.offsetHeight) + }, + + supportsTransitionEnd() { + return !!transition + } + +} + +export default Util + + +/** + * -------------------------------------------------------------------------- + * Private TransitionEnd Helpers + * -------------------------------------------------------------------------- + */ + +let transition = false + +const TransitionEndEvent = { + WebkitTransition : 'webkitTransitionEnd', + MozTransition : 'transitionend', + OTransition : 'oTransitionEnd otransitionend', + transition : 'transitionend' +} + +function getSpecialTransitionEndEvent() { + return { + bindType: transition.end, + delegateType: transition.end, + handle: function (event) { + if ($(event.target).is(this)) { + return event.handleObj.handler.apply(this, arguments) + } + } + } +} + +function transitionEndTest() { + if (window.QUnit) { + return false + } + + let el = document.createElement('bootstrap') + + for (var name in TransitionEndEvent) { + if (el.style[name] !== undefined) { + return { end: TransitionEndEvent[name] } + } + } + + return false +} + +function transitionEndEmulator(duration) { + let called = false + + $(this).one(Util.TRANSITION_END, function () { + called = true + }) + + setTimeout(() => { + if (!called) { + $(this).trigger(transition.end) + } + }, duration) + + return this +} + +function setTransitionEndSupport() { + transition = transitionEndTest() + + $.fn.emulateTransitionEnd = transitionEndEmulator + + if (Util.supportsTransitionEnd()) { + $.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent() + } +} + +setTransitionEndSupport() |
