SHARE
TWEET

Untitled

a guest Jun 24th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export const customEvent = (
  2.   events: string,
  3.   customEventInit?: CustomEventInit
  4. ) => (
  5.   target: Document | string,
  6.   listener: EventListener,
  7.   addEventListenerOptions?: boolean | AddEventListenerOptions
  8. ) => {
  9.   const eventInstance = new CustomEvent(events, customEventInit);
  10.  
  11.   if (target instanceof Document) {
  12.     target.addEventListener(events, listener, addEventListenerOptions);
  13.  
  14.     return () => target.dispatchEvent(eventInstance);
  15.   } else {
  16.     const targets = document.querySelectorAll(target);
  17.  
  18.     targets.forEach(element => {
  19.       element.addEventListener(events, listener, addEventListenerOptions);
  20.     });
  21.  
  22.     return () =>
  23.       [...targets]
  24.         .map(element => element.dispatchEvent(eventInstance))
  25.         .some(isPrevented => isPrevented === false)
  26.         ? false
  27.         : true;
  28.   }
  29. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top