0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-10 07:54:31 +01:00
Bootstrap/js/src/util.js

162 lines
3.9 KiB
JavaScript
Raw Normal View History

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