mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-11-16 22:25:30 +08:00
34 lines
1.2 KiB
JavaScript
34 lines
1.2 KiB
JavaScript
// Use this to register outside-click directive on a Vue component
|
|
// eslint-disable-next-line max-len
|
|
// eg v-click-outside="{handler: 'handlerToTrigger', exclude: [refs to ignore on click (eg 'searchInput', 'searchInputBtn')]}"
|
|
// eslint-enable-next-line max-len
|
|
|
|
export default {
|
|
bind(el, binding, vnode) {
|
|
el._vueClickOutside_ = (e) => {
|
|
let clickedOnExcludedEl = false;
|
|
const { exclude } = binding.value;
|
|
exclude.forEach(refName => {
|
|
if (!clickedOnExcludedEl) {
|
|
const excludedEl = vnode.context.$refs[refName];
|
|
if (!excludedEl) return;
|
|
|
|
clickedOnExcludedEl = (excludedEl._isVue ? excludedEl.$el : excludedEl).contains(e.target);
|
|
}
|
|
});
|
|
|
|
if (!el.contains(e.target) && !clickedOnExcludedEl) {
|
|
const { handler } = binding.value;
|
|
vnode.context[handler]();
|
|
}
|
|
};
|
|
|
|
document.addEventListener('click', el._vueClickOutside_);
|
|
document.addEventListener('touchstart', el._vueClickOutside_);
|
|
},
|
|
unbind(el) {
|
|
document.removeEventListener('click', el._vueClickOutside_);
|
|
document.removeEventListener('touchstart', el._vueClickOutside_);
|
|
el._vueClickOutside_ = null;
|
|
}
|
|
};
|