cartodb/lib/assets/javascripts/new-dashboard/directives/click-outside.js

22 lines
714 B
JavaScript
Raw Normal View History

2020-06-15 10:58:47 +08:00
import Vue from 'vue';
Vue.directive('clickOutside', {
bind: function (element, binding, vnode) {
element.clickOutsideEvent = function (event) {
if (!element.contains(event.target)) {
vnode.context[binding.expression](event);
}
};
element.addClickEvent = function (event) {
document.body.addEventListener('click', element.clickOutsideEvent, { passive: true });
};
element.addEventListener('click', element.addClickEvent, { passive: true });
},
unbind: function (element) {
document.body.removeEventListener('click', element.clickOutsideEvent, { passive: true });
element.removeEventListener('click', element.addClickEvent, { passive: true });
}
});