/**
 * On any event which is not addressed to element
 * @param element
 * @param eventList
 * @param callback
 */
export function onEventOutside (element, eventList, callback) {
  function handler (event) {
    if (!element.contains(event.target)) {
      return callback(event);
    }
    return null;
  }
  eventList.forEach((event) => {
    document.body.addEventListener(event, handler);
  });
  return () => {
    eventList.forEach((event) => {
      document.body.removeEventListener(event, handler);
    });
  };
}

export function onClickOutside (element, callback) {
  return onEventOutside(element, ['click'], callback);
}

export function onDropOutside (element, callback) {
  return onEventOutside(element, ['mouseup', 'touchend'], callback);
}
