bootstrap-vue
Version:
With more than 85 components, over 45 available plugins, several directives, and 1000+ icons, BootstrapVue provides one of the most comprehensive implementations of the Bootstrap v4 component and grid system available for Vue.js v2.6, complete with extens
54 lines (47 loc) • 1.63 kB
JavaScript
import { hasPassiveEventSupport } from './env'
import { isObject } from './inspect'
// --- Utils ---
// Normalize event options based on support of passive option
// Exported only for testing purposes
export const parseEventOptions = options => {
/* istanbul ignore else: can't test in JSDOM, as it supports passive */
if (hasPassiveEventSupport) {
return isObject(options) ? options : { capture: !!options || false }
} else {
// Need to translate to actual Boolean value
return !!(isObject(options) ? options.capture : options)
}
}
// Attach an event listener to an element
export const eventOn = (el, evtName, handler, options) => {
if (el && el.addEventListener) {
el.addEventListener(evtName, handler, parseEventOptions(options))
}
}
// Remove an event listener from an element
export const eventOff = (el, evtName, handler, options) => {
if (el && el.removeEventListener) {
el.removeEventListener(evtName, handler, parseEventOptions(options))
}
}
// Utility method to add/remove a event listener based on first argument (boolean)
// It passes all other arguments to the `eventOn()` or `eventOff` method
export const eventOnOff = (on, ...args) => {
const method = on ? eventOn : eventOff
method(...args)
}
// Utility method to prevent the default event handling and propagation
export const stopEvent = (
evt,
{ preventDefault = true, propagation = true, immediatePropagation = false } = {}
) => {
if (preventDefault) {
evt.preventDefault()
}
if (propagation) {
evt.stopPropagation()
}
if (immediatePropagation) {
evt.stopImmediatePropagation()
}
}