Socket
Socket
Sign inDemoInstall

dom-delegate

Package Overview
Dependencies
0
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.5.0 to 1.0.0

build/dom-delegate.js

2

component.json
{
"name": "dom-delegate",
"description": "Create and manage a DOM event delegator.",
"version": "0.3.0",
"version": "1.0.0",
"main": "lib/delegate.js",

@@ -6,0 +6,0 @@ "scripts": [

@@ -18,5 +18,2 @@ /*jshint browser:true, node:true*/

function Delegate(root) {
if (root) {
this.root(root);
}

@@ -29,3 +26,6 @@ /**

*/
this.listenerMap = {};
this.listenerMap = [{}, {}];
if (root) {
this.root(root);
}

@@ -59,7 +59,12 @@ /** @type function() */

if (this.rootElement) {
for (eventType in listenerMap) {
if (listenerMap.hasOwnProperty(eventType)) {
this.rootElement.removeEventListener(eventType, this.handle, this.captureForType(eventType));
for (eventType in listenerMap[1]) {
if (listenerMap[1].hasOwnProperty(eventType)) {
this.rootElement.removeEventListener(eventType, this.handle, true);
}
}
for (eventType in listenerMap[0]) {
if (listenerMap[0].hasOwnProperty(eventType)) {
this.rootElement.removeEventListener(eventType, this.handle, false);
}
}
}

@@ -86,7 +91,12 @@

// Set up master event listeners
for (eventType in listenerMap) {
if (listenerMap.hasOwnProperty(eventType)) {
this.rootElement.addEventListener(eventType, this.handle, this.captureForType(eventType));
for (eventType in listenerMap[1]) {
if (listenerMap[1].hasOwnProperty(eventType)) {
this.rootElement.addEventListener(eventType, this.handle, true);
}
}
for (eventType in listenerMap[0]) {
if (listenerMap[0].hasOwnProperty(eventType)) {
this.rootElement.addEventListener(eventType, this.handle, false);
}
}

@@ -123,3 +133,3 @@ return this;

*
* @param {string} eventType Listen for these events (in a space-separated list)
* @param {string} eventType Listen for these events
* @param {string|undefined} selector Only handle events on elements matching this selector, if undefined match root element

@@ -130,4 +140,4 @@ * @param {function()} handler Handler function - event data passed here will be in event.data

*/
Delegate.prototype.on = function(eventType, selector, handler, eventData) {
var root, listenerMap, matcher, matcherParam, self = this;
Delegate.prototype.on = function(eventType, selector, handler, useCapture) {
var root, listenerMap, matcher, matcherParam;

@@ -141,10 +151,11 @@ if (!eventType) {

if (typeof selector === 'function') {
useCapture = handler;
handler = selector;
selector = null;
eventData = handler;
}
// Normalise undefined eventData to null
if (eventData === undefined) {
eventData = null;
// Fallback to sensible defaults
// if useCapture not set
if (useCapture === undefined) {
useCapture = this.captureForType(eventType);
}

@@ -157,3 +168,3 @@

root = this.rootElement;
listenerMap = this.listenerMap;
listenerMap = this.listenerMap[useCapture ? 1 : 0];

@@ -163,3 +174,3 @@ // Add master handler for type if not created yet

if (root) {
root.addEventListener(eventType, this.handle, this.captureForType(eventType));
root.addEventListener(eventType, this.handle, useCapture);
}

@@ -202,3 +213,2 @@ listenerMap[eventType] = [];

selector: selector,
eventData: eventData,
handler: handler,

@@ -222,4 +232,4 @@ matcher: matcher,

*/
Delegate.prototype.off = function(eventType, selector, handler) {
var i, listener, listenerMap, listenerList, singleEventType, self = this;
Delegate.prototype.off = function(eventType, selector, handler, useCapture) {
var i, listener, listenerMap, listenerList, singleEventType;

@@ -229,2 +239,3 @@ // Handler can be passed as

if (typeof selector === 'function') {
useCapture = handler;
handler = selector;

@@ -234,3 +245,11 @@ selector = null;

listenerMap = this.listenerMap;
// If useCapture not set, remove
// all event listeners
if (useCapture === undefined) {
this.off(eventType, selector, handler, true);
this.off(eventType, selector, handler, false);
return this;
}
listenerMap = this.listenerMap[useCapture ? 1 : 0];
if (!eventType) {

@@ -267,3 +286,3 @@ for (singleEventType in listenerMap) {

if (this.rootElement) {
this.rootElement.removeEventListener(eventType, this.handle, this.captureForType(eventType));
this.rootElement.removeEventListener(eventType, this.handle, useCapture);
}

@@ -294,3 +313,3 @@ }

root = this.rootElement;
listenerList = this.listenerMap[event.type];
listenerList = this.listenerMap[event.eventPhase === Event.CAPTURING_PHASE ? 1 : 0][event.type];

@@ -358,14 +377,3 @@ // Need to continuously check

Delegate.prototype.fire = function(event, target, listener) {
var returned, oldData;
if (listener.eventData !== null) {
oldData = event.data;
event.data = listener.eventData;
returned = listener.handler.call(target, event, target);
event.data = oldData;
} else {
returned = listener.handler.call(target, event, target);
}
return returned;
return listener.handler.call(target, event, target);
};

@@ -372,0 +380,0 @@

{
"name": "dom-delegate",
"version": "0.5.0",
"version": "1.0.0",
"author": "FT Labs <enquiries@labs.ft.com> (http://labs.ft.com/)",

@@ -5,0 +5,0 @@ "description": "Create and manage a DOM event delegator.",

@@ -37,3 +37,3 @@ # ftdomdelegate [![Build Status](https://travis-ci.org/ftlabs/ftdomdelegate.png?branch=master)](https://travis-ci.org/ftlabs/ftdomdelegate)

Download the [production version](http://github.com/ftlabs/ftdomdelegate/raw/master/build/dom-delegate.min.js) (<1k gzipped) or the [development version](http://github.com/ftlabs/dom-delegate/raw/master/build/dom-delegate.js).
Download the [built version](http://wzrd.in/standalone/dom-delegate@latest).

@@ -114,3 +114,3 @@ ## Usage ##

### .on(eventType, selector, handler[, eventData]) ###
### .on(eventType, selector, handler[, useCapture]) ###

@@ -125,13 +125,13 @@ #### `eventType (string)` ####

`null` is also accepted and will match the root element set by `root()`. Passing a handler function into `.on`'s second argument (with `eventData` as an optional third parameter) is equivalent to `.on(eventType, null, handler[, eventData])`.
`null` is also accepted and will match the root element set by `root()`. Passing a handler function into `.on`'s second argument is equivalent to `.on(eventType, null, handler)`.
#### `handler (function|*)` ####
#### `handler (function|boolean)` ####
Function that will handle the specified event on elements matching the given selector. The function will receive two arguments: the native event object and the target element, in that order.
Function that will handle the specified event on elements matching the given selector. The function will receive two arguments: the native event object and the target element, in that order.
#### `eventData (*)` ####
#### `useCapture (boolean)` ####
If defined and non-null, will be made available in `event.data`.
Whether or not to listen during the capturing (pass in `true`) or bubbling phase (pass in `false`). If no value passed in, it will fallback to a 'sensible default', which is `true` for `error`, `blur` and `focus` events and `false` for all other types.
### .off([eventType][, selector][, handler]) ###
### .off([eventType][, selector][, handler][, useCapture]) ###

@@ -148,8 +148,12 @@ Calling `off` with no arguments will remove all registered listeners, effectively resetting the instance.

If null passed listeners registered to the root element will be removed. Passing in a function into `off`'s second parameter is equivalent to `.off(eventType, null, handler)` (the third parameter will be ignored).
If null passed listeners registered to the root element will be removed. Passing in a function into `off`'s second parameter is equivalent to `.off(eventType, null, handler[, useCapture])` (the third parameter will be ignored).
#### `handler (function)` ####
#### `handler (function|boolean)` ####
Only remove listeners registered with the given handler function, among the other arguments.
Only remove listeners registered with the given handler function, among the other arguments. If not provided, remove all handlers.
#### `useCapture (boolean)` ####
Only remove listeners with `useCapture` set to the value passed in. If not provided, remove listeners added with `useCapture` set to `true` and `false`.
### .root([element]) ###

@@ -167,2 +171,2 @@

The developers of ftdomdelegate are [Matthew Andrews](https://twitter.com/andrewsmatt) and [Matthew Caruana Galizia](http://twitter.com/mcaruanagalizia). Test engineering by [Sam Giles](https://twitter.com/SamuelGiles_). The API is influenced by [jQuery Live](http://api.jquery.com/live/). All open source code released by FT Labs is licenced under the MIT licence. We welcome comments, feedback and suggestions. Please feel free to raise an issue or pull request. Enjoy.
The developers of ftdomdelegate are [Matthew Andrews](https://twitter.com/andrewsmatt) and [Matthew Caruana Galizia](http://twitter.com/mcaruanagalizia). Test engineering by [Sam Giles](https://twitter.com/SamuelGiles_). The API is influenced by [jQuery Live](http://api.jquery.com/live/). All open source code released by FT Labs is licenced under the MIT licence. We welcome comments, feedback and suggestions. Please feel free to raise an issue or pull request.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc