npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

event-bridge

v3.5.0

Published

Bridge object for cross-browser working with events.

Downloads

13

Readme

Event Bridge

npm npm David Travis

Object for cross-browser working with events using bridge pattern.

This is a simple interface for adding and removing event listeners. Unfortunately, Internet Explorer 9 and older uses non-standard methods to do that.

You should use Event polyfill if you want the full functionality with native syntax.

How to use

Install the library via NPM:

npm install event-bridge --save

Then use in your project like this:

import EventBridge from 'event-bridge';

// add event listener
EventBridge.add(element, 'click', function (e) {/*...*/});

// remove event listener
// NOTE: you have to use reference to the same function object that you used
// when you were adding the event listener... so no anonymous functions
function handleEvent(e) {/*...*/}
EventBridge.add(element, 'click', handleEvent);  // now the listener is active
EventBridge.remove(element, 'click', handleEvent);  // now it is not

// add multiple event listeners at once
EventBridge.add(element, ['mouseover', 'mouseout'], handleEvent);

// handy methods for cross-browser handling of event objects
EventBridge.add(element, 'click', function (e) {

  // get the event target (IE provides event.srcElement instead event.target)
  EventBridge.target(e);

  // stop the event
  EventBridge.stop(e);

});

// add first event supported by browser - this will add 'onPopState' event in
// modern browsers, but 'onHashChange' in IE9
EventBridge.addFirstSupported(window, ['popstate', 'hashchange'], function () {...});

Documentation

EventCallback

Callback fired by an Event.

Type: Function

Parameters

event_object

Object for event listener. If function is used, it will be evaluated and its return value will be used.

Type: (Object | Function)

add

Add event listeners to the object.

Parameters

  • object event_object Any object that can receive event listener. (optional, default window)
  • events (string | Array<string>)? Single event name or list of event names. (optional, default [])
  • callback EventCallback? Function to be called when event is fired. (optional, default noop)

once

Add event listeners to the object. After any of the events has been fired, the event listeners are removed.

Parameters

  • object event_object Any object that can receive event listener. (optional, default window)
  • events (string | Array<string>)? Single event name or list of event names. (optional, default [])
  • callback EventCallback? Function to be called when event is fired. (optional, default noop)

addFirstSupported

Add first supported event listener from the list, ignore the rest.

Parameters

  • object event_object Any object that can receive event listener. (optional, default window)
  • events Array<string>? List of event names. (optional, default [])
  • callback EventCallback? Function to be called when event is fired. (optional, default noop)

Examples

Use onPopState in modern browsers, but onHashChange in IE9.

addFirstSupported(window, ['popstate', 'hashchange'], function () {...});

addFirstSupportedOnce

Add first supported event listener from the list, ignore the rest. After the event has been fired, the event listener is removed.

Parameters

  • object event_object Any object that can receive event listener. (optional, default window)
  • events Array<string>? List of event names. (optional, default [])
  • callback EventCallback? Function to be called when event is fired. (optional, default noop)

Examples

Cross-browser listener for CSS animation end:

addFirstSupportedOnce(my_element, ['transitionend', 'oTransitionEnd', 'webkitTransitionEnd'], function () {...});

remove

Remove event listeners from the object.

Parameters

  • object event_object Any object that can receive event listener. (optional, default window)
  • events (string | Array<string>)? Single event name or list of event names. (optional, default [])
  • callback EventCallback? Function to be called when event is fired. (optional, default noop)

stop

Cancel the event and stop its further propagation.

Parameters

target

Get reference to the object that dispatched the event.

Parameters

Returns (Object | null)

sanitizeInputObject

Sanitizes input object, evaluates it if it is function.

Parameters

  • input any?

Returns any

Bug reports, feature requests and contact

If you found any bugs, if you have feature requests or any questions, please, either file an issue at GitHub or send me an e-mail at mailto:[email protected].

License

This project is published under the MIT license. Feel free to use it in any way.