Skip to content

Commit

Permalink
adding getEventListeners_allOptions.js
Browse files Browse the repository at this point in the history
  • Loading branch information
orstavik committed Aug 5, 2020
1 parent eab7834 commit c9a79b9
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 16 deletions.
45 changes: 40 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,53 @@ y. we need a discussion about the safety of the getEventListeners() method.

## how to use it

### 1. Simple `getEventListener(eventTarget)` polyfill

The simple `getEventListener(eventTarget)` supports the native `once` event listener option. The ES6 `import` returns an `addEventTargetRegistry()` function that should be run before any event listeners are added to the DOM.

```javascript
import {addEventTargetRegistry} from "https://cdn.jsdelivr.net/gh/orstavik/getEventListeners@1/src/getEventListeners_once.js";

window.getEventListeners = addEventTargetRegistry();
```

### 2. `getEventListener(eventTarget)` polyfill with added support to `first` and `last` event listener options.

```javascript
import {addEventTargetRegistry as addEventTargetRegistry1} from "https://cdn.jsdelivr.net/gh/orstavik/getEventListeners@1/src/getEventListeners.js";
import {addEventTargetRegistry as addEventTargetRegistry2} from "https://cdn.jsdelivr.net/gh/orstavik/getEventListeners@1/src/getEventListeners_once.js";
import {addEventTargetRegistry as addEventTargetRegistry3} from "https://cdn.jsdelivr.net/gh/orstavik/getEventListeners@1/src/getEventListeners_once_last.js";
import {addEventTargetRegistry} from "https://cdn.jsdelivr.net/gh/orstavik/getEventListeners@1/src/getEventListeners_once_last_first.js";
import {addEventListenerOptionScopedUnstoppable} from "https://cdn.jsdelivr.net/gh/orstavik/getEventListeners@1/src/EventListenersOptionUnstoppableScoped.js";

window.getEventListeners = addEventTargetRegistry();
```

### 3. scoped `.stopPropagation()`

Make `stopPropagation()` and `stopImmediatePropagation()` only apply to the current DOM context, to avoid shadowTorpedoes, captureTorpedoes, etc. Returns a method `isStopped(event)` that can check whether or not an event's propagation has been stopped.

```javascript
import {addEventIsStoppedScoped} from "https://cdn.jsdelivr.net/gh/orstavik/getEventListeners@1/src/ScopedStopPropagation.js";

const isStopped = addEventIsStoppedScoped(Event.prototype);
```

### 4. `unstoppable` and `isScoped` event listener options

This method require the scoped `stopPropagation()` #3.

```javascript
import {addEventIsStoppedScoped} from "https://cdn.jsdelivr.net/gh/orstavik/getEventListeners@1/src/ScopedStopPropagation.js";
import {addEventListenerOptionScopedUnstoppable} from "https://cdn.jsdelivr.net/gh/orstavik/getEventListeners@1/src/EventListenersOptionUnstoppableScoped.js";

const scopedByDefault = true;

const isStopped = addEventIsStoppedScoped(Event.prototype);
addEventListenerOptionScopedUnstoppable(EventTarget.prototype, isStopped);
scopedByDefault && Object.defineProperty(Event.prototype, "isScoped", {value: true});
window.getEventListeners = addEventTargetRegistry(EventTarget.prototype);
```

### 5. Everything

```javascript
import {addGetEventListeners_allOptions} from "https://cdn.jsdelivr.net/gh/orstavik/getEventListeners@1/src/getEventListeners_allOptions.js";

window.getEventListeners = addGetEventListeners_allOptions(true); //isScoped is set as default value for all event listeners
```
2 changes: 1 addition & 1 deletion src/EventListenersOptionUnstoppableScoped.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function removeWrapper(target, type, options, cb) {

//scoped event listeners will only obey stopPropagations called inside the same scope.
//unstoppable event listeners will not obey any stopPropagations.
export function addEventListenerOptionScopedUnstoppable(EventTargetPrototype, isStopped) {
export function addEventListenerOptionScopedUnstoppable(EventTargetPrototype = EventTarget.prototype, isStopped) {
const addEventListenerOG = EventTargetPrototype.addEventListener;
const removeEventListenerOG = EventTargetPrototype.removeEventListener;

Expand Down
2 changes: 1 addition & 1 deletion src/ScopedStopPropagation.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function isStopped(event, listenerIsScoped) {
*
* @param EventPrototype
*/
export function addEventIsStoppedScoped(EventPrototype) {
export function addEventIsStoppedScoped(EventPrototype = Event.prototype) {
Object.defineProperty(EventPrototype, "stopPropagation", {
value: function stopPropagation(scoped) {
if (this.eventPhase === 0)
Expand Down
2 changes: 1 addition & 1 deletion src/getEventListeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function removeListener(target, type, listener, options) {

const dynamicallyRemovedEntries = new WeakSet();

export function addEventTargetRegistry(EventTargetPrototype) {
export function addEventTargetRegistry(EventTargetPrototype = EventTarget.prototype) {
const ogAdd = EventTargetPrototype.addEventListener;
const ogRemove = EventTargetPrototype.removeEventListener;

Expand Down
15 changes: 15 additions & 0 deletions src/getEventListeners_allOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {addEventTargetRegistry} from "./getEventListeners_once_last_first.js";
import {addEventListenerOptionScopedUnstoppable} from "./EventListenersOptionUnstoppableScoped.js";
import {addEventIsStoppedScoped} from "./ScopedStopPropagation.js";

export function addGetEventListeners_allOptions(
scopedByDefault,
eventPrototype = Event.prototype,
eventTargetPrototype = EventTarget.prototype)
{
const isStopped = addEventIsStoppedScoped(eventPrototype);
addEventListenerOptionScopedUnstoppable(eventTargetPrototype, isStopped);
scopedByDefault && Object.defineProperty(eventPrototype, "isScoped", {value: true});
return addEventTargetRegistry(eventTargetPrototype);
}

2 changes: 1 addition & 1 deletion src/getEventListeners_once.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function removeListener(target, type, listener, options) {
const entryToOnceCb = new WeakMap();
const dynamicallyRemovedEntries = new WeakSet();

export function addEventTargetRegistry(EventTargetPrototype) {
export function addEventTargetRegistry(EventTargetPrototype = EventTarget.prototype) {
const ogAdd = EventTargetPrototype.addEventListener;
const ogRemove = EventTargetPrototype.removeEventListener;

Expand Down
2 changes: 1 addition & 1 deletion src/getEventListeners_once_last.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const entryToOnceCb = new WeakMap();
const targetToLastEntry = new WeakMap();
const dynamicallyRemovedEntries = new WeakSet();

export function addEventTargetRegistry(EventTargetPrototype) {
export function addEventTargetRegistry(EventTargetPrototype = EventTarget.prototype) {
const ogAdd = EventTargetPrototype.addEventListener;
const ogRemove = EventTargetPrototype.removeEventListener;

Expand Down
7 changes: 1 addition & 6 deletions src/getEventListeners_once_last_first.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,7 @@ function verifyFirstLast(target, options) {
throw new Error("only one event listener {first: true} can be added to the same target and event type.");
}

/**
*
* @param EventTargetPrototype
* @returns {getEventListeners} the function that can be used to retrieve the event listeners from the event listener registry
*/
export function addEventTargetRegistry(EventTargetPrototype) {
export function addEventTargetRegistry(EventTargetPrototype = EventTarget.prototype) {
const ogAdd = EventTargetPrototype.addEventListener;
const ogRemove = EventTargetPrototype.removeEventListener;

Expand Down

0 comments on commit c9a79b9

Please sign in to comment.