/**
 * Program: Click outside
 * Author: SDL
 * Description: Manage mouse click events outside containers
 */
export default {
  beforeMount(el, binding) {
    const { bubble } = binding.modifiers;
    const handler = e => {
      if (bubble || (!el.contains(e.target) && el !== e.target)) {
        binding.value({ source: el, evt: e });
      }
    };
    el.vueClickOutside = handler;
    // add Event Listeners
    document.addEventListener('click', handler);
  },
  unmounted(el) {
    // Remove Event Listeners
    document.removeEventListener('click', el.vueClickOutside);
    el.vueClickOutside = null;
  },
};
