Nondeterministic Polynomial Munchies

    event-listener-helper

    1.1.3 • Public • Published

    event-listener-helper

    npm version CircleCI codecov Codacy Badge

    This library allows you to:

    • Get a list of event listeners attached to the target node
    • Confirms the existence of event listener registered on the target node
    • Deletes all event listeners registered on the target node
    • Registers event listeners with name (rather than a reference)

    These benefits can be received by calling addEventListener and removeEventListener via this library.

    MIT License

    Examples

    • Add named event listener to the button element
      const eh = new EventListenerHelper();
    
      const btn = document.querySelector('#myButton');
    
      eh.addEventListener(btn, 'click', () => {
        alert('Hello!');
      }, { listenerName: 'my-listener' });
    • Remove named event listener from the button element
      const eh = new EventListenerHelper();
    
      const btn = document.querySelector('#myButton');
    
      eh.addEventListener(btn, 'click', () => {
        alert('Hello!');
      }, { listenerName: 'my-listener' });
    
      eh.clearEventListener(btn, 'click', 'my-listener');
        
    • Get registered listeners on the button element
    eh.getEventListeners(btn, 'click');
    • Check the existence of a listener who has already registered
    eh.hasEventListeners(btn, 'click');

    Demo

    https://riversun.github.io/event-listener-helper/

    How to install

    • NPM
    npm install event-listener-helper
    

    or

    • use <script> tag from CDN
     <script src="https://cdn.jsdelivr.net/npm/event-listener-helper/lib/event-listener-helper.js"></script>

    API Details

    JSDoc here

    EventListenerHelper

    Kind: global class
    Author: Tom Misawa (riversun.org@gmail.com,https://github.com/riversun)

    new EventListenerHelper()

    This library allows you to: get a list of event listeners attached to the target node, confirms the existence of event listener registered on the target node, deletes all event listeners registered on the target node, registers event listeners with name (rather than a reference). These benefits can be received by calling addEventListener and removeEventListener through this library.

    MIT License

    eventListenerHelper.addEventListener(eventTarget, eventType, listener, [options]) ⇒

    Kind: instance method of EventListenerHelper

    Param Type Description
    eventTarget EventTarget EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.

    EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5.

    eventType String A case-sensitive string representing the event type to listen for.
    listener function The object which receives a notification (an object that implements the Event interface) when an event of the specified type occurs. This must be an object implementing the EventListener interface, or a JavaScript function. See The event listener callback for details on the callback itself.
    [options] Object An options object specifies characteristics about the event listener. The available options are:
    listenerName
    A StringBy assigning listenerName, the specified listener function (callback function) can be specified.In other words, it is possible to retrieve the listener function later using this listenerName as a key.listenerName must be unique.
    capture
    A Boolean indicating that events of this type will be dispatched to the registered listener before being dispatched to any EventTarget beneath it in the DOM tree.
    once
    A Boolean indicating that the listener should be invoked at most once after being added. If true, the listener would be automatically removed when invoked.

    addEventListener by Mozilla Contributors is licensed under CC-BY-SA 2.5.

    eventListenerHelper.removeEventListener(eventTarget, eventType, [listener], [options]) ⇒

    The EventListenerHelper#removeEventListener method removes from the EventTarget an event listener previously registered with EventListenerHelper#addEventListener. The event listener to be removed is identified using option. listenerName and a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal

    Kind: instance method of EventListenerHelper

    Param Type Description
    eventTarget EventTarget EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.

    EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5.

    eventType String A string which specifies the type of event for which to remove an event listener.
    [listener] function (Either the listener or options.listenerName must be specified. If both are specified, options.listenerName takes precedence.)
    The object which receives a notification (an object that implements the Event interface) when an event of the specified type occurs. This must be an object implementing the EventListener interface, or a JavaScript function. See The event listener callback for details on the callback itself.
    [options] Object (Either the listener or options.listenerName must be specified. If both are specified, options.listenerName takes precedence.)
    An options object specifies characteristics about the event listener. The available options are:
    listenerName
    A StringBy assigning listenerName, the specified listener function (callback function) can be specified.In other words, it is possible to retrieve the listener function later using this listenerName as a key.listenerName must be unique.
    capture
    A Boolean indicating that events of this type will be dispatched to the registered listener before being dispatched to any EventTarget beneath it in the DOM tree.

    removeEventListener by Mozilla Contributors is licensed under CC-BY-SA 2.5.

    eventListenerHelper.getEventListeners(eventTarget, [eventType]) ⇒

    Get a listener definition matching the specified eventTarget and eventType (optional). Please note that the return value is immutable.

    Kind: instance method of EventListenerHelper
    Returns: Example of the returned value when only eventTarget is specified:

    [
    {
    eventType:click,
    listener:[
    {
    listener:func,
    options:{
    listenerName:my-test-listener-1
    }
    },
    {
    listener:func,
    options:{
    capture:true,
    listenerName:my-test-listener-2
    }
    }
    ]
    },
    {
    eventType:mousemove,
    listener:[
    {
    listener:func,
    options:{
    once:true,
    listenerName:my-test-listener-3
    }
    }
    ]
    }
    ]
    

    Example of returned value when eventType is also specified as an argument:
    [
    {
    options:{
    listenerName:my-test-listener-1
    },
    listener:func1
    },
    {
    options:{
    capture:true,
    listenerName:my-test-listener-2
    },
    listener:func2
    },
    {
    options:{
    once:true,
    listenerName:my-test-listener-3
    },
    listener:func3
    }
    ]
    

    Param Type Description
    eventTarget EventTarget EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.

    EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5.

    [eventType] String A case-sensitive string representing the event type to listen for.

    eventListenerHelper.getAllEventListeners() ⇒

    You can get listeners for "inputElement" 's "click" event by map chain.

    Kind: instance method of EventListenerHelper
    Returns: const listeners=result.get(inputElement).get('click');

    eventListenerHelper.getEventListener(eventTarget, eventType, listenerName) ⇒ function

    Get a listener with the specified eventTarget, eventType and listenerName. The listenerName must be unique for one eventTarget and eventType combination, but it does not have to be unique for different eventTargets or different eventTypes.

    Kind: instance method of EventListenerHelper
    Returns: function - Returns null if no listener function is found

    Param Type Description
    eventTarget EventTarget EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.

    EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5.

    eventType String A case-sensitive string representing the event type to listen for.
    listenerName String The listener name of the listener you want to find

    eventListenerHelper.hasEventListeners(eventTarget, eventType) ⇒ boolean

    Returns whether or not there are more than one event listener for the given eventTarget and eventType.

    Kind: instance method of EventListenerHelper

    Param
    eventTarget
    eventType

    eventListenerHelper.hasEventListener(eventTarget, eventType, listenerName) ⇒ boolean

    Returns whether a listenerName exists for the specified eventTarget and eventType.

    Kind: instance method of EventListenerHelper

    Param Type Description
    eventTarget EventTarget EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.

    EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5.

    eventType String A case-sensitive string representing the event type to listen for.
    listenerName String The listener name of the listener you want to find

    eventListenerHelper.clearAllEventListeners()

    Removes all registered events through the addEventListener method.

    Kind: instance method of EventListenerHelper

    eventListenerHelper.clearEventListeners(eventTarget, [eventType])

    Remove all listeners matching the specified eventTarget and eventType (optional).

    Kind: instance method of EventListenerHelper

    Param Type Description
    eventTarget EventTarget EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.

    EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5.

    [eventType] String A case-sensitive string representing the event type to listen for.

    eventListenerHelper.clearEventListener(eventTarget, [eventType], listenerName)

    Removes the eventListener with eventTarget, eventType, and listenerName as arguments. The functions are the same as those of removeEventListener, except for the way to give arguments.

    Kind: instance method of EventListenerHelper

    Param Type Description
    eventTarget EventTarget EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.

    EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5.

    [eventType] String A case-sensitive string representing the event type to listen for.
    listenerName String The listener name of the listener you want to find

    eventListenerHelper.getAllEventTargets() ⇒

    Get all registered eventTargets through the #addEventListener method.

    Kind: instance method of EventListenerHelper

    eventListenerHelper.searchEventListenersByName(listenerName) ⇒

    Get all listeners(listener definition) with a given listenerName. Since listeners need only be unique to the eventTarget and eventType, it is possible to have the same listenerName for different eventTargets and eventTypes.

    Kind: instance method of EventListenerHelper
    Returns:

    [ { options: { listenerName: 'my-test-listener' },
    listener: [Function: func] },
    { options: { capture: true, listenerName: 'my-test-listener' },
    listener: [Function: func] },
    { options: { once: true, listenerName: 'my-test-listener' },
    listener: [Function: func] },
    { options: { once: true, listenerName: 'my-test-listener' },
    listener: [Function: func] } ]
    

    Param Type Description
    listenerName String The listener name of the listener you want to find

    Install

    npm i event-listener-helper

    DownloadsWeekly Downloads

    201

    Version

    1.1.3

    License

    MIT

    Unpacked Size

    55.5 kB

    Total Files

    11

    Last publish

    Collaborators

    • riversun