bootstrap-vue
Version:
BootstrapVue, with over 40 plugins and more than 75 custom components, provides one of the most comprehensive implementations of Bootstrap v4 components and grid system for Vue.js. With extensive and automated WAI-ARIA accessibility markup.
50 lines (48 loc) • 1.35 kB
JavaScript
import { contains, eventOff, eventOn } from '../utils/dom'
// @vue/component
export default {
data() {
return {
listenForClickOut: false
}
},
watch: {
listenForClickOut(newValue, oldValue) {
if (newValue !== oldValue) {
eventOff(this.clickOutElement, this.clickOutEventName, this._clickOutHandler, false)
if (newValue) {
eventOn(this.clickOutElement, this.clickOutEventName, this._clickOutHandler, false)
}
}
}
},
beforeCreate() {
// Declare non-reactive properties
this.clickOutElement = null
this.clickOutEventName = null
},
mounted() {
if (!this.clickOutElement) {
this.clickOutElement = document
}
if (!this.clickOutEventName) {
this.clickOutEventName = 'ontouchstart' in document.documentElement ? 'touchstart' : 'click'
}
if (this.listenForClickOut) {
eventOn(this.clickOutElement, this.clickOutEventName, this._clickOutHandler, false)
}
},
beforeDestroy() /* istanbul ignore next */ {
eventOff(this.clickOutElement, this.clickOutEventName, this._clickOutHandler, false)
},
methods: {
isClickOut(evt) {
return !contains(this.$el, evt.target)
},
_clickOutHandler(evt) {
if (this.clickOutHandler && this.isClickOut(evt)) {
this.clickOutHandler(evt)
}
}
}
}