bootstrap/js/src/util.js

153 lines
4.0 KiB
JavaScript
Raw Normal View History

import $ from 'jquery'
2015-05-08 03:48:22 +08:00
/**
* --------------------------------------------------------------------------
2018-07-12 15:04:51 +08:00
* Bootstrap (v4.1.2): 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
const TRANSITION_END = 'transitionend'
const MAX_UID = 1000000
2018-03-13 16:59:20 +08:00
const MILLISECONDS_MULTIPLIER = 1000
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) {
2018-01-22 03:51:49 +08:00
return {}.toString.call(obj).match(/\s([a-z]+)/i)[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 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() {
$.fn.emulateTransitionEnd = transitionEndEmulator
$.event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent()
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') || ''
}
try {
return document.querySelector(selector) ? selector : null
2017-12-16 20:00:38 +08:00
} catch (err) {
return null
}
},
2015-05-08 03:48:22 +08:00
2018-03-13 16:59:20 +08:00
getTransitionDurationFromElement(element) {
2018-03-13 17:38:36 +08:00
if (!element) {
return 0
}
2018-03-13 16:59:20 +08:00
// Get transition-duration of the element
let transitionDuration = $(element).css('transition-duration')
2018-03-13 17:38:36 +08:00
const floatTransitionDuration = parseFloat(transitionDuration)
2018-03-13 16:59:20 +08:00
// Return 0 if element or transition duration is not found
2018-03-13 17:38:36 +08:00
if (!floatTransitionDuration) {
2018-03-13 16:59:20 +08:00
return 0
}
// If multiple durations are defined, take the first
transitionDuration = transitionDuration.split(',')[0]
return parseFloat(transitionDuration) * MILLISECONDS_MULTIPLIER
},
reflow(element) {
return element.offsetHeight
},
2015-05-08 13:26:40 +08:00
triggerTransitionEnd(element) {
$(element).trigger(TRANSITION_END)
2015-05-08 13:26:40 +08:00
},
// TODO: Remove in v5
supportsTransitionEnd() {
return Boolean(TRANSITION_END)
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