2
0
mirror of https://github.com/frappe/books.git synced 2024-12-27 12:42:44 +00:00
books/ui/plugins/outsideClickDirective.js

31 lines
642 B
JavaScript
Raw Normal View History

2018-09-28 13:10:48 +00:00
import Vue from 'vue';
let instances = [];
function onDocumentClick(e, el, fn) {
let target = e.target;
if ((el !== target) && (!el.contains(target))) {
fn(e);
}
}
export default {
bind(el, binding) {
el.dataset.outsideClickIndex = instances.length;
const fn = binding.value;
const click = function (e) {
onDocumentClick(e, el, fn)
};
document.addEventListener('click', click);
instances.push(click);
},
unbind(el) {
const index = el.dataset.outsideClickIndex;
const handler = instances[index];
document.addEventListener('click', handler);
instances.splice(index, 1);
}
};