# event-bridge

> Bridge object for cross-browser working with events.

Latest version **3.5.0** (published 2017-12-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install event-bridge
pnpm add event-bridge
yarn add event-bridge
bun add event-bridge
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.5.0 |
| Published | 2017-12-09 |
| First published | 2016-03-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Riki Fridrich |
| Maintainers | fczbkk |

## Links

- npm: https://www.npmjs.com/package/event-bridge
- Repository: https://github.com/fczbkk/event-bridge
- Homepage: https://github.com/fczbkk/event-bridge#readme
- Issues: https://github.com/fczbkk/event-bridge/issues
- npm.io page: https://npm.io/package/event-bridge

## Recent versions

- 3.5.0 (latest) — 2017-12-09
- 3.4.0 — 2016-10-04
- 3.3.1 — 2016-06-29
- 3.3.0 — 2016-06-29
- 3.2.0 — 2016-06-29
- 3.0.0 — 2016-03-08

## README

# Event Bridge

[![npm](https://img.shields.io/npm/v/event-bridge.svg?maxAge=2592000)](https://www.npmjs.com/package/event-bridge)
[![npm](https://img.shields.io/github/license/fczbkk/event-bridge.svg?maxAge=2592000)](https://github.com/fczbkk/event-bridge/blob/master/LICENSE)
[![David](https://img.shields.io/david/fczbkk/event-bridge.svg?maxAge=2592000)](https://david-dm.org/fczbkk/event-bridge)
[![Travis](https://img.shields.io/travis/fczbkk/event-bridge.svg?maxAge=2592000)](https://travis-ci.org/fczbkk/event-bridge)

Object for cross-browser working with events using [bridge pattern](http://en.wikipedia.org/wiki/Bridge_pattern).

This is a simple interface for [adding and removing event listeners](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget). Unfortunately, Internet Explorer 9 and older uses non-standard methods to do that.

You should use [`Event` polyfill](https://cdn.polyfill.io/v1/polyfill.js?features=Event|always) if you want the full functionality with native syntax.

## How to use

Install the library via NPM:

```shell
npm install event-bridge --save
```

Then use in your project like this:

```javascript
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

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### EventCallback

Callback fired by an Event.

Type: [Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)

**Parameters**

-   `event` **[Event](https://developer.mozilla.org/en-US/docs/Web/API/Event)** 

### event_object

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

Type: ([Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) \| [Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function))

### add

Add event listeners to the object.

**Parameters**

-   `object` **[event_object](#event_object)** Any object that can receive event listener. (optional, default `window`)
-   `events` **([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>)?** Single event name or list of event names. (optional, default `[]`)
-   `callback` **[EventCallback](#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](#event_object)** Any object that can receive event listener. (optional, default `window`)
-   `events` **([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>)?** Single event name or list of event names. (optional, default `[]`)
-   `callback` **[EventCallback](#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](#event_object)** Any object that can receive event listener. (optional, default `window`)
-   `events` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>?** List of event names. (optional, default `[]`)
-   `callback` **[EventCallback](#eventcallback)?** Function to be called when event is fired. (optional, default `noop`)

**Examples**

_Use `onPopState` in modern browsers, but `onHashChange` in IE9._

```javascript
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](#event_object)** Any object that can receive event listener. (optional, default `window`)
-   `events` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>?** List of event names. (optional, default `[]`)
-   `callback` **[EventCallback](#eventcallback)?** Function to be called when event is fired. (optional, default `noop`)

**Examples**

_Cross-browser listener for CSS animation end:_

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

### remove

Remove event listeners from the object.

**Parameters**

-   `object` **[event_object](#event_object)** Any object that can receive event listener. (optional, default `window`)
-   `events` **([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>)?** Single event name or list of event names. (optional, default `[]`)
-   `callback` **[EventCallback](#eventcallback)?** Function to be called when event is fired. (optional, default `noop`)

### stop

Cancel the event and stop its further propagation.

**Parameters**

-   `event` **[Event](https://developer.mozilla.org/en-US/docs/Web/API/Event)** 

### target

Get reference to the object that dispatched the event.

**Parameters**

-   `event` **[Event](https://developer.mozilla.org/en-US/docs/Web/API/Event)** 

Returns **([Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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](https://github.com/fczbkk/event-bridge/issues) or send me an e-mail at <mailto:riki@fczbkk.com>.

## License

This project is published under the [MIT license](https://github.com/fczbkk/event-bridge/blob/master/LICENSE). Feel free to use it in any way.

---
_Source: https://npm.io/package/event-bridge · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
