bootstrap/js/src/util.js

162 lines
4.1 KiB
JavaScript
Raw Normal View History

import $ from 'jquery'
2015-05-08 03:48:22 +08:00
/**
* --------------------------------------------------------------------------
2018-01-19 00:21:22 +08:00
* Bootstrap (v4.0.0): util.js
2015-05-08 03:48:22 +08:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* --------------------------------------------------------------------------
*/
const Util = (($) => {
/**
* ------------------------------------------------------------------------
* Private TransitionEnd Helpers
* ------------------------------------------------------------------------
*/
2015-05-08 03:48:22 +08:00
let transition = false
2015-05-08 03:48:22 +08:00
const MAX_UID = 1000000
2017-12-16 20:00:38 +08:00
// Shoutout AngusCroll (https://goo.gl/pxwQGp)
2015-05-14 05:46:50 +08:00
function toType(obj) {
return {}.toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
2015-05-14 05:46:50 +08:00
}
function getSpecialTransitionEndEvent() {
return {
bindType: transition.end,
delegateType: transition.end,
2015-08-19 10:22:46 +08:00
handle(event) {
if ($(event.target).is(this)) {
return event.handleObj.handler.apply(this, arguments) // eslint-disable-line prefer-rest-params
}
2017-07-27 18:39:55 +08:00
return undefined // eslint-disable-line no-undefined
}
}
}
2015-05-08 03:48:22 +08:00
function transitionEndTest() {
if (typeof window !== 'undefined' && window.QUnit) {
return false
2015-05-08 03:48:22 +08:00
}
2017-12-07 19:10:18 +08:00
return {
end: 'transitionend'
}
2015-05-08 03:48:22 +08:00
}
function transitionEndEmulator(duration) {
let called = false
2015-05-08 03:48:22 +08:00
2015-08-19 10:22:46 +08:00
$(this).one(Util.TRANSITION_END, () => {
called = true
})
2015-05-08 03:48:22 +08:00
setTimeout(() => {
if (!called) {
2015-05-08 13:26:40 +08:00
Util.triggerTransitionEnd(this)
2015-05-08 03:48:22 +08:00
}
}, duration)
2015-05-08 03:48:22 +08:00
return this
2015-05-08 03:48:22 +08:00
}
function setTransitionEndSupport() {
transition = transitionEndTest()
$.fn.emulateTransitionEnd = transitionEndEmulator
2015-05-08 03:48:22 +08:00
if (Util.supportsTransitionEnd()) {
$.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent()
2015-05-08 03:48:22 +08:00
}
}
function escapeId(selector) {
2017-12-16 20:00:38 +08:00
// We escape IDs in case of special selectors (selector = '#myId:something')
// $.escapeSelector does not exist in jQuery < 3
2017-12-16 20:00:38 +08:00
selector = typeof $.escapeSelector === 'function' ? $.escapeSelector(selector).substr(1)
: selector.replace(/(:|\.|\[|\]|,|=|@)/g, '\\$1')
return selector
}
2015-05-08 03:48:22 +08:00
/**
* --------------------------------------------------------------------------
* Public Util Api
* --------------------------------------------------------------------------
*/
2015-05-08 03:48:22 +08:00
const Util = {
2015-05-08 03:48:22 +08:00
TRANSITION_END: 'bsTransitionEnd',
getUID(prefix) {
2015-08-19 10:22:46 +08:00
do {
// eslint-disable-next-line no-bitwise
prefix += ~~(Math.random() * MAX_UID) // "~~" acts like a faster Math.floor() here
2015-08-19 10:22:46 +08:00
} while (document.getElementById(prefix))
return prefix
},
2015-05-08 03:48:22 +08:00
getSelectorFromElement(element) {
let selector = element.getAttribute('data-target')
if (!selector || selector === '#') {
selector = element.getAttribute('href') || ''
}
2017-12-16 20:00:38 +08:00
// If it's an ID
if (selector.charAt(0) === '#') {
selector = escapeId(selector)
}
try {
2017-08-26 03:54:49 +08:00
const $selector = $(document).find(selector)
return $selector.length > 0 ? selector : null
2017-12-16 20:00:38 +08:00
} catch (err) {
return null
}
},
2015-05-08 03:48:22 +08:00
reflow(element) {
return element.offsetHeight
},
2015-05-08 13:26:40 +08:00
triggerTransitionEnd(element) {
$(element).trigger(transition.end)
},
supportsTransitionEnd() {
2015-08-19 10:22:46 +08:00
return Boolean(transition)
2015-05-14 05:46:50 +08:00
},
isElement(obj) {
return (obj[0] || obj).nodeType
},
2015-05-14 05:46:50 +08:00
typeCheckConfig(componentName, config, configTypes) {
for (const property in configTypes) {
2017-07-27 18:39:55 +08:00
if (Object.prototype.hasOwnProperty.call(configTypes, property)) {
const expectedTypes = configTypes[property]
const value = config[property]
2017-12-16 20:00:38 +08:00
const valueType = value && Util.isElement(value)
? 'element' : toType(value)
2015-08-19 10:22:46 +08:00
if (!new RegExp(expectedTypes).test(valueType)) {
throw new Error(
`${componentName.toUpperCase()}: ` +
`Option "${property}" provided type "${valueType}" ` +
`but expected type "${expectedTypes}".`)
}
2015-05-14 05:46:50 +08:00
}
}
}
2015-05-08 03:48:22 +08:00
}
setTransitionEndSupport()
return Util
2017-10-03 20:27:36 +08:00
})($)
export default Util