Comparing version 0.0.3 to 0.0.4
121
index.js
var flatten = require('flatten-list'), | ||
forEach = require('mout/array/forEach'), | ||
ieBug = require('./ie-bug'), | ||
document = window.document; | ||
function on(nodes, event, handler) { | ||
nodes.forEach(function(node) { | ||
node.addEventListener(event, handler, false); | ||
}); | ||
return { | ||
remove: function() { remove(nodes, event, handler); } | ||
}; | ||
if (ieBug.check(trigger)) { | ||
trigger = ieBug.fix(trigger); | ||
} | ||
function remove(nodes, event, handler) { | ||
nodes.forEach(function(node) { | ||
node.removeEventListener(event, handler, false); | ||
}); | ||
module.exports = events; | ||
function events() { | ||
return new Events(flatten(arguments)); | ||
} | ||
function once(nodes, event, handler) { | ||
var listener; | ||
listener = on(nodes, event, function(e) { | ||
listener.remove(); | ||
handler.call(this, e); | ||
}); | ||
return listener; | ||
} | ||
function createEvent(event) { | ||
var e = document.createEvent('Event'); | ||
e.initEvent(event, true, true); | ||
return e; | ||
} | ||
function trigger(nodes, event) { | ||
var e = createEvent(event); | ||
nodes.forEach(function(node) { | ||
node.dispatchEvent(e); | ||
}); | ||
} | ||
function Events(nodes) { | ||
@@ -60,56 +31,44 @@ this.nodes = nodes; | ||
function events() { | ||
return new Events(flatten(arguments)); | ||
} | ||
function on(nodes, event, handler) { | ||
forEach(nodes, function(node) { | ||
node.addEventListener(event, handler, false); | ||
}); | ||
module.exports = events; | ||
// Fix bug that occurs in at least IE 9 and 10 | ||
// Some newly-created nodes will not fire events until they are added to an | ||
// element. After they are added to an element, they will work even after they | ||
// are removed. | ||
// | ||
// The fix is to create an empty container element. Before triggering an event | ||
// on any element that does not have a parent, add the element to the container | ||
// and immediately remove it. | ||
function checkTriggerBug() { | ||
var a = document.createElement('div'), | ||
called = false; | ||
// Check if click event works on new DOM element | ||
a.addEventListener('click', function() { called = true; }, false); | ||
trigger([a], 'click'); | ||
if (called) { | ||
return false; | ||
function removeListener() { | ||
remove(nodes, event, handler); | ||
} | ||
// Check if event works on element after it is added to another | ||
var b = document.createElement('div'); | ||
b.appendChild(a); | ||
trigger([a], 'click'); | ||
return { | ||
remove: removeListener | ||
}; | ||
} | ||
// If it works now, it has the bug | ||
return called; | ||
function remove(nodes, event, handler) { | ||
forEach(nodes, function(node) { | ||
node.removeEventListener(event, handler, false); | ||
}); | ||
} | ||
function fixTrigger() { | ||
var container = document.createElement('div'); | ||
function once(nodes, event, handler) { | ||
var listener; | ||
function onceHandler(e) { | ||
listener.remove(); | ||
handler.call(this, e); | ||
} | ||
function trigger(nodes, event) { | ||
var e = createEvent(event); | ||
nodes.forEach(function(node) { | ||
if (node.parentNode === null) { | ||
container.appendChild(node); | ||
container.removeChild(node); | ||
} | ||
listener = on(nodes, event, onceHandler); | ||
return listener; | ||
} | ||
node.dispatchEvent(e); | ||
}); | ||
} | ||
function trigger(nodes, event) { | ||
var e = createEvent(event); | ||
forEach(nodes, function(node) { | ||
node.dispatchEvent(e); | ||
}); | ||
} | ||
return trigger; | ||
function createEvent(event) { | ||
var e = document.createEvent('Event'); | ||
e.initEvent(event, true, true); | ||
return e; | ||
} | ||
if (checkTriggerBug()) { | ||
trigger = fixTrigger(); | ||
} |
{ | ||
"name": "chi-events", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Easily manage DOM events", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "chi-build test", | ||
"serve": "chi-build serve", | ||
"lint": "chi-build lint" | ||
"test": "hut-build test", | ||
"serve": "hut-build serve", | ||
"lint": "hut-build lint" | ||
}, | ||
@@ -28,8 +28,9 @@ "repository": { | ||
"devDependencies": { | ||
"tape": "~1.0.4", | ||
"chi-build": "0.0.4" | ||
"tape": "~1.1.1", | ||
"hut-build": "~0.1.4" | ||
}, | ||
"dependencies": { | ||
"flatten-list": "~0.0.1" | ||
"flatten-list": "~0.0.1", | ||
"mout": "~0.7.1" | ||
} | ||
} |
@@ -5,5 +5,7 @@ # chi-events | ||
[![Build Status](https://travis-ci.org/conradz/chi-events.png?branch=master)](https://travis-ci.org/conradz/chi-events) | ||
[![Dependency Status](https://gemnasium.com/conradz/chi-classes.png)](https://gemnasium.com/conradz/chi-classes) | ||
[![Build Status](https://drone.io/github.com/conradz/chi-events/status.png)](https://drone.io/github.com/conradz/chi-events/latest) | ||
[![Dependency Status](https://gemnasium.com/conradz/chi-events.png)](https://gemnasium.com/conradz/chi-events) | ||
[![Selenium Test Status](https://saucelabs.com/browser-matrix/chi-events.svg)](https://saucelabs.com/u/chi-events) | ||
Easily manage DOM events. | ||
@@ -65,5 +67,5 @@ | ||
### `trigger(event)` | ||
### `#trigger(event)` | ||
Creates an event with the event name `event` and dispatches the event to all the | ||
DOM nodes. | ||
DOM nodes. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10429
209
70
2
+ Addedmout@~0.7.1
+ Addedmout@0.7.1(transitive)