2
0
mirror of https://github.com/frappe/books.git synced 2024-11-15 17:57:08 +00:00
books/src/ui.js
2022-01-21 03:06:17 +05:30

31 lines
728 B
JavaScript

let instances = [];
function onDocumentClick(e, el, fn) {
let target = e.target;
if (el !== target && !el.contains(target)) {
fn(e);
}
}
export const outsideClickDirective = {
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);
},
};
// https://github.com/frappe/frappejs/commits/master/ui/plugins/outsideClickDirective.js